Read a Potentiometer With Arduino's Analog Input




Introduction: Read a Potentiometer With Arduino's Analog Input

About: Learn electronics and Arduino with Tinkercad Circuits!

Let's learn how to read a potentiometer, a type of rotating variable resistor, using Arduino's analog input! We'll connect up a simple circuit using a solderless 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 you 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 embedded here clicking Start Simulation and clicking to turn the potentiometer. 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.

You can follow along virtually using Tinkercad Circuits. You can even view this lesson from within Tinkercad (free login required)! Explore the sample circuit and build your own right next to it. Tinkercad Circuits is a free browser-based program that lets you build and simulate circuits. It's perfect for learning, teaching, and prototyping.

Step 1: Build the Circuit

Take a look at the breadboard circuit pictured. It can be useful to look at a free-wired version of this sample circuit for comparison, also pictured. Remember that the breadboard rows are connected inside, so you can plug in components and wires to make quick temporary connections. You could load up a new Tinkercad Circuits window and build your own version of this circuit along side the sample.

    Identify the potentiometer, LED, resistor, and wires connected to the Arduino.

    Drag an Arduino Uno and breadboard from the components panel to the workplane.

    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 by creating a red wire between both power buses and a black wire between both ground buses.

    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). 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 the 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.

    Step 2: 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. At the beginning of the program, set the variable sensorValue to "read analog pin" A0 (from the Input category).

    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.

    Step 3: 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 called int 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 called analogRead(); checks the state of pin A0 (which will be a whole number from 0-1023), and stores that value in the variable sensorValue.

      // 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 with delay();. But instead of a fixed pause, the number of milliseconds to wait is set to whatever sensorValue is at that same moment. So if sensorValue is 1023, the program will pause for 1023 milliseconds when delay(sensorValue); is executed. As the potentiometer is rotates and the value changes, so does the duration of each flash of the LED.

    Step 4: Analog Input Circuit Starter

    This circuit is also available as a circuit starter in Tinkercad Circuits. 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). The 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.

    Step 5: 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!

    Step 6: 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? 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).

      You can also learn more electronics skills with the free Instructables classes on Arduino, Basic Electronics, LEDs & Lighting, 3D Printing, and more.



        • Fix It! Contest

          Fix It! Contest
        • Creative Misuse Contest

          Creative Misuse Contest
        • Water Contest

          Water Contest


        Thank you so much, i finally am learning code.