Adafruit Arduino Lesson 8 Analog Inputs
Adafruit Arduino Lesson 8 Analog Inputs
Analog Inputs
Created by Simon Monk
Guide Contents 2
Overview 3
Parts 4
Part 4
Qty 4
An Experiment 5
Variable Resistors (Pots) 7
Breadboard Layout 8
Arduino Code 9
Other Things to Do 11
Part
Qty
Half-size Breadboard
1
Arduino Uno R3
1
/*
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.
The Serial Monitor is displaying the analog reading value from A0 using the line:
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.
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.
/*
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 < 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:
int potPin = 0;
Note that we do not need to put anything in 'setup' to set the pin mode for an analog input.
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.
leds = 0;
for (int i = 0; i < numLEDSLit; i++)
{
bitSet(leds, i);
}
updateShiftRegister();
https://adafru.it/aUt
https://adafru.it/aUt
Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon
are available from Adafruit: Programming Arduino (http://adafru.it/1019), 30 Arduino Projects for the Evil
Genius (http://adafru.it/868) and Programming the Raspberry Pi (https://adafru.it/aM5).