esp32 arduino pwm example

ESP32 Arduino PWM Tutorial: Generate Custom Signals

By Hannah Walker
AD

If you're delving into the world of IoT and microcontrollers, you're likely to have encountered ESP32 and Arduino. Both are incredibly popular for their versatility, power, and cost-effectiveness. Today, we're going to explore a practical use case: pwm on ESP32 using Arduino IDE.

Before we dive into PWM with ESP32, let's quickly understand what PWM is and why it's crucial. PWM, or Pulse Width Modulation, is a technique that's primarily used to control the speed of DC motors, and it works by rapidly turning a signal on and off. By varying the ratio of on-to-off time, you can control the power delivered to a device, thus controlling its speed.

Understanding PWM on ESP32

ESP32, being a powerful microcontroller, supports PWM out of the box. It has 16 PWM channels that can be used to control various devices like LEDs, DC motors, or even to generate audio tones.

AD
ESP32 PWM - The Engineering Projects
ESP32 PWM - The Engineering Projects
In-Depth ESP32 PWM Tutorial | How to use PWM in ESP32?
In-Depth ESP32 PWM Tutorial | How to use PWM in ESP32?
ESP32 PWM Signals with Arduino – Analog Output
ESP32 PWM Signals with Arduino – Analog Output

Before we start, make sure you've installed the ESP32 board definition in your Arduino IDE. Go to Tools > Board > Boards Manager, then search for "ESP32" and install it.

Configuring the PWM Pin

ESP32 allows you to configure PWM on certain GPIO pins. To do this, you need to include the ledc library in your code:

10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
ESP32 PWM with Arduino IDE ( LED Fading Example )
ESP32 PWM with Arduino IDE ( LED Fading Example )

// Include the ledc library at the beginning of your sketch #include "ledc.h"

Setting up the Frequency and Channel

Now, let's set up the PWM frequency. The ESP32 supports frequencies up to around 80MHz. Here's how you can set it up:

AD

// Set the frequency (in Hz) and the channel (0-15) ledcSetup(0, 5000, 8);

PWM Tutorial for Arduino, ESP8266 and ESP32
PWM Tutorial for Arduino, ESP8266 and ESP32
ESP32 Multiple Sliders WebSocket Web Server - PWM Control LED
ESP32 Multiple Sliders WebSocket Web Server - PWM Control LED
ESP32 PWM with Arduino IDE ( LED Fading Example )
ESP32 PWM with Arduino IDE ( LED Fading Example )
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
How to Send SMS from ESP32
How to Send SMS from ESP32
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE

Controlling PWM with Arduino Functions

Once you've set up the PWM, you can use Arduino functions to control it. Here, we'll use `analogWrite()` and `ledcWrite()`.

The `analogWrite()` function isn't as powerful as `ledcWrite()` but it's easier to use. It works on a scale of 0-255, where 0 is off and 255 is full power:

AD

// Turn on PWM with full power analogWrite(4, 255);

ESP32 CYD with ESP-NOW: Receive Data | Random Nerd Tutorials
ESP32 CYD with ESP-NOW: Receive Data | Random Nerd Tutorials
ESP32 Interrupts and Timers with PIR Sensor using Arduino IDE
ESP32 Interrupts and Timers with PIR Sensor using Arduino IDE
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
Custom Arduino Library for ESP32 📦⚙️
Custom Arduino Library for ESP32 📦⚙️
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
Arduino Esp32-dev powering with 9V battery connected to pins
Arduino Esp32-dev powering with 9V battery connected to pins
ESP8266 PWM with Arduino IDE – LED Fading
ESP8266 PWM with Arduino IDE – LED Fading
Mini proiect (33) - Înregistrarea audio cu ESP32 și INMP441 într-un fisier wave, și salvat pe un card microSD
Mini proiect (33) - Înregistrarea audio cu ESP32 și INMP441 într-un fisier wave, și salvat pe un card microSD
Measure Wind Speed with Anemometer on ESP32 TFT Display
Measure Wind Speed with Anemometer on ESP32 TFT Display
ESP 32 Wroom-32 pin allocation
ESP 32 Wroom-32 pin allocation
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32-C3 Super Mini Pinout & Specs 📌⚡
ESP32-C3 Super Mini Pinout & Specs 📌⚡
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!

Dimming LEDs Using PWM

However, if you want more control over your PWM, you should use `ledcWrite()`. It uses a duty cycle, measured in a scale of 0-1023, where 0 is off and 1023 is full power. Here's how you can dim an LED gradually:

// Gradually dim the LED from full power to off for (int dutyCycle = 1023; dutyCycle >= 0; dutyCycle--) { ledcWrite(0, dutyCycle); delayMicroseconds(100); }

AD

Changing PWM Frequency Dynamically

Not only can you control the power delivered, but you can also change the PWM frequency on the fly. Here's an example:

// Change PWM frequency dynamically ledcSetup(0, 5000, 8); delay(1000); ledcSetup(0, 2500, 8);

The world of IoT is vast and full of possibilities. Mastering PWM on ESP32 using Arduino IDE is a substantial step towards unlocking this potential. Whether you're creating robotics projects, smart home gadgets, or LED lighting systems, PWM is a crucial technique to keep in your toolbox. Now that you've got a solid understanding and hands-on experience with PWM on ESP32, it's time to start experimenting and building something amazing!

AD