ESP32 PWM With Arduino IDE (Analog Output)

Learn to generate PWM signals with the ESP32 using Arduino IDE. We’ll explain two different methods: using analogWrite and using the LEDC API. As an example, we’ll build a simple circuit to fade an LED.

ESP32 PWM with Arduino IDE Analog Output LED

Updated 11 June 2024

Before proceeding with this tutorial you should have the ESP32 add-on installed in your Arduino IDE. Follow the next tutorial to install the ESP32 on the Arduino IDE, if you haven’t already.

  • Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)

This tutorial is compatible with ESP32 board add-on version 3.X or above – learn more in our migration guide.

Table of Contents

  • ESP32 PWM Controller
    • analogWrite
    • LEDC Functions
  • Dimming an LED with the ESP32
  • ESP32 PWM Example using analogWrite – Code
  • ESP32 PWM Example using the LEDC API – Code

Parts Required

To follow this tutorial you need these parts:

  • ESP32 DOIT DEVKIT V1 Board – read best ESP32 development boards
  • 3x 5mm LED
  • 3x 330 Ohm resistor (or similar values)
  • Breadboard
  • Jumper wires

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

ESP32 LED PWM Controller

The ESP32 has an LED PWM controller with 6 to 16 independent channels (depending on the ESP32 model) that can be configured to generate PWM signals with different properties.

There are different functions you can use to generate PWM signals and achieve the same results. You can use analogWrite (like in Arduino boards) or you can use LEDC functions.

analogWrite

The most basic function is analogWrite which accepts as arguments the GPIO where you want to generate the PWM signal and the duty cycle value (ranging from 0 to 255).

void analogWrite(uint8_t pin, int value);

For example:

void analogWrite(2, 180);

Set the Frequency and Resolution

You can set the resolution and frequency of the PWM signal on a selected pin by using the analogWriteResolution and analogWriteFrequency functions.

To set the resolution:

void analogWriteResolution(uint8_t pin, uint8_t resolution);

To set the frequency:

void analogWriteFrequency(uint8_t pin, uint32_t freq);

LEDC Functions

Alternatively, you can use the Arduino-ESP32 LEDC API. First, you need to set up an LEDC pin. You can use the ledcAttach or ledcAttachChannel functions.

ledcAttach

The ledcAttach function sets up an LEDC pin with a given frequency and resolution. The LEDC channel will be selected automatically.

bool ledcAttach(uint8_t pin, uint32_tfreq, uint8_t resolution);

This function will return true if the configuration is successful. If false is returned, an error occurs and the LEDC channel is not configured.

ledcAttachChannel

If you prefer to set up the LEDC channel manually, you can use the ledcAttachChannel function instead.

bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel);

This function will return true if the configuration is successful. If false is returned, an error occurs and the LEDC channel is not configured.

ledcWrite

Finally, after setting the LEDC pin using one of the two previous functions, you use the ledcWrite function to set the duty cycle of the PWM signal.

void ledcWrite(uint8_t pin, uint32_t duty);

This function will return true if setting the duty cycle is successful. If false is returned, an error occurs and the duty cycle is not set.

For more information and all functions of the LEDC PWM controller, check the official documentation.

Dimming an LED with the ESP32

To show you how to generate PWM signals with the ESP32, we’ll create two simple examples that dim the brightness of an LED (increase and decrease brightness over time). We’ll provide an example using analogWrite and another using the LEDC functions.

Schematic

Wire an LED to your ESP32 as in the following schematic diagram. The LED should be connected to GPIO 16.

Dimming an LED with the ESP32

(This schematic uses the ESP32 DEVKIT V1 module version with 30 GPIOs – if you’re using another model, please check the pinout for the board you’re using.)

Note: You can use any pin you want, as long as it can act as an output. All pins that can act as outputs can be used as PWM pins. For more information about the ESP32 GPIOs, read: ESP32 Pinout Reference: Which GPIO pins should you use?

ESP32 PWM Example using analogWrite – Code

Open your Arduino IDE and copy the following code. This example increases and decreases the LED brightness over time using the analogWrite function.

