Let's learn to adjust an LED's brightnessusing Arduino's analog output.
You've probably already used Arduino's digitali/o pins to send HIGH and LOW signals to an LED, but some of these pins are capable ofsimulating a signal somewhere in between on and off.
These pins are labeled on the Arduino witha tilde, or little squiggle, next to the pin number.
We'll connect an LED to one of these specialpins and compose a simple program to slowly fade the LED brighter and dimmer.
You can follow along virtually using TinkercadCircuits, or grab your electronics supplies and buildalong 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 connectto Arduino 5 volts and ground, respectively.
The LED's negative, shorter leg, called thecathode, connects to one leg of a resistor, but it doesn't matter which resistor leg youstart with.
The other resistor leg connects to ground.
The LED's positive, longer leg, called theanode, connects to Arduino pin 9, which has the symbol we've been looking for.
Click Start Simulation to watch the LED fadebrighter and dimmer.
Let me show you this simple program you cancreate using regular Arduino or the code blocks editor.
I'm going to start with a control block thatcounts.
And I'm going to count up by five for somethingI'm going to call brightness from zero to 255.
Inside this counting loop I'm going to addin an output block to set one of the special pins, pin 9, to.
and then I'm going to navigateto variables.
to that variable called brightness.
And then in control I'm just going to putin a little wait block, and wait 30 milliseconds.
Now if I just run this block, it's going tofade up and then go back to zero and fade up over and over.
If I want it to fade out, I have to createanother counting loop.
I'm going to duplicate this one, and thistime I'm going to count down, start with 255, and go down to zero.
In the text editor, you can see the Arduinocode generated by the code blocks.
This first section is a comment, and it justexplains what the program does for humans like you and me to read.
The main body of the program starts by creatinga variable called brightness and sets it equal to zero, and then inside the setup(), pin9 is initialized as an output because we want to use it to send signals to an LED, ratherthan listen as an input on that pin.
The program's loop uses two for loops to countup 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, whichchanges over the course of the for loop, as the value to write to the LED.
To program your physical Arduino Uno, copythe code from the window and paste it into an empty Arduino sketch, or click DownloadCode and open the resulting file using your Arduino software.
This circuit is also available as a starterin Tinkercad Circuits.
You can use this circuit starter anytime youwant to fade an LED, code included.
So how does all this work, exactly? By adding an oscilloscope component to theworkplane and connecting it to the LED terminals, we can observe the oscillating digital signaldriving the LED– this is a square wave.
The Arduino board is only capable of generatingdigital signals (HIGH and LOW), but analogWrite(); simulates the appearance of brightnesses betweenon and off by flashing the LED very quickly, and your eye interprets a dimmer light.
The ratio of time the LED spends on vs.
offdetermines how bright or dim the LED appears.
This is called pulse width modulation, orPWM for short.
Cameras aren't so easily tricked, however, and you can see here how the telltale PWM flickering effect is more or less noticeabledepending on the duty cycle, or ratio of on to off, of the wave.
PWM can be observed with the multimeter componentas well.
When brightness equals zero, we'll observezero volts on the meter.
When brightness is 50%, we'll observe 2.
5volts, which is half of the maximum 5 volts.
Now that you know how to fade an LED usingpulse width modulation, you're ready to try other Arduino exercises that utilize the analogWrite()function.
Multicolor RGB LEDs are a logical next thingto try, but you can also control the speed of a motor or the pitch of a sound made bya piezo buzzer.
Thanks for watching and learning about fadingan LED with Arduino and Tinkercad Circuits.
Check out the rest of our huge collectionof interactive beginner electronics tutorials, and even build circuits into your 3D designs.
See you next time!.