|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
Analog In with an ArduinoOverviewIn 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. PartsFor this lab you will need to have the following parts:
Variable resistors
2. Prepare the breadboardConect 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:
3. Add a potentiometer and LEDConnect a potentiometer to analog in pin 0 of the module, and an LED to digital pin 9:
4. Program the ModuleProgram 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.
const int ledPin = 9; // pin that the LED is attached to const int potPin = 0; // pin that the potentiometer is attached to int analogValue = 0; // value read from the pot In the setup() method, initialize serial communications at 9600 bits per second, and set the LED's pin to be an output.
void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); // declare the led pin as an output: pinMode(ledPin, 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.
void loop() { analogValue = analogRead(potPin); // read the pot value Serial.println(analogValue); // print the value of the pot back to the serial monitor digitalWrite(ledPin, HIGH); // turn the LED on delay(analogValue); // delay for a bit digitalWrite(ledPin, LOW); // turn the LED off delay(analogValue); // delay for a bit } 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. 5. Other variable resistorsYou 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: Serial.println(voltage); 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.
const int LEDone = 9; // pin that the first LED is on const int LEDtwo = 10; // pin that the second LED is on const int LEDone = 11; // pin that the third LED is on const int LEDtwo = 12; // pin that the fourth LED is on const int sensorPin=0; //pin that the sensor is on int sensorValue = 0; // value read from the analog sensor 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(LEDone, OUTPUT); pinMode(LEDtwo, OUTPUT); pinMode(LEDthree, OUTPUT); pinMode(LEDfour, OUTPUT); } Start the main loop by reading the sensor using analogRead(). Print the sensor value out as well.
void loop() { sensorValue = analogRead(sensorPin); // read the pot value Serial.println(sensorValue); // print the sensor value back to the serial monitor Finish the main loop by setting up an if/else loop that turns the appropriate LEDs on/off.
//assuming the values are between 400 and 900 if(sensorValue<450){ digitalWrite(LEDone, HIGH); digitalWrite(LEDtwo, LOW); digitalWrite(LEDthree, LOW); digitalWrite(LEDfour, LOW); }else if (sensorValue >=450 && sensorValue<= 650){ digitalWrite(LEDone, HIGH); digitalWrite(LEDtwo, HIGH); digitalWrite(LEDthree, LOW); digitalWrite(LEDfour, LOW); }else if (sensorValue >=651 && sensorValue<= 775){ digitalWrite(LEDone, HIGH); digitalWrite(LEDtwo, HIGH); digitalWrite(LEDthree, HIGH); digitalWrite(LEDfour, LOW); }else if (sensorValue >=776){ digitalWrite(LEDone, HIGH); digitalWrite(LEDtwo, HIGH); digitalWrite(LEDthree, HIGH); digitalWrite(LEDfour, HIGH); } } |