## Arduino Potentiometer Tutorial In this tutorial we will see what an analog signal is, and find out how to read it using Arduino's built in 'analogRead()' function. We will need a potentiometer, or "pot" to connect to the Arduino. They are available at electronics stores, like Fry's, Radio Shack, and the online sellers Digikey and Mouser. Get one that is 10kΩ or more, and similar to the ones shown - it will be the easiest to handle. What you learn about reading analog signals will be useful for many, many projects.

### What is analog?

In the real world it means "equivalent" or "similar where it matters". In the electronics world it means a signal that is one of a virtually infinite number of values. For example, the voltage on the center lead of the pot is "analogous" to the position of the knob, which can be in one of a virtually infinite number of positions.

#### What is a Potentiometer?

A pot is a resistor with no insulation, with a "wiper" that contacts it at a position determined by the position of the shaft. If we put ground on one end of the pot (the two outer pins connect to the two ends of the resistor inside) and +5V on the other end, the wiper will always be somewhere between ground and +5V. The actual value is based on the position of the shaft.

#### Arduino's Analog to Digital Converter

The Arduino has a circuit which compares the voltage from our pot to a reference voltage, and determines how many parts of the reference equal the voltage on the pot. It can divide the reference into 1024 equal parts. That makes it a 10-bit converter, because it takes 10 bits to hold a number the size of 1024. There are other sizes of analog to digital converters - from 6-bit to over 32 bits. All Arduinos have 10-bit converters, except Due, which has 12 bits, or 4096 values. The more bits the converter has, the higher its resolution, and the closer it copies the input. Unfortunately, more bits usually means more time to convert.

#### Connecting the Potentiometer Strip each end of three four inch long solid wires. Solder the wires, one to each lead of the pot. With the Arduino unplugged and off, plug the wire on the right in one of the two sockets marked "5V", the wire on the left in the socket marked "GND" and the center wire in the socket marked "A0". If you don't have solid wire, you can use stranded, but you might want to either strip the Arduino end long or solder pins to the ends. I soldered pins on the wires.

#### Code

```
int LED = 13;
int POT = A0;
int midway = int(1023/2);

void setup() {

// Digital pin to output.
pinMode(LED, OUTPUT);
}

void loop() {

// Read the pot and set the LED.
if (analogRead(POT) > midway) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
```

#### What it Does

The program continually reads the value of the pot and compares it to one half of the reference (511). If the value is greater than one half of the reference, the LED is turned on. If the value is less than one half of the reference, the LED is turned off. What you should see is with the pot turned all of the way to the left, the LED is off. As you turn it to the right, when you pass half way, the LED will come on. Turning it back to the left turns the LED off.

Another, more traditional use of the analogRead function it to actually return an analog value! The following code prints the value (0 - 1023) read by the ADC. We can also print the voltage on the wiper of the pot.

```
#define POT A0

void setup() {

Serial.begin(9600);
}

void loop() {

Serial.print("\t=\t");
Serial.println(5.0 * analogRead(POT) / 1024);
delay(1000);
}
```

Which prints something like this when you turn the knob:

```
0	=	0.00
18	=	0.08
72	=	0.36
153	=	0.75
207	=	1.02
261	=	1.27
315	=	1.54
377	=	1.84
436	=	2.13
489	=	2.39
557	=	2.72
642	=	3.13
754	=	3.68
847	=	4.14
956	=	4.67
1023	=	5.00
1023	=	5.00
1023	=	5.00
```
Notes:

The digital pins default to read mode. You must set them up for writing using "pinMode(PIN, OUTPUT)".

The analog input pins do not have to be set up for reading. The Arduino code does that when you call analogRead().

The first analog reading should be discarded. The first reading after changing the reference should be discarded.