Arduino 101 meetup prep – day 2 – twirly knobs

5May2009 – old stuff that may still be useful

This salvaged bit with 5 pots was from a Sony monitor I believe.

It was another straight-forward reverse-the-board job…

and using a proper labbook this time

I just needed to jumper a few places to accommodate (+5V,Gnd) instead of the original (+5V,-12V) connections

and add the every-handy PC-guts wires to hook it to the Duemilanove

and then write a simple sketch (below) to read the 5 voltage levels every half second and send the result to a PC-hosted terminal

so you can see the values changing as I madly twirl the knobs.  For the newly-initiated, a potentiometer is a variable resistor that’s handy for things like this case where you want to provide a circuit with a user-adjustable voltage.  The Arduino looks at this analog voltage which in this case might range between ground (0V) and Vcc (+5V) (but not quite that wide because of the other resistors involved), and does an analog-to-digital conversion (ADC)  via the sketch instruction:

val0 = analogRead(potPin0);

So now what was previously, say, 3.97 volts in the analog world is now 813 in the digital realm.  I think this is a 10-bit ADC, so you take your 0V-to-5V and break it up into 2**10 (1024) discrete buckets that can be used to approximately represent the original analog value.  (3.97/5.0)*1024 = 813.  Here I just dump out these values to the terminal, but you might use them to control the speed of a motor or the brightness of an LED.

We can talk more about this in person at the Arduino meetup on Friday (I’ll be late, but I’ll be there).

DW

Arduino code:


/*
Read values on 5 pots on analog inputs 0 to 4 and send over serial port to host pc
Author: Darin White
Date: 2009-05-05
*/

long i=0;
int val0=0;
int val1=0;
int val2=0;
int val3=0;
int val4=0;

int potPin0 = 0;
int potPin1 = 1;
int potPin2 = 2;
int potPin3 = 3;
int potPin4 = 4;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  val0 = analogRead(potPin0);    // read the values from the 5 pots all at once to get the best snapshot
  val1 = analogRead(potPin1);
  val2 = analogRead(potPin2);
  val3 = analogRead(potPin3);
  val4 = analogRead(potPin4);  
  
  Serial.print(val0);
  Serial.print(" | ");
  Serial.print(val1);
  Serial.print(" | ");
  Serial.print(val2);
  Serial.print(" | ");
  Serial.print(val3);
  Serial.print(" | ");
  Serial.print(val4);
  Serial.print("\n\r");
  delay(500);
}
 
This entry was posted in arduino and tagged , , . Bookmark the permalink.