Analog Input Lab
In the analog input lab, we look at variable resistors, and the ability to read their changes in voltage through the Arduino’s analog inputs.
Using a simple variable resistor
The basic setup involves a potentiometer, which serves as a voltage divider without any additional components. The center pole of the potentiometer is run to analog input pin 3 on the Arduino, while the other poles run to power and ground. An LED (with a 330 Ohm resistor) is attached to digital pin 9 and ground to serve as our output.
The following code is loaded to the Arduino:
int ledPin = 9; // LED connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
The Arduino reads the voltage coming off of the potentiometer (reading 1023 for 5v, and 0 for 0 volts) and divides it by 4 to get an 8-bit value for pulse-width modulated output to the LED.
Adjusting LED brightness with Arduino from Jeff Kirsch on Vimeo.
The code also prints the values coming off pin 3 to the serial monitor:
Serial output of potentiometer readings from Jeff Kirsch on Vimeo.
Sensing temperature
The next step is replacing the potentiometer with another form of variable resistor (and an appropriate fixed resistor to complete the voltage divider). I chose a thermistor, and started with a 10k fixed resistor.
Squeezing with my fingers, I was only able to raise the temperature a few degrees, but there was clearly a change in voltages. The voltage drop across the thermistor read between 4.45 volts when “resting” and 4.38 volts when held, with appropriate changes in voltage measured across the fixed resistor.
Switching to a 1k fixed resistor gave a more even division of voltage at ambient temperatures, with the thermistor ranging between 2.45v baseline and 2.14v when held, with the fixed resistor taking on the balance of 5 volts in each case. Serial output reflected this with reads in the 500+ range.
Invent something
For the last part of the lab, we’re asked to make something with an analog input.
A raid of another institution’s computer store brought a very interesting component to my attention, the Sparkfun ADXL335 Triple Axis Accelerometer. Some quick research revealed that this breakout board outputs voltage between 0 and 5 volts from each of 3 pins depending on its orientation. That meant that I’d need three analog outputs as well. But what has three components with values between 0 and 255? Why, the RGB color space, of course. Luckily, the computer store also carried a diffused Triple Output RGB LED.
To start with, however, I simply soldered a header onto the accelerometer, hooked it up to the Arduino (with the help of this tutorial), and watched the values roll into the serial monitor.
I then tweaked the code to map the input to three channels of PWM output and hooked up a few LEDs for a quick test.
Accelerometer-controlled LEDs from Jeff Kirsch on Vimeo.
With that all working, and having gotten past some common cathode confusion, I wired up the RGD LED, tweaked the code, and gave it a try.
Arduino + Accelerometer + RGB LED from Jeff Kirsch on Vimeo.
And there you have it… real-time color mixing based on orientation. My lovely fiancee Cara assisted with the demo.
In my excitement, I left the outputs connected to the closest pins of the LED, so in this video, the color channels don’t map to the axes in the correct order. In the code included below, X is blue, Y is red and Z is green.
/*
Created September 27 2009 by Jeff Kirsch
Translates 3 axis motion into color via a triple input LED.
Based on http://www.arduino.cc/en/Tutorial/ADXL3xx by David A. Mellis
modified by Tom Igoe
The circuit:
analog 0: accelerometer self test
analog 1: z-axis
analog 2: y-axis
analog 3: x-axis
analog 4: ground
analog 5: vcc
Digital pins 9, 10 and 11 drive blue, red and green legs
*/
// these constants describe the pins. They won't change:
const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
const int xpin = 3; // x-axis of the accelerometer
const int ypin = 2; // y-axis
const int zpin = 1; // z-axis (only on 3-axis models)
const int blueLed = 9; // output pin for blue LED
const int redLed = 10; // output pin for red LED
const int greenLed = 11; // output pin for green LED
void setup()
{
// Provide ground and power by using the analog inputs as normal
// digital pins. This makes it possible to directly connect the
// breakout board to the Arduino. If you use the normal 5V and
// GND pins on the Arduino, you can remove these lines.
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
//sets pins for LEDs as outputs
pinMode(blueLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}
void loop()
{
//run tilitToLight function for each axis
tiltToLight(xpin, blueLed);
tiltToLight(ypin, redLed);
tiltToLight(zpin, greenLed);
}
void tiltToLight(int readFrom, int writeTo){
//set readVal to the input from the accelerometer, minus 400
//for this model accelerometer, raw input minus 400 yields values roughly between 0 and 255
int readVal = (analogRead(readFrom) - 400);
//check for out-of-range values
if (readVal < 0) {
readVal = 0;
}
else if(readVal > 255) {
readVal = 255;
}
//write the modified input from the specificed axis of the accelerometer to the appropriate output pin
analogWrite(writeTo, (readVal));
}
Thanks to Eric for his help in getting the input reading and output writing into a nice reusable function.
Lab instructions available here.
About this entry
You’re currently reading “Analog Input Lab,” an entry on Jeff Kirsch
- Published:
- 9.28.09 / 10pm
- Category:
- Coursework, Fundamentals of Physical Computing
- Tags:

No comments
Jump to comment form | comments rss [?] | trackback uri [?]