ESP32 PWM with Arduino IDE

ESP32 LED PWM Controller

PWM on ESP32

The ESP32 features two PWM peripherals: the LED Control Peripheral (LEDC) and the Motor Control Pulse Width Modulator Peripheral (MCPWM).

The MCPWM is primarily designed for motor control applications, offering features like dead zone management and auto-braking. Meanwhile, the LEDC is tailored for LED driving, boasting advanced functionalities like auto-dimming. Despite its LED-centric design, the LEDC can also be repurposed to generate PWM signals for various other applications beyond LED control.

PWM frequency

PWM frequency refers to the rate at which the PWM signal repeats its cycle and is measured in Hertz (Hz).For instance, if we set a PWM frequency of 1 Hz, the timer will complete one cycle in 1 second, while a frequency of 1000 Hz means the cycle completes in 1 millisecond.The ESP32’s LEDC peripheral allows PWM signals with frequencies of up to 40 MHz, offering a wide range of frequency options for various applications.

PWM Resolution

  • PWM resolution refers to the number of bits used to represent the PWM signal’s amplitude or intensity.
  • A higher resolution allows for finer control over the PWM signal’s levels, resulting in smoother transitions and more precise adjustments.
  • For example, if the PWM resolution is set to 8 bits, the timer counts from 0 to 255 before resetting, while a resolution of 16 bits allows counting from 0 to 65,535.
  • The PWM resolution affects the granularity of the PWM signal, influencing the accuracy and granularity of the output signal.

Duty cycle

The duty cycle determines the duration the PWM output remains high before transitioning to low. This duration is controlled by the value stored in the timer’s capture/compare register (CCR).

Initially, when the timer resets, the PWM output becomes high. As the timer counts, once it matches the value stored in the capture/compare register, the PWM output switches to low. The timer continues counting, and upon reaching its maximum value, the PWM output returns to high, initiating a new cycle of counting for the subsequent period.


To dim an LED using PWM with the ESP32’s LED PWM controller in the Arduino IDE, follow these steps:

  1. Begin by selecting a PWM channel, ranging from 0 to 15.
  2. Set the PWM signal frequency. For LED applications, a frequency of 5000 Hz is typically suitable.
  3. Specify the duty cycle resolution, ranging from 1 to 16 bits. For LED brightness control, opt for 8-bit resolution, allowing values from 0 to 255.
  4. Use the following function to assign the PWM signal to a GPIO or GPIOs:
ledcAttachPin(GPIO, channel);

5. This function takes two arguments: the GPIO responsible for outputting the signal and the channel generating the signal. To manage LED brightness through PWM, employ the following function:

ledcWrite(channel, dutycycle);

Code:

Dim an LED using the LED PWM controller of the ESP32

const int ledPin = 5; // GPIO pin connected to the LED

void setup() {
  // Initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);

  // Configure LED PWM channel
  ledcSetup(0, 5000, 8); // Channel 0, 5000 Hz frequency, 8-bit resolution

  // Attach the LED PWM channel to the GPIO pin
  ledcAttachPin(ledPin, 0);
}

void loop() {
  // Dim the LED gradually
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
    // Set the LED PWM duty cycle
    ledcWrite(0, dutyCycle);
    delay(10); // Delay for smooth dimming effect
  }

  // Brighten the LED gradually
  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
    // Set the LED PWM duty cycle
    ledcWrite(0, dutyCycle);
    delay(10); // Delay for smooth dimming effect
  }
}

This code gradually dims and brightens an LED connected to GPIO pin 5 using the LED PWM controller of the ESP32. Adjust the ledPin variable to match the GPIO pin to which your LED is connected.

Brief explanation of how the code works:

  1. Setup:
    • The ledPin variable is defined to specify the GPIO pin connected to the LED.
    • In the setup() function, the LED pin is initialized as an output using pinMode().
    • The ledcSetup() function configures the LED PWM channel. It specifies Channel 0, a PWM frequency of 5000 Hz, and an 8-bit resolution for dimming the LED. The frequency determines how fast the LED pulses, and the resolution determines the range of brightness levels.
    • The ledcAttachPin() function attaches the LED PWM channel to the GPIO pin specified by ledPin.
  2. Loop:
    • The loop() function contains two loops: one for gradually dimming the LED and one for gradually brightening it.
    • In the first loop, the duty cycle of the LED PWM signal is incremented from 0 to 255, gradually dimming the LED.
    • Each duty cycle value corresponds to a specific brightness level, where 0 represents fully off and 255 represents fully on.
    • The ledcWrite() function sets the duty cycle of the LED PWM signal for Channel 0.
    • The delay() function is used to introduce a short delay between each change in brightness, creating a smooth dimming effect.
    • The second loop does the opposite, gradually increasing the duty cycle from 255 to 0, thereby brightening the LED.

Overall, this code achieves a smooth dimming and brightening effect by adjusting the duty cycle of the LED PWM signal using the ESP32’s LED PWM controller.

    Leave a Reply

    Your email address will not be published.

    Need Help?