Using a Photo Resistor
Try reloading this page, or reviewing your browser settings
This segment serves as the introduction to using analog inputs with Arduino using a potentiometer to control an LED.
About this video
- Liz Clark
- First online
- 09 April 2019
- Online ISBN
- Copyright information
- © Liz Clark 2019
In this section, we’ll go over true analog control of Arduino using potentiometers. To follow along, you’ll need the supplies we’ve used so far, shown here, as well as at least one potentiometers.
Some potentiometers aren’t breadboard compatible, so be sure to either use one specifically made for breadboarding, or one with wires soldered into the connections. Let’s get started.
So far, we’ve been working with the digital side of things with Arduino. Arduino boards can also handle purely analog signals as well. But what is an analog signal, exactly? We discussed this a bit in the PWM section, but an analog signal allows for values in between the binary states of on and off the digital signals provide. Analog signals are actually voltage readings, and that’s why their value can have such a range.
A real world example of analog control is when you adjust the volume knob on a sound mixer. The mixer’s volume is adjusted in increments, rather than having the sound being just on or off. And by using a knob, you can adjust the levels precisely to what is needed. This knob is actually a potentiometer, and we’ll be using potentiometers in this section to demonstrate analog control.
The Arduino boards are able to handle analog signal natively because they have an analog to digital converter, or ADC, as part of their circuit design. This means that the voltage reading that is coming in or out is converted to a digital value so that it can be understood by the Arduino’s processor and code.
In the case of Arduino, this value range is expressed as 0 through 1023, with 0 being no signal or off, and 1023 being the highest possible signal, or all the way on– similar to how PWM values are expressed with 0 through 255.
The analog pins on the Arduino are located on the other side of the board across from the digital pins, and are denoted with an A before the pin number. Relating this all back to controlling LEDs, using an Arduino for analog control presents some great opportunities for LED projects. You’ll have more precise control over an LED’s brightness– or even color, as we’ll explore in a later section– and it presents some unique ways to visualize data with different sensors.
Now that we have a better understanding of working with analog values with Arduino, let’s control an LED with an analog potentiometer. First, let’s set up our breadboard. We’ll keep our ground and 5 volt rails from our previous segment. And as we’ve done in the past few segments, connect an LED with a resistor to pin 9.
One caveat about analog control of Arduino is that the component receiving analog value will either need to be connected to one of the analog pins or digital pin with PWM enabled. This allows for the proper conversion of analog to digital data. Then, we’ll connect our potentiometer.
Potentiometers have three pins. Going left to right, the first is connected to ground, the middle pin is connected to data– in our case, analog pin 0– and the third pin is connected to 5 volts. We can use jumper wire to connect the potentiometer to the ground and power rails, and then an additional piece to A0 as shown.
For this section, we won’t be utilizing a piece of example code that’s found in the Arduino IDE. This code, though, is also available on the Arduino website, Arduino.cc.
First, we’ll declare our pins as integers. We’re going to call the LED LED and the potentiometer pot. Enter integer LED equals 9 and integer pot equals A0.
Next in the setup, we’re going to declare the LED as pin as an output by entering pin mode LED output. This then brings us to the loop.
We’re first going to create a new integer named val in the loop to store the value of the potentiometer by entering integer val equals analog read pot. And then we’re going to map the analog values that will be received by the potentiometer to digital PWM values that can be accepted by the LED connected to pin 9.
This is done with the line val equals map val 0 1023 0, 255, where the 0 to 1023 analog range will be converted to the PWM 0 of the 255 range, and be applied to the val integer that will be carrying the analog data.
Then we’ll have the LED receive the potentiometer’s data with analog right, LED val. Analog right will always look for two integers similar to digital right, which we’ve used in previous segments.
Now we can compile and upload our code. You should be able to turn the potentiometer up and down and see the LED’s brightness be affected. Similar to how we use multiple buttons to control multiple LEDs, we can do the same thing with potentiometers. Simply add more LEDs and potentiometers to your breadboard and then the code, set up the pins to reflect how your circuit is set up.
You also need to create multiple value integers to hold each potentiometer’s value. Here, we’re creating val 1 and val 2 to correspond with pot 1 and pot 2. In the loop, you’ll basically insert two iterations of our original code, but with our new integers as shown here. After you compile and upload, you should be able to use both potentiometers to control each LED.
We’ve gone over printing data to the serial monitor, and we can do that with analog values as well. This technique can be especially helpful of analog values, since there’s such a wide range that you may want to be checking for.
First, we’ll open up serial communication and set up a portion of the code by entering serial dot begin 9600, as we have in previous segments. Then in the loop, we’ll print the value of the potentiometer by adding serial dot print ln val.
Now, compile and upload the code. And after that, we can open the serial monitor by going to Tools, Serial Monitor. As you turn the potentiometer, you should see the LED react, as well as the analog values appear in the monitor.
The values are appearing very fast, since we don’t have a delay in the loop. The delay will cause the analog control of the LED to slow down as well, and cause it to look a bit choppy.
If you ever needed to see the values in the monitor at a slower rate though, you could always add the delay, just knowing that the LED’s animation will be affected as well.