/* Rui Santos & Sara Santos - Random Nerd Tutorials Complete project details at https://RandomNerdTutorials.com/esp32-pwm-arduino-ide/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ // the number of the LED pin const int ledPin = 16; // 16 corresponds to GPIO 16 void setup() { // set the LED as an output pinMode(ledPin, OUTPUT); } void loop(){ // increase the LED brightness for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){ // changing the LED brightness with PWM analogWrite(ledPin, dutyCycle); delay(15); } // decrease the LED brightness for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){ // changing the LED brightness with PWM analogWrite(ledPin, dutyCycle); delay(15); } }

View raw code

You start by defining the pin the LED is attached to. In this example, the LED is attached to GPIO 16.

const int ledPin = 16; // 16 corresponds to GPIO 16

In the setup(), you need to configure the LED as an output using the pinMode() function.

pinMode(ledPin, OUTPUT);

In the loop(), you vary the duty cycle between 0 and 255 to increase the LED brightness.

// increase the LED brightness for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){ // changing the LED brightness with PWM analogWrite(ledPin, dutyCycle); delay(15); }

Notice the use of the analogWrite() function to set up the duty cycle. You just need to pass as arguments the LED pin and the duty cycle.

analogWrite(ledPin, dutyCycle);

Finally, we vary the duty cycle between 255 and 0 to decrease the brightness.

// decrease the LED brightness for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){ // changing the LED brightness with PWM analogWrite(ledPin, dutyCycle); delay(15); }

Testing the Example

Upload the code to your ESP32. Make sure you have the right board and COM port selected. Look at your circuit. You should have a dimmer LED that increases and decreases brightness over time.

ESP32 PWM with Arduino IDE- Analog Output LED Set to 0 LED off
ESP32 PWM with Arduino IDE- Analog Output LED Set to 50
ESP32 PWM with Arduino IDE- Analog Output LED Set to 100 maximum brightness
ESP32 PWM with Arduino IDE- Analog Output LED Set to 75

ESP32 PWM Example using the LEDC API – Code

Open your Arduino IDE and copy the following code. This example increases and decreases the LED brightness over time using the ESP32 LEDC functions.

/* Rui Santos & Sara Santos - Random Nerd Tutorials Complete project details at https://RandomNerdTutorials.com/esp32-pwm-arduino-ide/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ // the number of the LED pin const int ledPin = 16; // 16 corresponds to GPIO16 // setting PWM properties const int freq = 5000; const int resolution = 8; void setup(){ // configure LED PWM ledcAttach(ledPin, freq, resolution); // if you want to attach a specific channel, use the following instead //ledcAttachChannel(ledPin, freq, resolution, 0); } void loop(){ // increase the LED brightness for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){ // changing the LED brightness with PWM ledcWrite(ledPin, dutyCycle); delay(15); } // decrease the LED brightness for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){ // changing the LED brightness with PWM ledcWrite(ledPin, dutyCycle); delay(15); } }

View raw code

You start by defining the pin the LED is attached to. In this example, the LED is attached to GPIO 16.

const int ledPin = 16;

Set the PWM properties: frequency and resolution.

// setting PWM properties const int freq = 5000; const int resolution = 8;

As we’re using 8-bit resolution, the duty cycle will be controlled using a value from 0 to 255.

In the setup(), set up the LEDC pin—use the ledcAttach function as follows.

ledcAttach(ledPin, freq, resolution);

This will configure the LEDC pin with the frequency and resolution defined previously on a default PWM channel.

If you want to set up the PWM channel yourself, you need to use ledcAttachChannel instead. The last argument of this function is the PWM channel number.

ledcAttachChannel(ledPin, freq, resolution, 0);

Finally, in the loop(), you increase and decrease the brightness of the LED over time.

The following lines increase the LED brightness.

// increase the LED brightness for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){ // changing the LED brightness with PWM ledcWrite(ledPin, dutyCycle); delay(15); }

Notice the use of the ledcWrite() function to set up the duty cycle. You just need to pass as arguments the LED pin and the duty cycle.

ledcWrite(ledPin, dutyCycle);

Finally, we vary the duty cycle between 255 and 0 to decrease the brightness.

// decrease the LED brightness for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){ // changing the LED brightness with PWM ledcWrite(ledPin, dutyCycle); delay(15); }

Testing the Example

Upload the code to your ESP32. Make sure you have the right board and COM port selected. Look at your circuit. You should have a dimmer LED that increases and decreases brightness over time just like in the previous example.

ESP32 PWM with Arduino IDE- Analog Output LED Set to 0 LED off
ESP32 PWM with Arduino IDE- Analog Output LED Set to 50
ESP32 PWM with Arduino IDE- Analog Output LED Set to 75
ESP32 PWM with Arduino IDE- Analog Output LED Set to 100 maximum brightness

Wrapping Up

In summary, in this article, you learned how to use the LED PWM controller of the ESP32 with the Arduino IDE to dim an LED. The concepts learned can be used to control other outputs with PWM by setting the right properties to the signal.

We have more ESP32 resources you may like:

  • Getting Started with ESP32 Dev Module
  • ESP32 Pinout Reference: Which GPIO pins should you use?
  • ESP32 Digital Inputs and Digital Outputs (Arduino IDE)
  • ESP32 ADC – Read Analog Values with Arduino IDE
  • ESP32 with PIR Motion Sensor using Interrupts and Timers
  • More ESP32 Tutorials and Projects

This is an excerpt from our course: Learn ESP32 with Arduino IDE. If you like ESP32 and you want to learn more, we recommend enrolling in Learn ESP32 with Arduino IDE course.

Tag » Arduino Code Pwm Output