In the Arduino IDE Environment, Utilize ESP32 to Dim Multiple LED Channels.
by cashcc in Circuits > Arduino
50 Views, 0 Favorites, 0 Comments
In the Arduino IDE Environment, Utilize ESP32 to Dim Multiple LED Channels.
This experiment uses the PWM signal generated by ESP32 to control multiple LEDs in the Arduino IDE environment. ESP32 comes with a 16-channel LED PWM controller that can be configured to generate PWM signals with different characteristics.
Supplies
- ESP32 DEVKIT development board
- 3x 5mm LEDs
- 3x 330 ohm resistors
- breadboard
- several jumpers
Select PWM Channel
Select PWM channel from 0 to 15
Set the PWM Frequency
Set the PWM frequency, the LED frequency is better to choose 5000Hz
Select the Duty Cycle of the Signal
Its sub-scale range is 1-16bits. This project uses 8bit, so the controllable LED brightness level value is 0-255.
GPIO or GPIOs
Specify the GPIO or GPIOs where the signal appears: the function is ledcAttachPin(GPIO, channel). This function has two variables, the first GPIO is used to output the signal, and the second represents the channel that generates the signal.
Controlling LED Brightness Using PWM
Use PWM to control LED brightness, the function is ledcWrite(channel, dutycycle). Among them, the first variable generates the PWM signal and the first variable is the duty cycle
One LED Dimming Control
To light an LED and dim it, connect the LED to GPIO 16 pin.
The ESP32 DEVKIT module has 30 GPIOs, as long as they can be used as outputs, they can be used as PWM pins to connect LEDs.
const int ledPin = 16;
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
void setup(){
//The set PWM signal frequency is 5000Hz, select channel 0 to generate the signal, and the resolution of the signal is 8bits.
ledcSetup(ledChannel, freq, resolution);
// select the GPIO pin for the signal
ledcAttachPin(ledPin, ledChannel);
}
void loop(){
//Increase the PWM channel duty cycle (0 -> 255), the LED will gradually brighten
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
//Reduce the duty cycle of the PWM channel (255 -> 0), the LED will gradually dim
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
}
To adjust the LED brightness, you must use the ledcWrite() function. This function has two parameters: the channel that generates the signal, and the duty cycle. Note that the variable that generates the signal in the ledcWrite() function here is the channel, not the GPIO.
Make sure the board and COM port are selected correctly, upload the code to ESP32, and you can test the dimming effect.
Multiple LED Dimming Control
It is also possible to get the same signal from different GPIOs of the same channel, which requires connecting these GPIOs to the same channel of setup(). Next, we slightly modify the above operation to control the dimming of 3 LEDs through the PWN signal of the same channel.
code show as below:
const int ledPin = 16;
const int ledPin2 = 17;
const int ledPin3 = 5;
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
void setup(){
//The set PWM signal frequency is 5000Hz, select channel 0 to generate the signal, and the resolution of the signal is 8bits.
ledcSetup(ledChannel, freq, resolution);
//Configure multiple pins to the same channel
ledcAttachPin(ledPin, ledChannel);
ledcAttachPin(ledPin2, ledChannel);
ledcAttachPin(ledPin3, ledChannel);
}
void loop(){
//Increase the duty cycle of the PWM channel (0 -> 255), the LED will gradually become brighter
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
//Reduce the duty cycle of the PWM channel (255 -> 0), the LED will gradually dim
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
}