esp32 arduino pwm example

ESP32 Arduino PWM Example: Control Motors & LEDs

By William Miller
AD

The ESP32, a low-cost, low-power system on a chip microcontroller with integrated Wi-Fi and dual-mode Bluetooth, can be quite a handful for novices due to its vast capabilities. However, with the right guidance, it's possible to unlock its potential using Arduino, a popular open-source electronics platform. In this guide, we'll explore how to create a PWM (Pulse Width Modulation) example using the ESP32 and the Arduino IDE.

But first, let's briefly discuss PWM and how it works in the context of ESP32 and Arduino.

Understanding PWM on ESP32 with Arduino

PWM is a method of encoding information into a digital signal by using varying pulse widths. On the ESP32, PWM can be generated using the Hardware PWM module, which supports up to 16 channels. To use this with Arduino, you'll need to add the ESP32 board package and the necessary libraries to your Arduino IDE.

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

Now that we understand PWM and its relevance to ESP32 and Arduino, let's dive into the key aspects of setting up and using PWM in our projects.

Setting up the Arduino IDE for ESP32

Before we can start writing code, we need to ensure our Arduino IDE is configured to work with the ESP32. This involves installing the ESP32 board package and setting up the correct boards and ports in the IDE. Here's a step-by-step guide to do this:

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 )

1. Install the ESP32 board package by following the official ESP32 Arduino core installation instructions. You can find these in the Arduino IDE by going to File → Preferences and typing 'https://github.com/espressif/arduino-esp32' into the 'Additional Boards Manager URLs' field.

2. Once installed, you can select your ESP32 board from the Tools → Board menu. For this example, let's choose 'ESP32 Dev Module'.

AD

Writing the PWM Code

Now that our Arduino IDE is set up for the ESP32, we can start writing our PWM code. Here's a simple example that uses PWM to control the brightness of an LED connected to one of the ESP32's PWM-capable GPIO pins:

PWM Tutorial for Arduino, ESP8266 and ESP32
PWM Tutorial for Arduino, ESP8266 and ESP32
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
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 )
How to Send SMS from ESP32
How to Send SMS from ESP32
ESP32 CYD with ESP-NOW: Receive Data | Random Nerd Tutorials
ESP32 CYD with ESP-NOW: Receive Data | Random Nerd Tutorials

```cpp const int LED_PIN = 25; // PWM-capable GPIO pin connected to the LED void setup() { ledcSetup(0, 5000, 8); // Configure the LEDC timer with 8-bit resolution and a frequency of 5000 Hz ledcAttachPin(LED_PIN, 0); // Attach the LED to the specified channel (0) } void loop() { for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) { // Loop through duty cycles from 0 to 255 ledcWrite(0, dutyCycle); // Write the duty cycle to the attached LED channel delay(10); // Wait for 10ms before changing the duty cycle } } ```

This code initializes the PWM system with an 8-bit resolution and a frequency of 5000 Hz. It then attaches an LED connected to GPIO25 to the PWM channel and slowly increases the brightness by incrementing the duty cycle in a loop.

AD

Troubleshooting PWM on ESP32 with Arduino

While PWM is generally straightforward, you may encounter issues during implementation. Here are two common problems and their solutions:

Measure Wind Speed with Anemometer on ESP32 TFT Display
Measure Wind Speed with Anemometer on ESP32 TFT Display
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 Interrupts and Timers with PIR Sensor using Arduino IDE
ESP32 Interrupts and Timers with PIR Sensor using Arduino IDE
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!)
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
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
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
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
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
an electronic device is shown in this poster, with instructions on how to use it
an electronic device is shown in this poster, with instructions on how to use it
Tutorial:ESP32 & DC Motors
Tutorial:ESP32 & DC Motors

LED Not Brightening/Changing Color

If your LED is not changing brightness or color as expected, double-check that theISP32 board you're using supports PWM on the pin you've selected. Additionally, ensure that the LED is correctly connected and that your PWM timings and frequencies are within the acceptable range. If you're using a common-anode LED, you might need to adjust your code to account for this.

Noise or Artifacts on the Signal

If you're seeing noise or artifacts on your PWM signal, it's possible that the ESP32's internal oscillator is causing interference. Try adjusting the PWM frequency slightly to avoid harmonics of the oscillator frequency. You can also use filtering techniques to smooth out any remaining noise.

AD

The ESP32's PWM capabilities are incredibly versatile and powerful. With the right implementation and a bit of experimentation, you can create a wide range of applications using PWM and the ESP32 with Arduino. Whether you're building LED lighting projects, controlling motors, or Kota's why it's essential to stay informed about the latest developments in this dynamic field. Keep learning, keep experimenting, and most importantly, keep building!

AD