Research & Learning
Other Class pages
Analog In with an Arduino
In this lab, you'll learn how to connect a variable resistor to a microcontroller and read it as an analog input. You'll be able to read changing conditions from the physical world and convert them to changing variables in a program.
Table of Contents (hide)
For this lab you will need to have the following parts:
Conect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections:
Connect a potentiometer to analog in pin 0 of the module, and an LED to digital pin 9:
Program your Arduino as follows:
First, establish some global variables: One to hold the value returned by the potentiometer; Make a global constants to give the LED's pin number a name as well as the sensor.
In the setup() method, initialize serial communications at 9600 bits per second, and set the LED's pin to be an output.
In the main loop, read the analog value using analogRead() and put the result into the variable that holds the analog value and print it out. Use the delay() command in conjunction with digitalWrite() to turn the LED on and off.
When you run this code, the LED should blink faster and slower as you turn the pot, and the value should show up in the serial monitor.
You can use many different types of variable resistors for analog input. For example, the pink monkey in the photo below has his arms wired with flex sensors. These sensors change their resistance as they are flexed. When the monkey's arms move up and down, the values of the flex sensors change the number of LEDs that are turned on.
Note: Flex sensors and force-sensing resistors melt easily, so unless you are very quick with a soldering iron, it's risky to solder directly to their leads. Here are three better solutions:
Here's an example circuit much like the pink monkey circuit above, but with a force-sensing resistor instead of a flex sensor.
The circuit above works for any variable resistor. It's called a voltage divider. There are two voltage dividers, one on analog in 0 and one on analog in 1. The fixed resistor in each circuit should have the same order of magnitude as the variable resistor's range. For example, if you're using a flex sensor with a range of 50 - 100 kilohms, you might use a 47Kohm or a 100Kohm fixed resistor. If you're using a force sensing resistor that goes from inifinity ohms to 10 ohms, but most of its range is between 10Kohms and 10 ohms, you might use a 10Kohm fixed resistor.
The code above assumed you were using a potentiometer, which always gives the full range of analog input, which is 0 to 1023. The voltage divider circuit, on the other hand, can't give you the full range. The fixed resistor in the circuit limits the range. You'll need to find your maximum and minimum values to determine the range for turning on the LEDs.
To find out your range, open the serial monitor and watch the printout as you press the FSR or flex the flex sensor. Note the maximum value and the minimum value. Then you can use the range that the sensor actually gives as input to the range that the LED needs as output.
You know that the maximum input range of any analog input is from 0 to 5 volts. So if you wanted to know the voltage on an analog input pin at any point, you use a little math to get it.
// read the sensor:
int analogValue = analogRead(A0);
// map the result to a voltage range from 0 to 5 volts
float voltage = (5./1023.)*analogValue;
// print it out:
Now write a sketch to control the LEDs with the sensor so that as the sensor lets more voltage through, the number of LEDs that light up changes. First, make constants for the LED pin numbers, and a variable for the sensor values.
In the setup(), initialize serial communication at 9600 bits per second, and make the LED pins outputs.
Start the main loop by reading the sensor using analogRead(). Print the sensor value out as well.
Finish the main loop by setting up an if/else loop that turns the appropriate LEDs on/off.