ESP32 PWM application and sample program description

2020/01/1823:38:09 technology 981

ESP32 PWM with Arduino IDE

In this tutorial, we will show you how to use Arduino IDE to generate PWM signal through ESP32. As an example, we will build a simple circuit that uses the LED PWM controller of ESP32 to dim the LED. We will also show you how to get the same PWM signal on different GPIOs at the same time.

Before continuing this tutorial, you should install the ESP32 plugin in the Arduino IDE. Parts required for

To use this tutorial, you need the following parts:

ESP32 DOIT DEVKIT V1 development board

  • 3 5mm LED
  • 3 330 ohm resistors
  • breadboard
  • jumper
  • z0 PWM controller with LED ESP32 LED

    ESP32 LED 16 independent channels can be configured to generate PWM signals with different properties.

    This is the step you must use Arduino IDE to dim the LED through PWM:

    1. First, you need to select a PWM channel. There are 16 channels from 0 to 15.

    2. Then, you need to set the PWM signal frequency. For LEDs, a frequency of 5000 Hz can be used.

    3. You also need to set the duty cycle resolution of the signal: the resolution is 1 to 16 bits. We will use 8-bit resolution, which means you can use a value between 0 and 255 to control the LED brightness.

    4. Next, you need to specify which GPIO the signal will appear on. For this, you will use the following function:

    ledcAttachPin(GPIO, channel)

    This function accepts two parameters. The first is the GPIO that will output the signal, and the second is the channel that will generate the signal.

    5. Finally, to use PWM to control the brightness of the LED, please use the following function:

    ledcWrite(channel, dutycycle)

    This function accepts the channel and duty cycle of the PWM signal as parameters.

    dimming LED

    Let us look at a simple example to understand how to use the ESP32 LED PWM controller with Arduino IDE.

    schematic diagram

    as shown in the figure below, connect the LED wire to ESP32. The LED should be connected to GPIO 16.

    ESP32 PWM application and sample program description - DayDayNews

    (This schematic uses the ESP32 DEVKIT V1 module version with 30 GPIOs-if you are using other models, please check the pinout of the board used.)

    Note: you can use any pin you want , As long as it can be used as output. All pins that can be used as outputs can be used as PWM pins.

    program code

    Open your Arduino IDE and copy the following code.

    // the number of the LED pin

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

    // setting PWM properties

    const int freq = 5000;

    const int ledChannel = 0;

    const int resolution = 8;

    void setup(){

    z0 configure LED PWM functionalitites

    ledcSetup(ledChannel, freq, resolution);

    // attach the channel to the GPIO to be controlled

    ledcAttachPin(ledPin, ledChannel);

    }

    void loop(){

    // increase the LED brightness

    for(int dutyCycle = 0; dutyCycle <=>

    ledcWrite(ledChannel, dutyCycle);

    delay(15);

    }

    // decrease the LED brightness

    for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){zz

    // changing the LED brightness, with PWMWrite3 dutyCycle);

    delay(15);

    }

    }

    First define the pin to which the LED is connected. In this case, the LED is connected to GPIO 16.

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

    Then, set the properties of the PWM signal. The frequency you define is 5000 Hz, select channel 0 to generate the signal, and set the resolution to 8 bits. You can choose other attributes different from these to generate different PWM signals.

    const int freq = 5000;

    const int ledChannel = 0;

    const int resolution = 8;

    In setup(), you need to configure LEDPWM to the previously defined attributes by using ledcSetup() function, which accepts ledChannel, frequency and resolution The rate as a parameter is as follows:

    ledcSetup(ledChannel, freq, resolution);

    Next, you need to select the GPIO from which the signal will be obtained. To do this, use the ledcAttachPin() function, which accepts the GPIO to get the signal and the channel that generated the signal as parameters. In this example, we will get the signal in ledPin GPIO, which corresponds to GPIO 16. The channel that generates the signal is ledChannel, which corresponds to channel 0.

    ledcAttachPin(ledPin, ledChannel);

    In the loop, you will change the duty cycle from 0 to 255 to increase the brightness of the LED.

    for(int dutyCycle = 0; dutyCycle <=>

    // changing the LED brightness with PWM

    ledcWrite(ledChannel, dutyCycle);

    delay(15);

    }

    Then, reduce the brightness between 255 and 0.

    for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--)(

    // changing the LED brightness with PWM

    ledcWrite(ledChannel, dutyCycle);

    delay(15);

    }

    To set the brightness of the LED, you only need to use the ledcWrite() function, which accepts the channel and duty cycle of the generated signal as parameters.

    ledcWrite(ledChannel, dutyCycle);

    When we use 8-bit resolution, we will use a value between 0 and 255 to control the duty cycle. Please note that in the ledcWrite() function, we are using the channel that generates the signal, not the GPIO.

    test example

    upload the code to ESP32. Make sure to select the correct board and COM port. Look at your circuit. You should have a dimmer LED, which can increase and decrease brightness.

    ESP32 PWM application and sample program description - DayDayNews

    get the same signal on different GPIOs

    You can get the same signal from the same channel in different GPIOs. To do this, you only need to attach these GPIOs to the same channel of setup().

    Let us modify the previous example to use the same PWM signal from the same channel to dim the 3 LEDs.

    schematic

    follow the next schematic, add two more LEDs to the circuit:

    ESP32 PWM application and sample program description - DayDayNews

    (this schematic uses the ESP32 DEVKIT V1 module version with 30 GPIOs-if you are using other models, please check the circuit board used The pin arrangement.)

    program code

    Copy the following code to your Arduino IDE.

    // the number of the LED pin

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

    const int ledPin2 = 17; // 17 corresponds to GPIO17

    const int ledPin3 = 5; // 5 corresponds to GPIO5

    eq = setting PWM properties

    const int fr

    zeq = 17 5000;

    const int ledChannel = 0;

    const int resolution = 8;

    void setup(){

    // configure LED PWM functionalitites

    ledcSetup(ledChannel, freq, resolution);

    // attach the channel to the GPIO to be controlled

    ledPin, Attach );

    ledcAttachPin(ledPin2, ledChannel);

    ledcAttachPin(ledPin3, ledChannel);

    }

    void loop(){

    // increase the LED brightness

    for(int dutyCycle = 0; dutyCycle <=>

    ledcWrite(ledChannel, dutyCycle);

    delay(15);

    )

    // decrease the LED brightness

    for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){

    // changing the LED brightness with PWM

    ledcWrite(ledChannel, dutyCycle);

    delay(15);

    }

    }

    This code is the same as the previous code with some modifications. We defined two other variables for the two new LEDs, referencing GPIO 17 and GPIO 5 respectively.

    const int ledPin2 = 17; // 17 corresponds to GPIO17

    const int ledPin3 = 5; // 5 corresponds to GPIO5

    Then, in setup(), the following lines are added to assign two GPIOs to channel 0. This means that we will get the same signal generated on channel 0 on both GPIOs.

    ledcAttachPin(ledPin2, ledChannel);

    ledcAttachPin(ledPin3, ledChannel);

    test project

    Upload the program to ESP32. Make sure to select the correct board and COM port. Now, look at your circuit:

    ESP32 PWM application and sample program description - DayDayNews

    All GPIO output the same PWM signal. Therefore, all three LEDs increase and decrease their brightness at the same time, thereby creating a synchronization effect.

    ESP32 PWM application and sample program description - DayDayNews

    All in all, in this article, you learned how to use the ESP32 LED PWM controller with Arduino IDE to dim LEDs. By setting the correct properties for the signal, the learned concepts can be used for other output control of PWM.

    technology Category Latest News