Main

Research & Learning

Class pages

Shop Admin

ITP Help Pages
Tom's pcomp site
DanO's pcomp site


Analog In

Analog Input Lab

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.

For this lab you'll need:

Solderless breadboard
Solderless breadboard
hookup wire
22-AWG hookup wire
Arduino module
Arduino Microcontroller
module


Light Emiting Diodes
Light Emiting Diodes, LED
resistors
220-ohm and 10Kohm resistors
potentiometer
10Kohm potentiometer

Variable resistors

potentiometer
Flex sensors
(or a different
form of variable resistor)


Prepare the breadboard

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:

If you're using an Arduino breadboard shield, there is a row of sockets connected to 5V on the analog in side of the breadboard, and a row connected to ground on the digital in side of the board:


Add a potentiometer and LED

Connect a potentiometer to analog in pin 0 of the module, and an LED to digital pin 9:

Breadboard version:

Breadboard shield version:

Program the Module

Program your Arduino with the following code:

int potPin = 0;    // Analog input pin that the potentiometer is attached to
int potValue = 0;   // value read from the pot
int led = 9;    // PWM pin that the LED is on.  n.b. PWM 0 is on digital pin 9

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  potValue = analogRead(potPin); // read the pot value
  analogWrite(led, potValue/4);  // PWM the LED with the pot value (divided by 4 to fit in a byte)
  Serial.println(potValue);      // print the pot value back to the debugger pane
  delay(10);                     // wait 10 milliseconds before the next loop
}

When you run this code, the LED should dim up and down as you turn the pot, and the value of the pot should show up in the debugger pane.

Other variable resistors

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.

table
)

Monkey's arms schematic:

Breadboard version:

Breadboard Shield version:

Here's a closeup on the breadboard:

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.

Get creative

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.

  Edit | View | History | Print | Recent Changes | Search Page last modified on October 03, 2006, at 11:15 PM