Learn to generate PWM signals with the ESP32 using Arduino IDE. This tutorial explains two different methods: using analogWrite
and using the LEDC API. As an example, we’ll build a simple circuit to fade an LED.
The ESP32 is equipped with multiple PWM channels, allowing us to independently control the duty cycle of each channel. The duty cycle refers to the percentage of time the PWM signal is in the “ON” state compared to the total period of the signal. By varying the duty cycle, we can control the average power delivered to the device connected to the PWM output.
Working Principle
In this project, we will use the ESP32 to generate PWM signals to control the brightness of an LED. Pulse Width Modulation (PWM) is a technique used to simulate an analog signal using digital means. By varying the duty cycle of a digital signal, we can control the power delivered to an electronic component.
Things we Need
- ESP32 DOIT DEVKIT V1 Board
- 3x 5mm LED
- 3x 330 Ohm resistor (or similar values)
- Breadboard
- Jumper wires
Circuit Diagram
Libraries Required
Before proceeding with this tutorial, ensure you 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.
Code
const int ledPin = 12; // 12 corresponds to GPIO 12
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);
}
}
Instructions to Edit the Code:
- Change the
ledPin
variable if you are using a different GPIO. - Modify the
delay(15)
value to change the speed of the LED fading.
Example Using the LEDC API
This example increases and decreases the LED brightness over time using the ESP32 LEDC functions.
// the number of the LED pin
const int ledPin = 12; // 12 corresponds to GPIO12
// 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);
}
}
Instructions to Edit the Code:
- Change the
ledPin
variable if you are using a different GPIO. - Modify the
delay(15)
value to change the speed of the LED fading. - Adjust the
freq
andresolution
constants to change the frequency and resolution of the PWM signal.
Conclusion
In summary, 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. Experiment with different settings and components to explore the capabilities of PWM with the ESP32.