# Arduino Lesson 8. Analog Inputs

## Overview

In this lesson, you will start by using the Serial Monitor to display analog readings, and then extend the project using eight LEDs from lesson 4, so that you can control the number of LEDs that are lit by turning the knob on a variable resistor.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/240/medium800/learn_arduino_overview.jpg?1396780856)

# Arduino Lesson 8. Analog Inputs

## Parts

To build the projects described in this lesson, you will need the following parts.

# Arduino Lesson 8. Analog Inputs

## An Experiment

Before we go ahead and use the LEDs, you can try a little experiment using just the variable resistor also known as a&nbsp; **potentiometer** (often called a 'pot' for short) and the Arduino Serial Monitor.

Connect up your breadboard as shown below:

![](https://cdn-learn.adafruit.com/assets/assets/000/002/248/medium800/learn_arduino_breadboard_1.jpg?1396780899)

Load the following sketch onto your Arduino.

```auto
/*
Adafruit Arduino - Lesson 8. Analog Inputs
*/

int potPin = 0;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  int reading  = analogRead(potPin);
  Serial.println(reading);
  delay(500);
}
```

Now open the Serial Monitor, and you will see a stream of numbers appearing.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/249/medium800/learn_arduino_serial_monitor.jpg?1396780907)

Turn the knob on the variable resistor and you will see the number change between 0 and 1023.

The Serial Monitor is displaying the analog reading value from A0 using the line:

```auto
 int reading  = analogRead(potPin);
```

The voltage at A0 is being transformed into a number between 0 and 1023.

# Arduino Lesson 8. Analog Inputs

## Variable Resistors (Pots)

For historical reasons, variable resistors are often called 'pots' which is short for 'potentiometers'.

In our experiment with the Serial Monitor, the pot is somehow varying the voltage at A0 and the little test sketch is converting this voltage into a number between 0 and 1023.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/250/medium800/learn_arduino_pot.png?1396780987)

Your pot has a circular 'track' that acts as a resistor, in our case it's a 10 kΩ resistor. However, the difference with a pot, is that there is also a middle connection called the 'slider'. This connection is rotated when you turn the pot. So if you connect one end of the pot to 5V and the other to GND, then the voltage at the slider will vary between 0 and 5V as you turn it.

# Arduino Lesson 8. Analog Inputs

## Breadboard Layout

Let's do something more interesting with the pot. We can use it to control the number of LEDs lit.

This breadboard layout is based on that of lesson 4, there are a few jumpers moved, and the pot and it's connections to the Arduino have been added.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/251/medium800/learn_arduino_breadboard_2.jpg?1396780994)

# Arduino Lesson 8. Analog Inputs

## Arduino Code

Load the following sketch onto your Arduino board. &nbsp;

```auto
/*
Adafruit Arduino - Lesson 8. Analog Inputs - LEDs
*/

int potPin = 0;
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;

int leds = 0;

void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}

void loop() 
{
  int reading  = analogRead(potPin);
  int numLEDSLit = reading / 114;  //1023 / 9
  leds = 0;
  for (int i = 0; i &lt; numLEDSLit; i++)
  {
    bitSet(leds, i); 
  }
  updateShiftRegister();
}

void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}
```

You should recognize much of this code from lesson 4. So refer back to that lesson for more information about how the LEDs are controlled.

The key parts of the sketch as far as analog inputs are concerned are the line where we define the analog pin that we are going to connect to the slider of the pot:

```auto
int potPin = 0;
```

Note that we do not need to put anything in 'setup' to set the pin mode for an analog input.

In the main loop, we read the analog value like this:

```auto
  int reading  = analogRead(potPin);
```

But then this reading between 0 and 1023 needs converting into a number of LEDs to light, between 0 and 8. The range of numbers, between 0 and 8 is actually 9 values. So we need to scale the reading by 1023 divided by 9 or 114.

```auto
int numLEDSLit = reading / 114;
```

To light the right number of LEDs, we use the 'for' loop to count from 0 up to 'numLEDSLit' setting the bit at that position.

```auto
  leds = 0;
  for (int i = 0; i &lt; numLEDSLit; i++)
  {
    bitSet(leds, i); 
  }
```

Finally we update the shift register with a call to:

```auto
updateShiftRegister();
```

# Arduino Lesson 8. Analog Inputs

## Other Things to Do

It is actually simpler to light a single LED to indicate the position of the knob. Try modifying your sketch to do this.

