-
Notifications
You must be signed in to change notification settings - Fork 8
Exercise 02: Control Blue LED on GPIO2 (Glowing Light Effect)
In this exercise, we will use the same blue LED in Exercise 1, but producing glowing light effect, as the image shown below. Where the blue LED color will start from OFF and slowly glowing until it reach its maximum brightness and turn OFF again, repeatedly.
As we know, the output signal from Exercise 1 is DIGITAL OUTPUT, where the output signal is either turn ON or turn OFF, but to achieve glowing light effect, we need an output signal that has ranges of values from minimum to maximum value.
What we need is the Pulse-Width Modulation (PWM), where the signal output that has been generated by the PWM, is almost ANALOG OUTPUT, but not a true sine wave analog output. PWM is a modulation technique which generates variable-width pulses in the duty cycle of digital signal (square wave), where in average over time, it is representing analog output signal, as shown in the image below.
With PWM output signal, we can control brightness of LEDs, the speed of motors, the heat of the heating elements, the vibration of piezo elements for buzzer loudness, the direction of the servo motors and modulated audio signal.
The great news is, ESP32 have 16 congifurable independent PWM channels, which can be configured to generate PWM signal on all GPIOs, except GPIO34 to GPIO39.
Prior to hands-on programming, let's discuss about the program to generate the PWM output signal.
Usually in any official Arduino boards or any compatible Arduino boards, we will use analogWrite() function to generate PWM signal. Since ESP32's PWM channels is configurable, it has 3 functions to configure and generate the PWM signal, which are:
-
ledcSetup(_channel_, _frequency_, _resolution_)setup function for PWM controller, with 3 arguments:
- channel the number of the PWM channel, from 0 to 15.
- frequency the PWM signal frequency, for LED is 5 kHz.
- resolution the PWM signal resolution, from 1 bit to 16 bits, for the LED we will use 8 bits resolution.
-
ledcAttachPin(_gpio_, _channel_)function to declare LED's GPIO number and the PWM channel, with 2 arguments:
- gpio the GPIO number for output of the PWM signal.
- channel the number of the PWM channel.
-
ledcWrite(_channel_, _dutycycle_)function to generate PWM signal outputs, with 2 arguments:
- channel the number of the PWM channel.
- dutycycle the PWM duty cycle value. For 8 bits resolutions, the value range from 0 - 255.
Again, to be remember, the blue LED circuit on Hibiscus Sense is active-low, so we will program the LED for glowing effect as follows:
Complete Sketch
void setup() {
// configure PWM controller congfiguration
ledcSetup(0, 5000, 8);
// declare the GPIO number for PWM signal output
ledcAttachPin(2, 0);
}
void loop() {
// function for() to create decremental value by 1 start from 255 --> 0
// from OFF LED to linear increasing brightness, for active-low circuit
for(int brightness = 255; brightness >= 0; brightness--){
// ledcWrite() function will generate PWM output signal according to variable brightness value
// 1st argument: PWM channel number.
// 2nd argument: Tone frequency.
ledcWrite(0, brightness);
delay(15);
}
// wait for 0.2 seconds before start again
delay(200);
}Detail Sketch Explanations
In the void setup() function, there are two functions to be program, first to configure the PWM channelf using ledcSetup() function, with PWM channel 0, PWM frequency 5 kHz and 8 bits PWM resolution.
ledcSetup(0, 5000, 8);In the void setup() function, we also declare which GPIO to deliver the output of the PWM signal using ledcAttachPin(); function, with GPIO number 2 where the blue LED is interfaced and PWM channel 0.
ledcAttachPin(2, 0);Both function to configure the PWM channel and to declare GPIO for PWM output signal has been done. Now we can generate the PWM signal by using ledcWrite() function inside the void loop to control the brightness of the blue LED to produce glowing blue LED effect. Since the blue LED circuit is active-low, the PWM value (from 255-0) needs to be automatically decremental 1 by 1 by, from 255 to 254, from 254 to 253, from 253 to 252 and so on until the value reach minimum PWM value, 0. Therefore, for() function is used to automatically generate decremental variable of PWM value from 255-0 as follows, where the ledcWrite() function is inside for() function:
for(int brightness = 255; brightness >= 0; brightness--){
ledcWrite(0, brightness);
delay(15);
}Now, we can upload the complete sketch to ESP32, then observe the output. By observatoin, the output of the blue LED is repeated glowing light effect, which the results from decremental value of the PWM signal output.


