Introduction
Pulse Width Modulation or PWM is a technique that is used to create an analog like signal with the help of digital signals. The ON time and OFF time of the digital signal matters here. The longer the digital pin is HIGH, higher is the PWM value. Longer the digital pin is LOW, lower is the PWM value. PWM is commonly used to control the speed of electric motors, the brightness of lights, in ultrasonic cleaning applications, and many more.
Working Principle
We will be controlling the brightness of an LED. Only the PWM pins on the ESP32 have the ability to do it (Almost all are PWM pins – you need to check your board layout). Any normal digital pin will not function as desired. The LED in the project will be given a PWM signal from 0 to 255 and then the same signal will be reversed from 255 to 0. (The Pulse Width Modulation can be used for a lot of components and actuators.)
Components Required
- ESP32
- LED x 1
- 1kΩ Resistors x 1
- Breadboard x 1
How does the ESP32 give the PWM signal?
The ESP32 will provide a PWM signal ranging from 0-255. The values are in this range because of the configuration in the code for 8 bit resolution.
The ledcWrite( ) function will set the brightness of the LED. The PWM signal has ON time and OFF time, also called as the duty cycle which will decide the brightness of the LED.
Code – Simplified
const int ledPin = 12; // GPIO12
// setting PWM properties
const int frequency = 5000; // frequency
const int ledChannel = 0; // using the 0th Channel out of the 16 channels available
const int resolution = 8; // to make the timer of the ESP32 8 bit (0 - 255)
void setup(){
ledcSetup(ledChannel, frequency, resolution); //PWM LED configurations
ledcAttachPin(ledPin, ledChannel); // attach the channel to the GPIO to be controlled
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
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);
}
}
Extended Activity Simplified
Now if you want to add some more LED and control them using the PWM technique. If you individually want to control the brightness off the LED then we will need to use 2 different channels. If you want set the brightness for all the LEDs as the same then you could use the same channel.
- First connect the circuit as shown. GPIO 4 and GPIO5 will be the acting PWM pins.
- Upload the Code given below.
- Now you can change the code with separate channels if you want to control the PWM of the LEDs at a different duty cycle.
const int ledPin1 = 2; // 2 is the PWM GPIO pin
const int ledPin2 = 4; // 2 is the PWM GPIO pin
// setting PWM properties
const int freq = 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
ledcAttachPin(ledPin1, ledChannel);
ledcAttachPin(ledPin2, ledChannel); //can change the chanel here to set a new duty cycle
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
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);
}
}
Conclusion
You have now learnt how to provide PWM signals to the ESP32. You are set to make more projects wireless projects with the PWM technique using the ESP32. To learn more you can click here.