Home‎ > ‎Unit 08‎ > ‎

Simple Analog Read

Arduino's have 6 analog inputs. Analog inputs are much cooler than digital inputs. A digital input can be LOW or HIGH, 0 or 1. Analog inputs can be any value from 0 to 1023. You should find three slide potentiometers with wires attached in your kit. You will need one of them. The wire that is alone on a side is the one that goes to the analog pin on your Arduino. Note, this is alone on a side, not an end (see Picture). One wire from each end should go to positive (5v) and negative (gnd). It doesn't matter which goes where.

If you inadvertently hook up the slide potentiometer incorrectly it is very easy to cause a short circuit. Watch for the LEDs on your Arduino to go out while you are using the potentiometer. If that happes disconnect the potentiometer immediately!


Try the code below. You'll notice we're using the text function from Chapter 5 in Getting Started with Processing. We added a new piece to it however. The second parameter in text() is for vertical alignment. We also introduce arduino.analogRead(#) where # is the analog pin number. Move the slider back and forth. You should see the number change. Note down which side is 1023 and which is 0.

Now swap the two wires hooked up to power. Did anything change?

import processing.serial.*; //Import the serial library into your sketch
import cc.arduino.*;        //Import the Arduino-Firmata library into your sketch

Arduino arduino;            //Create an instance of Arduino named arduino (can be any name) 

void setup()
{
  size(240,120);
  textSize(64);
  textAlign(CENTER, CENTER);
  arduino = new Arduino(this, Arduino.list()[0], 57600);
}

void draw()
{
  background(0);
  text(arduino.analogRead(2),width/2,height/2);
}

Try making your own slide potentiometer. For this you'll need a pencil, some jumper wires and the three alligator leads from your kits. Scribble a thick line on a piece of paper. I mean thick and really dark. The darker the better. Fold the paper in half so that the line is on both sides of the fold and attach an alligator lead to each end. The other ends of these go to power. Attach your third alligator lead to your analog pin. Now simply slide it along the pencil line. Not very consistant, but it should give you the idea. This is pretty much the way your actual slide potentiometer works.

Assignment 8.1 - Modify your program from 7.3 to respond to your slide potentiometer. Note, you'll probably find map() to be useful.

Comments