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, and another to hold the brightness value. Make a global constant to give the LED's pin number a name.
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. Then divide the analog value by 4 to get it into a range from 0 to 255. Then use the analogWrite() command to face the LED. Then print out the brightness value.
When you run this code, the LED should dim up and down as you turn the pot, and the brightness 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 brightness of two LEDs. The same values could be used to control servo motors, change the frequency on a speaker, or move servo motors.
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 force-sensing resistors instead of flex sensors.
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. Dividing by 4 gives you a range of 0 to 255, which is the full output range of the
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 map the range that the sensor actually gives as input to the range that the LED needs as output. For example, if your photocell gives a range from 400 to 900, you'd do this:
// map the sensor value from the input range (400 - 900, for example) to the output range (0-255):
int brightness = map(sensorValue, 400, 900, 0, 255);
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, how could you use the map function to get it?
Now write a sketch to control the red LED with the first sensor (we'll call it the right hand sensor) and the green LED with the second sensor (we'll call it the left hand sensor). First, make two constants for the LED pin numbers, and two variables for the left and right 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 right sensor using analogRead(). Map its range to a range from 0 to 255. Then use analogWrite() to set the brightness of the LED from the mapped value. Print the sensor value out as well.
Finish the main loop by doing the same thing with the left sensor and the green LED.
Make a luv-o-meter with analog inputs. A luv-o-meter is a device that measures a person's potential to be a lover, and displays it on a graph of lights. In gaming arcades, the luv-o-meter is usually a handle that a person grips, and his or her grip is measured either for its strength or its sweatiness. But your luv-o-meter can measure any analog physical quantity that you want, providing you have a sensor for it. Make sure the display is clear, so the participant knows what it means, and make sure it is responsive.