Skip to content

Instantly share code, notes, and snippets.

@xandout
Created February 13, 2014 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xandout/8967814 to your computer and use it in GitHub Desktop.
Save xandout/8967814 to your computer and use it in GitHub Desktop.
Arduino Potentiometer Reading
//Potentiometer Reader
/*
+++Wiring+++
(ground)----(potentiometer)----|----(+5VDC)
|
Analog Pin 5
*/
int potPin = A5; //Analog input pin 5, labeled A5
double onePercent = 10.24; //analogRead() gets the value from 0 - 1023, this is roughly the step size to get 1%
void setup() {
Serial.begin(9600); //Initialize serial at 9600 baud
}
void loop(){
Serial.println(calcPercent(analogRead(potPin))); //Prints the percentage of voltage(5VDC) coming from the potentiometer
delay(200); //Just to slow things down so you can see the precision, you would be amazed how fast 16MHz is.
}
//calculates the percentage of voltage coming through the potentiometer
double calcPercent(int pot){ //Takes the integer value from the analog pin
double dblPot = (double)pot; //cast C integer to C double(needed for precision greater than 1 ex: 20.36
return dblPot/onePercent; //return a value between 0.00 and 99.90
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment