Component: Potentiometer

Objectives and Overview

This lesson introduces the potentiometer component. The potentiometer is a commonly used analog input component. Understanding how potentiometers work opens the door for several fun experiment ideas, such as being able to control LED brightness with the turn of a knob, or making a synthesizer where the knob controls the pitch!

Lesson Objectives

  • Become familiar with the potentiometer component and explain how it’s used for analog input.
  • Understand and explain how the Arduino map() function is used with the potentiometer.

The Potentiometer

In this guide, you’ll be introduced to a new component and some new programming concepts. The component that you’ll be working with is the potentiometer, sometimes known informally as a pot.

potentiometer is a resistor that supports a range of values that are typically adjusted by a knob. Volume controls on audio equipment are examples of potentiometers, so you’ve likely had experience interacting with this component.

You’ve already worked with analog output when you created the fading LED. Remember that the Arduino used PWM to rapidly turn an LED on and off, creating a fading effect. Now you’ll use analog input which produces a range of input values from 0 to 1023. The potentiometer sends this range of input to the Arduino.

You’ve worked with digital input with the pushbutton. The pushbutton used digitalRead() to determine the state of the button, which could be either HIGH or LOW. With digital input, there are only two possible states. Analog input sends a range of values.

The best way to understand this concept is with a project, so let’s get to the experiment!

Project: Dialing in the LED

The potentiometer is the only new component in this project. The one you’ll be using is a tiny plastic knob that looks like this:

Blue potentiometer component

Supplies

  • Arduino Uno and breadboard
  • Jumper wires
  • (New!) Potentiometer: The potentiometer is a knob. The one in your kit is a blue plastic knob, although other pots are sometimes metallic. Once the potentiometer is mounted, turning the knob will generate the range of input.
  • LED circuit supplies:
    • LED and 220 (or 560) Ohm resistor

Steps

The project steps are broken down into the circuit setup and the sketch. Let’s start with the circuit.

Circuit

Use this breadboard diagram as your guide:

Circuit diagram for an analog input circuit using a potentiometer

Connect the 5V (Power) on the Arduino to the + rail of the breadboard.

Connect the GND on the Arduino to the – rail of the breadboard.

Wire up the LED circuit that you’ve previously done. If you need help, look back to one of the previous projects.

Attach the potentiometer to the breadboard. Place it so that the pins are closest (on the left side) to the Arduino. The edge of the potentiometer can hang over into the middle of the breadboard where the rails split.

Use 1 jumper wire to connect the power rail of the breadboard to the V+ (top pin) on the potentiometer.

Use 1 jumper wire to connect the output (middle pin) of the potentiometer to the A0 analog input on the Arduino board. Make sure to use the A0 input and not a digital input.

Use 1 jumper wire to connect the **GND (bottom pin)** of the potentiometer.

Sketch

Create a new sketch with the following code and then upload it to your Arduino. For right now, go ahead and use the code below as a guide. Make sure to write the code yourself instead of copying it- this may seem like an unnecessary step, but actually taking the time to write the code will help you internalize it and get a better feel for how it’s written.

There are two new commands in the code: analogRead() and map(). These will be explained in more detail below. To get started, go ahead and just write the code in its entirety:

int ledPin=9; //Variable to store pin of LED int potentPin=A0; //Variable to store pin of potentiometer int potentValue=0; //Variable to store last known value of potentiometer int brightnessValue=0; //Variable to store LED brightness void setup() { // put your setup code here, to run once: pinMode(ledPin, OUTPUT); //Setup LED pin for output } void loop() { // put your main code here, to run repeatedly: potentValue=analogRead(potentPin); //Read the value of the potentiometer pin brightnessValue=map(potentValue,0,1023,0,255); //Map the potentiometer value to a brightness analogWrite(ledPin,brightnessValue); //Set the brightness of the ledPin }
Code language: JavaScript (javascript)

New Commands

The sketch for this project introduced some new commands. Here’s what they do:

analogRead(pin):
analogRead() reads the output from a pin and returns a value between 0 and 1023.

map():

map(inputValue, fromLow, fromHigh, toLow, toHigh)
The map() command re-maps a number from one range to another. The range of inputs returned from the potentiometer is 0 to 1023, whereas the range of brightness for the LED in the code is 0 to 255.

Why map()?

You may notice an issue here: the maximum values for these ranges don’t match. The potentiometer values range from 0 – 1023 whereas the LED accepts values from 0 – 255. The minimum values are both 0, but the maximums are different. Since this is the case, you need to use map() to map the value from the potentiometer to a value on the LED.

map() takes 5 parameters. Look at line 14 in the code:

brightnessValue = map(potentValue, 0, 1023, 0, 255); // map the potentiometer value (0 - 1023) to brightness (0 - 255)

This seems complicated, so let’s break down the syntax.

map()‘s first parameter is potentValue. This variable stores the last known value of the potentiometer. This value will be mapped to the LED’s brightness.

The fromLow value is mapped to a value in toLow. This may seem confusing because the parameters aren’t next to each other. Think of it like this: the low value of the input (potentiometer) → the low value of destination (LED brightness).

In the example, the minimum values are the same for the potentiometer reading and the LED brightness. This means that fromLow and toLow are both 0.

The fromHigh value is mapped to a value in toHigh, the fifth parameter in map(). This is the same structure as the low values: the high value of the input (potentiometer) → the high value of destination (LED brightness). The maximum values are different for the potentiometer reading and the LED brightness, so the fromHigh and toHigh values are different.

Now that you’ve set this up via map(), all the values in between will be corresponding as well so that when you move the potentiometer, you’ll then see changes in the LED brightness. What do you think would happen if you set the toHigh value to 255 instead of 1023? Try this and see if you correctly predicted the outcome.