Let's learn how to read a variable resistor,
in this case a potentiometer using Arduino’s
analog input.
We'll create a circuit and compose a simple
program to control the rate of a flashing
LED based on the position of a knob we can
turn.
You can follow along virtually using Tinkercad
Circuits,
or optionally grab your Arduino Uno board
and a USB cable, along with a breadboard,
any value potentiometer and an LED, as well
as a resistor and breadboard wires.
We’ll build upon the basic circuit for lighting
up an LED connected to pin 13.
If that’s new to you, follow the link to
check out that introductory lesson first.
In the previous lesson, we learned to detect
a pushbutton's state, on or off, with digital
input.
In this lesson, we'll move to the opposite
side of the Arduino board to the analog inputs,
which sense the gradually changing electrical
signal from turning the potentiometer.
The Arduino’s analog to digital converter,
or ADC, converts an incoming analog signal
between 0V and 5V into a range of numbers
from 0-1023.
Click Start Simulation to start up the sample
program, and click and drag to rotate the
knob.
You can also just click around the potentiometer
to move the knob to that position.
Try it for yourself in Tinkercad Circuits!
Add a potentiometer to the basic LED circuit
and wire it up the outer pins to power and
ground, and the center pin to Arduino pin
A0.
Since you may just getting the hang of the
breadboard, here’s the equivalent free-wired
version of this circuit for comparison.
You can build either way in the Circuits editor,
but if you are also building a circuit with
physical components, the breadboard will help
your virtual circuit look the same.
Let’s compose some simple code to read the
changing value of the potentiometer by opening
up the code blocks editor.
Create a new variable named sensorValue, then
drag out a set block.
In the input category, grab a blocked called
“read analog pin” and place it inside
the “set” block.
Click the Output category and drag out block
to set the built-in LED to HIGH, or on.
Then click on the Control category and drag
out a wait block, and navigate to the Variables
category to grab sensorValue and place it
inside the wait block.
Make sure the drop-down menu is set to milliseconds.
Duplicate this block and sandwich them around
another output block to set the built-in LED
low.
So this section turns the LED on, waits between
0 and 1023 milliseconds, turns the LED off,
and waits the same time again.
In the text editor, you can see the Arduino
code generated by the code blocks.
After creating our variable sensorValue, inside
the setup we use the pinMode() function to
establish analog pin A0 as an input, and digital
pin 13 as an output.
The code inside the loop uses a new function
called analogRead() to listen to the pin’s
state.
Interspersed are comment lines marked with
double slashes, which are just notes to help
clarify what the program is doing.
They aren’t actually included in the final
program when it runs on the Arduino.
The program uses a familiar function digitalWrite()
and then another one you might know well by
now, delay().
But instead of pausing for a fixed amount
of time, the value passed to the delay() function
will change as you turn the knob because each
time through the loop it’s going to read
the position again.
So the LED flashes at a rate of somewhere
between 0 and 1023 milliseconds, or just about
a second or less.
The basic pushbutton circuit and code is available
as a circuit starter.
Use it anytime you want to read a potentiometer
or other kind of variable resistor with analog
input.
Find Arduino starters in the component panel
using the dropdown menu.
You can wire up your your physical Arduino
Uno the same way as it’s pictured in Tinkercad
Circuits, then copy the code from the window
and paste it into an empty Arduino sketch,
or click Download Code and open and upload
the resulting file using your Arduino software.
Now that you know how to detect analog inputs,
mastering the serial monitor is a tremendously
useful next step.
Combining analog inputs and the serial monitor
will let you detect light with a photoresistor,
and use a temperature sensor or ultrasonic
distance sensor.
Thanks for watching and learning with 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!
