0:00
Imagine if your lights slowly increase in brightness when you turn them on and slowly decrease in brightness when you turn them off
0:08
This kind of effect can easily be done with Arduino. With the fading LED effect, you can have thousands of creative uses, whether you want to light up your car, truck, boat, house, appliances, or furniture
0:30
In this video, we will do project 7 of the Arduino intro app which is the fade in fade out project
0:39
You will need one LED and one 220 ohm resistor. I will also explain how to use the 4 loop in
0:47
Arduino. Let's get started. First, let's take a look at the breadboard diagram. From here
0:54
you will see that it is basically the same with our blinking LED circuit. However, there's one
1:02
very important thing to consider here. If we want to be able to vary the brightness level of our LED
1:09
then we need to connect the LED to a PWM pin. PWM stands for Pulse with Modulation. PWM is a
1:19
technique for getting an og-like behavior from a digital output by switching it off and on
1:26
very fast and with different ratios between on and off time. With this technique, we can control
1:34
the brightness of an LED or the speed of a motor. In the Arduino Uno, not all digital pins have PWM
1:43
capability. Only digital pins 3, 5, 6, 9, 10, and 11 are capable of PWM. If you connect your LED to
1:53
any of these pins, then you can make a fade effect. In some boards, these pins are labeled as such
2:00
Now, for the code, what we want to do here is just for the LED to start fading in as soon as
2:08
the board is powered up. And when it has reached its maximum brightness, it will start to fade out
2:16
And again, this will go in a loop. Fade in, fade out, fade in, fade out. This will be our first
2:24
attempt to make a fade effect. To start, we'll declare a variable named LED pin for digital pin
2:31
Next, inside the setup function we simply set that pin as an output pin. What's different
2:40
this time is in the loop function. The first thing we need to know is the og write
2:47
function The og write writes an og value or PWM wave to a digital pin The value can be anywhere from 0 to 255 og write can be used
3:01
to light up an LED at varying brightness or drive a motor at various speeds. After a call to og
3:10
write, the pin will generate a steady rectangular wave of the specified duty cycle until the next
3:18
called to og write function. In fading an LED, the og value of zero effectively means that
3:26
the LED is off or the pin is always off, while the og value of 255 sets the pin as always on
3:34
with 5 volts, thus giving the LED its maximum brightness. If we write an og value of about
3:42
125, then we can say that the LED will have half of its maximum brightness because the pin is
3:49
cycling from on half of the time to off the other half, thus giving the illusion of half brightness
3:57
Now that we know how to control the brightness of an LED, let's study the concept behind the fade-in
4:05
effect. What you want to happen first is to set the brightness of the LED to 0. We can achieve
4:14
this by writing og write lead pin comma 0. Assuming our variable is lead pin. Then gradually
4:23
after a set amount of time, we slowly increase the brightness. For example, after 30 milliseconds
4:31
we write og write lead pin comma 5. It doesn't have to be 5. You decide on how much
4:37
brightness you want to add. Then after another 30 milliseconds, you write again og write
4:45
lead pin comma 10. This goes on until you reach the maximum PWM value of 255. You can see it from
4:55
our modified code. To create a fade out effect, we just do the opposite. We start first at full
5:03
brightness. So we write og write lead pin comma 255. Then after 30 milliseconds, we reduce
5:11
the PWM value by 5. Thus we type og write lead pin comma 250. We write og write again
5:20
and again until the PWM value is 0. Now, obviously, writing this code takes so much time and energy
5:29
but notice that there are some parts that are just repeated. In fact, every time we write the og write function
5:38
we just need to change the PWM value. Now, this is a perfect situation to use a loop
5:46
By using a for loop we can actually shorten our code Loops can execute a block of code a number of times Loops are handy if you want to run the same code over and over again each time with a different
6:04
value. There are many types of loops in Arduino, but for this code, we will use the for loop
6:13
The for loop has the following syntax. Statement 1 is executed once before the execution of the code block
6:23
Statement 2 defines the condition for executing the code block. Statement 3 is executed or run after the code block has been executed
6:36
However, the condition in statement 2 has to be true. in order for statement 3 to be executed
6:45
The loop will stop only when the condition becomes false. In this case, the program proceeds to the next statement after the for loop
6:56
Now, let's take a look at our revised code. Now, this time, using the for loop
7:05
The first statement inside the main loop function is actually a for loop
7:10
Statement 1 inside the for loop parenthesis says int fade value equal to 0
7:18
Normally, you will use statement 1 to initialize the variable used in the loop
7:23
In this case, we initialize the fade value variable to 0. There are other ways to do this
7:32
In fact, statement 1 is optional, but it's good to start with the basics
7:36
Statement 2 is used to evaluate the condition of the initial variable
7:41
It has to be written in such a way that it returns a true or false answer
7:46
Think of it as a question. For example, is fade value less than or equal to 255
7:53
If statement 2 returns true, the loop will start, meaning the statements inside the for loop will be executed
8:02
If it returns false, the loop will end. Often, statement 3 increments the value of the initial variable
8:11
This is not always the case. Statement 3 can do anything like increase or decrease the value of the initial variable
8:18
by a certain number or anything else. In this case, we add 5 to the current value of the fade value variable
8:27
So we write fade value equal to fade value plus 5. The way of saying this is that the new value of the fadeValue variable is equal to the current
8:40
value of the fadeValue plus 5 The first statement inside the for loop which is written after the open brace is og write led pin comma fade value Note that we did not type in a number as our og value Instead we used the variable
9:01
fade value. Why is this so? Well, when this code will be executed, Arduino will simply replace
9:08
fade value with the actual value of the fade value variable. Remember that every time the for loop
9:16
is executed, we add 5 to the current value of the fade value variable. So if in the previous run
9:24
the value of fade value is 10, then in the next run, the value of fade value becomes 15
9:31
With this technique, we will only write the og write function only once inside the for loop
9:40
A delay is written just before the for loop ends. This is to provide a transition time for the LED to change its brightness
9:51
It also affects how smooth the fade-in effect will happen. You can experiment with this value to see which one works for your project
10:03
After the last statement inside the for loop, which is delay 30, the loop goes back again from the beginning
10:13
But just before the first statement is once again executed, it checks first if the answer to statement 2, which is our condition statement, is true
10:23
If it is, then the loop proceeds. If it is false, then the loop stops and the next statement inside the main loop function is executed
10:35
This is after the close brace of the for loop. In this case, if the value of the fade value variable is greater than 255, then statement 2 becomes false and this loop will end
10:49
The next statement after the first for loop is another for loop, but this time it is the opposite
10:56
This for loop creates a fade out effect. The fade value starts at 255, meaning the LED is at full brightness
11:06
Then as the loop runs, its value decreases until it becomes a negative value
11:12
When this happens, statement 2 becomes false, thus ending the loop. However, the main loop function will still run
11:23
Learning how to use the for loop can be confusing at first, but once you get the idea, then everything
11:29
becomes clear. You should use loops whenever possible to save on time and energy
11:36
The best way to understand the for loop is to try it
11:40
So go ahead and try this project if you haven't done so already