Potentiometer (Analog Input)
Let's learn how to read a potentiometer, a type of rotating variable resistor, using Arduino's analog input! We'll connect up a circuit using a breadboard and use some simple Arduino code to control a single LED.
So far you've learned to control LEDs with Arduino's output, and learned to detect a pushbutton's state (on or off) with digital input. In this lesson, we'll sense the gradually changing electrical signal from turning the potentiometer with Arduino's analog inputs, located on the opposite side of the board from the digital i/o (input/output) pins. These special analog pins are connected to the Arduino's analog to digital converter (ADC), which converts an incoming analog signal between 0V and 5V into a range of numbers from 0-1023 (zero counts as a value).
Explore the sample circuit here in the workplane by starting the simulation and turning the potentiometer. In this lesson, you'll build this circuit yourself along side the sample. To optionally build the physical circuit, gather up your Arduino Uno board, USB cable, solderless breadboard, an LED, resistor (any value from 100-1K), potentiometer, and breadboard wires.
- Continue to the next step.
Build the Circuit
Take a look at the breadboard circuit in the workplane. It can be useful to look at a free-wired version of this sample circuit for comparison, pictured. In this step, you will build your own version of this circuit along side the sample in the workplane.
- Identify the potentiometer, LED, resistor, and wires connected to the Arduino in the Tinkercad Circuits workplane.
- Drag an Arduino Uno and breadboard from the components panel to the workplane, next to the existing circuit.
- Connect breadboard power (+) and ground (-) rails to Arduino 5V and ground (GND), respectively, by clicking to create wires.
- Extend power and ground rails to their respective buses on the opposite edge of the breadboard (optional for this circuit but good common practice).
- Plug the LED into two different breadboard rows so that the cathode (negative, shorter leg) connects to one leg of a resistor (anywhere from 100-1K ohms is fine).
HINT:
The resistor can go in either orientation because resistors aren't polarized, unlike LEDs, which must be connected in a certain way to function.
- Connect other resistor leg to ground.
- Wire up the LED anode (positive, longer leg) to Arduino pin 13.
- Drag a potentiometer from the components panel to your breadboard, so its legs plug into three different rows.
- Click to create a wire connecting one outer potentiometer leg to power.
- Connect the center leg to Arduino analog pin A0.
- Create a wire connecting the other outer leg to ground.
- Continue to the next step.
Code With Blocks
Let's use the code blocks editor to listen to the state of the potentiometer, then flash an LED at a rate related to the variable resistance of the potentiometer.
- Click the "Code" button to open the code editor. The grey Notation blocks are comments for making note of what you intend for your code to do, but this text isn't executed as part of the program.
- Click on the Variables category in the code editor.
- To store the resistance value of the potentiometer, create a variable named "sensorValue".
- Drag out a "set" block.
- We'll store the state of our potentiometer in the variable
sensorValue
. - Click on the Input category and drag out an "analog read pin" block, and place it into the "set" block after the word "to"
- Since our potentiometer is connected to the Arduino on pin A0, change the dropdown to A0.
- Click the Output category and drag out the first block to set the built-in LED
HIGH
. - Click the Control category and drag out a wait block, then navigate back to Variables and drag
sensorValue
onto the wait block, and adjust the dropdown menu to milliseconds. - Continue to the next step.
Potentiometer Arduino Code Explained
When the code editor is open, you can click the dropdown menu on the left and select "Blocks + Text" to reveal the Arduino code generated by the code blocks. Follow along as we explore the code in more detail.
-
int sensorValue = 0;
Before the
setup()
, we create a variable to store the current value read from the potentiometer. It’s calledint
because it’s an integer, or any whole number.void setup() { pinMode(A0, INPUT); pinMode(13, OUTPUT); }
Inside the setup, pins are configured using the
pinMode()
function. Pin A0 is configured as an input, so we can "listen" to the electrical state of the potentiometer. Pin 13 is configured as an output to control the LED. -
void loop() { // read the value from the sensor sensorValue = analogRead(A0);
Anything after a set of slashes
//
is a comment, which helps folks understand in plain language what the program is intended to do, but is not included in the program your Arduino runs. In the main loop, a function calledanalogRead();
checks the state of pin A0 (which will be a whole number from 0-1023), and stores that value in the variablesensorValue
. -
// turn the LED on digitalWrite(13, HIGH); // pause the program for <sensorValue> millseconds delay(sensorValue); // Wait for sensorValue millisecond(s) // turn the LED off digitalWrite(13, LOW); // pause the program for <sensorValue> millseconds delay(sensorValue); // Wait for sensorValue millisecond(s) }
Up next is some familiar code if you started out blinking LEDs! A function called
digitalWrite();
sets the LED on (HIGH) and off (LOW), separated by pauses withdelay();
. But instead of a fixed pause, the number of milliseconds to wait is set to whateversensorValue
is at that same moment. So ifsensorValue
is 1023, the program will pause for 1023 milliseconds whendelay(sensorValue);
is executed. As the potentiometer is rotates and the value changes, so does the duration of each flash of the LED. - Continue to the next step.
Analog Input Circuit Starter
This circuit is also available as a circuit starter. You can use this circuit starter anytime you want to read a potentiometer or other kind of variable resistor/analog input.
- Grab this circuit and code combo any time using the starter available in the components panel (dropdown menu -> Starters -> Arduino).
- If you added the circuit starter to the existing breadboard circuit workplane, delete the duplicate Arduino board and components by clicking on them (or shift+drag to select within a box) and then clicking the trash icon (you can also press delete on your keyboard).
- Continue to the next step.
HINT:
This circuit starter has the same code as the sample circuit for this lesson, but lacks a breadboard and relies on the Uno's internal LED wired to pin 13 instead of an additional LED.
Build a Physical Arduino Circuit (Optional)
To program your physical Arduino Uno, you'll need to install the free software (or plugin for the web editor), then open it up.
- Wire up the Arduino Uno circuit by plugging in components and wires to match the connections shown here in Tinkercad Circuits. For a more in-depth walk-through on working with your physical Arduino Uno board, check out the free Instructables Arduino class (a similar circuit is described in the third lesson).
- Copy the code from the Tinkercad Circuits code window and paste it into an empty sketch in your Arduino software, or click the download button (downward facing arrow) and open the resulting file using Arduino.You can also find this example in the Arduino software by navigating to File -> Examples -> 03.Analog -> AnalogInput.
- Plug in your USB cable and select your board and port in the software’s Tools menu.
- Upload the code and turn the knob to adjust the flashing rate of the LED!
- Continue to the next step.
Next, Try...
Now that you’ve learned to read a potentiometer, you're ready to link up those incoming values with other skills you've learned so far.
- Instead of using the
sensorValue
to affect timing, can you figure out a way to make it affect the LED's brightness instead?HINT:
Look at the circuit pictured, and change your LED from pin 13 to pin 9 as shown, then find the example code in your Arduino software by navigating to File -> Examples -> 03.Analog -> AnalogInOutSerial.
- Continue on to the next lesson to learn how to monitor your Arduino's digital and analog inputs through the computer using the Serial Monitor.
- Try swapping out your potentiometer for other analog inputs such as an ultrasonic distance sensor or photoresistor (light sensor).