Skip to content

Instantly share code, notes, and snippets.

@pral2a
Last active August 29, 2015 13:58
Show Gist options
  • Save pral2a/10007689 to your computer and use it in GitHub Desktop.
Save pral2a/10007689 to your computer and use it in GitHub Desktop.
ACS712 - Arduino (test)
#define ACS712_SENSITIVITY5 0.185
#define ACS712_SENSITIVITY20 0.100
#define ACS712_SENSITIVITY30 0.066
#define ADCREF 5.0
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
double current = acs712_getcurrent(A0);
// print out the value you read:
Serial.println(current);
delay(1); // delay in between reads for stability
}
double acs712_getcurrent(int analogPin) {
int rawADC = analogRead(analogPin);
double voltagein = map(double(rawADC), 0, 1023, 0, 5.0);
return (double)((double)(voltagein - (double)(ADCREF/(double)2)) / (double)ACS712_SENSITIVITY20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment