Arduino Basics - Fade


Today we will go through the Fade program that is found in the Arduino's IDE at File - Examples - Basics. The Components we will need for this experiment are Arduino Uno board, a breadboard, a led, a 220 ohms resistor, and two connecting wires. I have used a yellow and a black wire.



click to enlarge
Connect the led on the breadboard, connect the 220 ohms resistor to any of it's legs. That is ok for this experiment. I have connected the resistor to the shorter leg/ negative/ cathode of the led. Connect the other end of the resistor with a black wire, attach the other end of the black wire to any of the Gnd pins on the Arduino board.

Connect the yellow wire to the longer leg/ positive/ anode of the led. Connect the other end of the yellow wire to digital pin number 9 on the Arduino's board. That;s all.

Now connect the Arduino board with your computer with a usb cable. And open the Arduino IDE. Open the Fade program in the Examples in Basics.

Now upoad the program on the Arduino's board. Once the upload is complete you will see the led glowing and dimming in regular intervals. That's all there is in this experiment.

I have created a trimmed version of the same program shown here below.

click to enlarge
We are using analogWrite() function to send current in the led. The led is connected to the pin number 9. That is what is written in the first parameter in the pinMode() function as well as in the analogWrite() function.

The second parameter in the analogWrite() function is the value of Duty Cycle that is sent to the led through the pin number 9. is value is denoted by x. We have created an integer variable x.  This is the value of the duty cycle of the PWM (pulse width modulation) that we will send to the led. This value can be in the range of 0 to 255 in the Arduino Uno board.

When the duty cycle is  0 the led is off, when we increase the number, the pulse width starts getting bigger and led starts getting brighter. It is brightest when it is 255.  We increase the PWM duty cycle value by 5 in each iteration of the loop() function. When the PWM duty cycle value reaches above 255 we start decrementing it by -5, and when it reaches below 0 we start incrementing it again. Thus the led goes from low to high intensity and back to low intensity.

You can experiment with different values in this program. If you change the value of a to 1 then the led will slowly brighten and dim. if you make the value of a higher, then the brightening and dimming process will happen faster.

In the same way if you reduce the delay which is in milliseconds, the led will iterate faster, and if you increase the delay and it will iterate slower.