Saturday, March 3, 2012

Reading a potentiometer

Reading a potentiometer is a simple thing to do. Connect a 3 leg potentiometer to your Arduino. The middle leg connects to a analog port. We used analog 3. the two outer legs connect to 5v and Gnd, respectively.

Insert the following code into your editor, upload, and run the serial monitor at 9600. As you rotate your potentiometer, the values change in the serial monitor.

int analogPin = 3;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

void setup()
  Serial.begin(9600);          //  setup serial

void loop()
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val);             // debug value

Future posts will show you what you can do with this module.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...