Sie sind auf Seite 1von 8

http://itp.nyu.edu/physcomp/Labs/AnalogIn?

action=print

From Physical Computing at ITP

Labs: AnalogIn
Overview
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) 1. 2. 3. 4. 5. 6. Parts Prepare the breadboard Add a potentiometer and LED Program the Module Other variable resistors Get creative

1. Parts
For this lab you will need to have the following parts:

Solderless breadboard

22-AWG hookup wire

Arduino Microcontroller module

Variable resistors

Light Emiting Diodes, LED

560-ohm (anything from 220 to 1K) and 10Kohm resistors

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

1 von 8

17.01.2013 12:54

http://itp.nyu.edu/physcomp/Labs/AnalogIn?action=print

2. 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:

(Diagram made with Fritzing)

3. Add a potentiometer and LED


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

2 von 8

17.01.2013 12:54

http://itp.nyu.edu/physcomp/Labs/AnalogIn?action=print

(Diagram made with Fritzing)

4. Program the Module


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.
const int ledPin = 9; int analogValue = 0; int brightness = 0; // pin that the LED is attached to // value read from the pot // PWM pin that the LED is on.

[Get Code]

In the setup() method, initialize serial communications at 9600 bits per second, and set the LED's pin to be an output.

3 von 8

17.01.2013 12:54

http://itp.nyu.edu/physcomp/Labs/AnalogIn?action=print

void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); // declare the led pin as an output: pinMode(ledPin, OUTPUT); }

[Get Code]

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.
void loop() { analogValue = analogRead(A0); brightness = analogValue /4; analogWrite(ledPin, brightness); Serial.println(brightness); }

// read the pot value //divide by 4 to fit in a byte // PWM the LED with the brightness value // print the brightness value back to the serial monitor

[Get Code]

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.

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

4 von 8

17.01.2013 12:54

http://itp.nyu.edu/physcomp/Labs/AnalogIn?action=print

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:

use wire wrapping wire and a wire wrapping tool

use screw terminals (if you have a row of three, you can attach the fixed resistor as well)

use female headers

Thanks to adafruit, who have a good FSR tutorial as well. Here's an example circuit much like the pink monkey circuit above, but with force-sensing resistors instead of flex sensors.

5 von 8

17.01.2013 12:54

http://itp.nyu.edu/physcomp/Labs/AnalogIn?action=print

(Diagram made with Fritzing)

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 analogWrite() command. 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 modify the code. 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); analogWrite(ledPin, brightness);

[Get Code]

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?

6 von 8

17.01.2013 12:54

http://itp.nyu.edu/physcomp/Labs/AnalogIn?action=print

// read the sensor: int analogValue = analogRead(A0); // map the result to a voltage range from 0 to 5 volts // using a floating point decimal variable (called a float): float voltage = map(analogValue, 0.0, 5.0); // print it out: Serial.println(voltage);

[Get Code]

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.
const int redLED = 10; const int greenLED = 11; int rightSensorValue = 0; int leftSensorValue = 0; // // // // pin that the red LED is on pin that the green LED is on value read from the right analog sensor value read from the left analog sensor

[Get Code]

In the setup(), initialize serial communication at 9600 bits per second, and make the LED pins outputs.
void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); // declare the led pins as outputs: pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); }

[Get Code]

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.
void loop() { rightSensorValue = analogRead(A0); // read the pot value // map the sensor value from the input range (400 - 900, for example) // to the output range (0-255). Change the values 400 and 900 below // to match the range your analog input gives: int brightness = map(rightSensorValue, 400, 900, 0, 255); analogWrite(redLED, brightness); // set the LED brightness with the result Serial.println(rightSensorValue); // print the sensor value back to the serial monitor

[Get Code] 7 von 8

17.01.2013 12:54

http://itp.nyu.edu/physcomp/Labs/AnalogIn?action=print

Finish the main loop by doing the same thing with the left sensor and the green LED.
// now do the same for the other sensor and LED: leftSensorValue = analogRead(A1); // read the pot value // map the sensor value to the brightness again. No need to // declare the variable again, since you did so above: brightness = map(leftSensorValue, 400, 900, 0, 255); analogWrite(greenLED, brightness); // set the LED brightness with the result Serial.println(leftSensorValue); // print the sensor value back to the serial monitor }

[Get Code]

6. 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.
Retrieved from http://itp.nyu.edu/physcomp/Labs/AnalogIn Page last modified on September 04, 2012, at 02:30 PM

8 von 8

17.01.2013 12:54

Das könnte Ihnen auch gefallen