Hi. in this tutorial we are going to learn how to control the brightness
of an LED faded in or faded out using SunFounder esp32 module with the extension
board esp32 starter kit from SunFounder this is the best esp32 learning kit from SunFounder it
has this esp32 microcontroller which has 12 and Wi-Fi and Bluetooth this board can do everything
Arduino Uno can do or many other Arduinos can do plus extra more features because we have Wi-Fi and
Bluetooth the possibilities are endless you can get connected to the cloud or do the control or
read information or values via your mobile device or your desktop or over the cloud from a far
location it comes with a camera extension kit where you can stream the video over
Wi-Fi either through the cloud or to your network and also it comes with micro SD
card where you can save images on the micro SD card or you can write from the device any
information log the information on the device and read it back you can power the board using
this included 18650 lithium battery and it has built-in charger where you can connect micro USB
and charge the battery the kit comes with 320 pieces of component that you can learn tons of
projects to the home page let's see the documentation page click here for or user
and 2.1 hello LED this is a documentation page it has explanation and wiring diagram and an actual
wiring and at the bottom there is also to code if you are using Cloud you can connect it via your
browser generally Digital Signal looks like this these are all times each line and when
the signal goes let's say 5 volts or 3.3 volts the voltage doesn't matter so it goes to that
value or it come and it comes back to zero and then it has now two second off or zero and then
it becomes high and then low so this area that the voltage is maximum is called high and the
area that is zero is called Low so this is a digital signal and this Digital Signal will
have something called pulse width modulation and this signal the width of this is 20% of
the signal because the signal starts from here to here that's one cycle so this is 20% and this is
60% because the high time is larger so the high time is longer than the small time and 20% the
time that it was high was very shorter 1 so this was 20% and this is a 20% duty cycle signal the
on time or high time and low time are the same the high time and low time are the same and here
this is a 20% duty cycle and if you compare the three signal together so this is a 10% duty cycle
duty cycle is the time that it is on and this is 30% and this is 50% so 60% duty cycle mean 3/5th
of a signal and so 60% duty cycle means 35 of a signal and 20% duty cycle means 1/ 15if of the
signal time the signal is high and 100% duty cycle means full signal always high and this
is an example of 100% duty cycle when the signal is 5 volts for UNI and for esp32 it will be 3.3V.
volts and here is the wiring we are looking at the diagram uh we have already ex we have
already explained this in a previous lesson we have a 220 Ohm resistor is connected to pin 26
the other side of resistor is connected to anode of LED and the cathode of LED is connected to
the ground and here on the breadboard this side is uh anode which is connected to resistor and
from this point it is connected so let's do that here the here is my LED and this is the long pin
on this side I'm just inserting it somewhere on the breadboard like that and then this is a 220
Ohm resistor from the package get one and one side will be connected to the same pin as the
LED and the other side somewhere on the board black wire from the left pin connected it goes
to this point where later on this becomes ground and it is connected to ground or GND but we can
connect it directly from g& D from here so that is ready and now this side will be connected to
where you connected the resistor so the blue is connected somewhere in the same line as the
resistor there is my board now to make sure that wiring is correct connect the ground to
the ground the second pin is ground from this side of USB and this one is we will connect it
to pin 3.3 volts the last pin if the wiring is correct your LED should turn on now according to
this diagram we connect it to pin 26 and here I've connected it to pin 26 as shown in the
diagram
now let's see how we can Define functions so we can phase an LED the first function is called
LED C that refers to language C setup and then it has opening closing parenthesis, and it has three
parameters each parameter is separated with comma and the first one is password
modulation channel on esp3 2 and this is the frequency and the last one is the
resolution uh for example we can set the par modulation channel one and the frequency at
5,000 Hz and the resolution in terms of bit it will be eight on esp32 it's from 1 to 20
bits then we need to attach a pin so we use ledcAttachPin it has two parameter now separated
with comma and here we will enter the GPI open and the last one is the Pulse Width modulation
channel so we say this pin should act as pulse modulation for example we can enter 26 pin 26
to be Pulse Width modulation channel one so this will be done in the setup and then for
the action that we do we will re write ledcWrite so we are writing the value to the pin and it has
two parameters separated with comma the first one is the GPI open the second one is a duty
value from 0 to 255 for example we can enter 25 and this could be for example 175 and this
pin 26 will go around 50% of the brightness uh and this 125 is like 50% I believe it's
half of 255 so it will be half of brightness of an LED is Set uh with pin 26 so for summary
we first need to do ledcSetup uh channel one par modulation 5,000 HZ with 8bit resolution
then we have to attach a pin so we are attaching pin 26 to channel one this was the same channel
that we defined in the setup and then we can use the pin using ledcWrite like pin 26 at 125
value open the project by clicking on file open go to download esp32
starter CIT Main click on C folder and then go to codes and then 2.2 is
fading LED double click to open it and this is a file click and click
open this is the same as the previous lesson we are defining pin 26 as led pin with the
type integer and set it to as constant so it cannot change this variable is holding
the brightness of LED initial value is zero with the type integer and then the fade amount
because we have from 0 to 255 we can go with the steps of five if we want so this is for the
step we use we use here the ledcSetup this is the pin or P modulation Channel Zero with 5
KZ frequency and the this is the bit rate eight bits usually we do not change these two we just
change the channel which channel we want and then now for this channel we have to attach
it to the LED pin then we say ledc this line in this line we are attaching the Channel Zero
this channel which is zero to this pin so this means the same pin this is a if I type here 26
but because we are using it multiple time we are using the variable and this is attaching the pin
this runs once and here we say ledc this is a c language right Channel Zero with the amount of
brightness which is zero so this will turn the LED with a minimum amount of brightness then we set
the brightness by getting the brightness plus the fading so initially it was 0 + 5 now this
will be five because this is a loop now five plus this fade amount here plus the brightness which
is zero so now this is zero and here we check if the brightness is smaller than zero this is
comparing it less than or equal zero so this is one condition and these two vertical line means
or if the brightness is zero or less than zero or brightness is greater than or equal 255 if this
is out of bound it means this means less than zero and that means less more than 255 then we
say good in Reverse how we say get the amount and set it to negative so now this will be negative
and when it comes next time here uh next time here it will be reduced so this is just taking
care of the bound making sure it never goes above to 55 and Below uh zero and here the delay is 50
millisecond and the code reaches here and comes back and sets the value of the brightness but pay
attention now we are we have added that now this is five in the previous step it was five now we
get five plus five now this time is becomes 10 and it comes back and sets it at 10 and it becomes uh
15 20 25 30 up to 255 when it reaches above 255 then it goes in reverse and it becomes from 255
it becomes uh 250 245 to uh uh 40 and 235 and so forth until it becomes zero and it goes back and
up so up and down up and down with the delay of 50 millisecond we will see the brightness is changing
my esp32 is already connected to computer and make sure the board is selected if not
click on tool board and come to esp32 and select esp32 Dev module after that we have
to select the port uh right click on the uh start menu go to device manager let's
see what port it is and here under the port we I've shown you before now again mine shows
Port eight yours might be different whatever it is just remember it because if you connect
multiple devices you will have different numbers and here click on tool and then
comport if see multiple of this select the one that you saw under the device manager so it is
selected and now everything is ready let's just upload the
code first it compile set and then send it to the downloads set to the
esp32 the code have been uploaded and
here as you can see it's fading increasing and
decreasing from the top you will not see it much or maybe you can see it
yeah but here maybe a little better so this is the amount if you want this to
go very fast you can reduce it let's say make it 20 and let's upload the
code when this is finished you will see 100 per here this will take effect still the old code is
running and now the code has changed it is now going faster and if you want to make it very
slower just increase this time and it will do the same thing in a longer period of time the
other way to change the brightness step faster is the amount of fade uh this is written five
if I make this 10 and upload the code you will see that this will fade very faster twice as
fast wait until it's 100% And now as you can see this is fading so fast we cannot even see
it perhaps five is better maybe even reduce it uh it it's up to
you