# 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.

# Parts

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

# 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 potentiometer (often called a ‘pot’ for short) and the Arduino Serial Monitor. Connect up your breadboard as shown below:
1. /*
3. */
4.
5. int potPin =0;
6.
7. void setup()
8. {
9. Serial.begin(9600);
10. }
11.
12. void loop()
13. {
16. delay(500);
17. }
Now open the Serial Monitor, and you will see a stream of numbers appearing.
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:
The voltage at A0 is being transformed into a number between 0 and 1023.

# 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.
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.

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.

# Arduino Code

1. /*
3. */
4.
5. int potPin =0;
6. int latchPin =5;
7. int clockPin =6;
8. int dataPin =4;
9.
10. int leds =0;
11.
12. void setup()
13. {
14. pinMode(latchPin, OUTPUT);
15. pinMode(dataPin, OUTPUT);
16. pinMode(clockPin, OUTPUT);
17. }
18.
19. void loop()
20. {
22. int numLEDSLit = reading /114;//1023 / 9
23. leds =0;
24. for(int i =0; i < numLEDSLit; i++)
25. {
26. bitSet(leds, i);
27. }
29. }
30.
32. {
33. digitalWrite(latchPin, LOW);
34. shiftOut(dataPin, clockPin, LSBFIRST, leds);
35. digitalWrite(latchPin, HIGH);
36. }
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:
1. 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:
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.
1. 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.
1. leds =0;
2. for(int i =0; i < numLEDSLit; i++)
3. {
4. bitSet(leds, i);
5. }
Finally we update the shift register with a call to: