Let's learn to adjust an LED's brightness
using Arduino's analog output. You've probably already used Arduino's digital
i/o pins to send HIGH and LOW signals to an LED, but some of these pins are capable of
simulating a signal somewhere in between on and off. These pins are labeled on the Arduino with
a tilde, or little squiggle, next to the pin number. We'll connect an LED to one of these special
pins and compose a simple program to slowly fade the LED brighter and dimmer. You can follow along virtually using Tinkercad
Circuits, or grab your electronics supplies and build
along with a physical Arduino Uno, breadboard, LED, resistor, and some breadboard wires. Take a look at the circuit in the workplane. The breadboard power and ground rails connect
to Arduino 5 volts and ground, respectively. The LED's negative, shorter leg, called the
cathode, connects to one leg of a resistor, but it doesn't matter which resistor leg you
start with. The other resistor leg connects to ground. The LED's positive, longer leg, called the
anode, connects to Arduino pin 9, which has the symbol we've been looking for. Click Start Simulation to watch the LED fade
brighter and dimmer. Let me show you this simple program you can
create using regular Arduino or the code blocks editor. I'm going to start with a control block that
counts. And I'm going to count up by five for something
I'm going to call brightness from zero to 255. Inside this counting loop I'm going to add
in an output block to set one of the special pins, pin 9, to... and then I'm going to navigate
to variables... to that variable called brightness. And then in control I'm just going to put
in a little wait block, and wait 30 milliseconds. Now if I just run this block, it's going to
fade up and then go back to zero and fade up over and over. If I want it to fade out, I have to create
another counting loop. I'm going to duplicate this one, and this
time I'm going to count down, start with 255, and go down to zero. In the text editor, you can see the Arduino
code generated by the code blocks. This first section is a comment, and it just
explains what the program does for humans like you and me to read. The main body of the program starts by creating
a variable called brightness and sets it equal to zero, and then inside the setup(), pin
9 is initialized as an output because we want to use it to send signals to an LED, rather
than listen as an input on that pin. The program's loop uses two for loops to count
up from 0 to 255 by increments of 5. The analogWrite() function takes two arguments:
the first is a pin number, which is 9 in our case, and the second is a value between 0,
or off, and 255, or all the way on. We're using the variable brightness, which
changes over the course of the for loop, as the value to write to the LED. To program your physical Arduino Uno, copy
the code from the window and paste it into an empty Arduino sketch, or click Download
Code and open the resulting file using your Arduino software. This circuit is also available as a starter
in Tinkercad Circuits. You can use this circuit starter anytime you
want to fade an LED, code included. So how does all this work, exactly? By adding an oscilloscope component to the
workplane and connecting it to the LED terminals, we can observe the oscillating digital signal
driving the LED-- this is a square wave. The Arduino board is only capable of generating
digital signals (HIGH and LOW), but analogWrite(); simulates the appearance of brightnesses between
on and off by flashing the LED very quickly, and your eye interprets a dimmer light. The ratio of time the LED spends on vs. off
determines how bright or dim the LED appears. This is called pulse width modulation, or
PWM for short. Cameras aren't so easily tricked, however,
and you can see here how the telltale PWM flickering effect is more or less noticeable
depending on the duty cycle, or ratio of on to off, of the wave. PWM can be observed with the multimeter component
as well. When brightness equals zero, we'll observe
zero volts on the meter. When brightness is 50%, we'll observe 2.5
volts, which is half of the maximum 5 volts. Now that you know how to fade an LED using
pulse width modulation, you're ready to try other Arduino exercises that utilize the analogWrite()
function. Multicolor RGB LEDs are a logical next thing
to try, but you can also control the speed of a motor or the pitch of a sound made by
a piezo buzzer. Thanks for watching and learning about fading
an LED with Arduino and Tinkercad Circuits. Check out the rest of our huge collection
of interactive beginner electronics tutorials, and even build circuits into your 3D designs. See you next time!