[Click Here for the Next Lesson](http://learn.adafruit.com/adafruit-arduino-lesson-9-sensing-light)
 **About the Author**  
  
Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit:&nbsp;[Programming Arduino](https://www.adafruit.com/products/1019 "Link: https://www.adafruit.com/products/1019"),&nbsp;[30 Arduino Projects for the Evil Genius](https://www.adafruit.com/products/868)&nbsp;and&nbsp;[Programming the&nbsp;Raspberry Pi](https://www.adafruit.com/index.php?main_page=adasearch&q=programming+raspberry+pi "Link: https://www.adafruit.com/index.php?main\_page=adasearch&q=programming+raspberry+pi").
## Featured Products

### Breadboard trim potentiometer

[Breadboard trim potentiometer](https://www.adafruit.com/product/356)
These are our favorite trim pots, perfect for breadboarding and prototyping. They have a long grippy adjustment knob and with 0.1" spacing, they plug into breadboards or perfboards with ease.

This is the same pot that comes with our character LCDs and tutorial...

In Stock
[Buy Now](https://www.adafruit.com/product/356)
[Related Guides to the Product](https://learn.adafruit.com/products/356/guides)
### Diffused Red 5mm LED (25 pack)

[Diffused Red 5mm LED (25 pack)](https://www.adafruit.com/product/299)
Need some indicators? We are big fans of these diffused red LEDs, in fact we use them exclusively in our kits. They are fairly bright so they can be seen in daytime, and from any angle. They go easily into a breadboard and will add that extra zing to your project.

- Pack of 25...

In Stock
[Buy Now](https://www.adafruit.com/product/299)
[Related Guides to the Product](https://learn.adafruit.com/products/299/guides)
### 74HC595 Shift Register - 3 pack

[74HC595 Shift Register - 3 pack](https://www.adafruit.com/product/450)
Add lots more outputs to a microcontroller system with chainable shift registers. These chips take a serial input (SPI) of 1 byte (8 bits) and then output those digital bits onto 8 pins. You can chain them together so putting three in a row with the serial output of one plugged into the serial...

In Stock
[Buy Now](https://www.adafruit.com/product/450)
[Related Guides to the Product](https://learn.adafruit.com/products/450/guides)
### Premium Male/Male Jumper Wires - 40 x 6" (150mm)

[Premium Male/Male Jumper Wires - 40 x 6" (150mm)](https://www.adafruit.com/product/758)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/758)
[Related Guides to the Product](https://learn.adafruit.com/products/758/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Adafruit METRO 328 Fully Assembled - Arduino IDE compatible

[Adafruit METRO 328 Fully Assembled - Arduino IDE compatible](https://www.adafruit.com/product/50)
We sure love the ATmega328 here at Adafruit, and we use them&nbsp;_a lot_&nbsp;for our own projects. The processor has plenty of GPIO, Analog inputs, hardware UART SPI and I2C, timers and PWM galore - just enough for most simple projects. When we need to go small, we use a <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/50)
[Related Guides to the Product](https://learn.adafruit.com/products/50/guides)

## Related Guides

- [74HC595 Shift Register](https://learn.adafruit.com/74hc595.md)
- [TMP36 Temperature Sensor](https://learn.adafruit.com/tmp36-temperature-sensor.md)
- [Metal Inlay Capacitive Touch Buttons](https://learn.adafruit.com/metal-inlay-capacitive-touch-buttons.md)
- [Mini Thermal Receipt Printers](https://learn.adafruit.com/mini-thermal-receipt-printer.md)
- [Wave Shield Voice Changer](https://learn.adafruit.com/wave-shield-voice-changer.md)
- [Animating Multiple LED Backpacks](https://learn.adafruit.com/animating-multiple-led-backpacks.md)
- [SMS Texting Pet Food Dish](https://learn.adafruit.com/sms-texting-pet-food-dish.md)
- [Remote controlled door lock using a fingerprint sensor & Adafruit IO](https://learn.adafruit.com/remote-controlled-door-lock-using-a-fingerprint-sensor-and-adafruit-io.md)
- [Arduino Lesson 1. Blink](https://learn.adafruit.com/adafruit-arduino-lesson-1-blink.md)
- [DS1307 Real Time Clock Breakout Board Kit](https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit.md)
- [3D Printed Animatronic Robot Head](https://learn.adafruit.com/3d-printed-animatronic-robot-head.md)
- [TTL Serial Camera](https://learn.adafruit.com/ttl-serial-camera.md)
- [Collin's Lab: MIDI](https://learn.adafruit.com/collins-lab-midi.md)
- [NeoPixel Painter](https://learn.adafruit.com/neopixel-painter.md)
- [Automatic Monitor Color Temperature Adjustment](https://learn.adafruit.com/automatic-monitor-color-temperature-adjustment.md)
- [Geofencing with the FONA 808 & Adafruit IO](https://learn.adafruit.com/geofencing-with-the-fona-808-and-adafruit-io.md)
