Advertisement
sunu

ACS712.ino

Nov 26th, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. //https://gist.github.com/locy/7022857
  2. //https://gist.githubusercontent.com/locy/7022857/raw/1a3ff02c1ad74ddd1f4765555a64532e356b704e/ACS712.ino
  3.  
  4. int VQ;
  5. int ACSPin = 0;
  6.  
  7. void setup() {
  8.   Serial.begin(115200);
  9.   VQ = determineVQ(ACSPin); //Quiscent output voltage - the average voltage ACS712 shows with no load (0 A)
  10.   delay(1000);
  11. }
  12.  
  13. void loop() {
  14.   Serial.print("ACS712@A2:");Serial.print(readCurrent(ACSPin),3);Serial.println(" mA");
  15.   delay(150);
  16. }
  17.  
  18. int determineVQ(int PIN) {
  19.   Serial.print("estimating avg. quiscent voltage:");
  20.   long VQ = 0;
  21.   //read 5000 samples to stabilise value
  22.   for (int i=0; i<5000; i++) {
  23.     VQ += analogRead(PIN);
  24.     delay(1);//depends on sampling (on filter capacitor), can be 1/80000 (80kHz) max.
  25.   }
  26.   VQ /= 5000;
  27.   Serial.print(map(VQ, 0, 1023, 0, 5000));Serial.println(" mV");
  28.   return int(VQ);
  29. }
  30.  
  31. float readCurrent(int PIN) {
  32.   int current = 0;
  33.   int sensitivity = 185.0;//change this to 100 for ACS712-20A or to 66 for ACS712-30A
  34.   //read 5 samples to stabilise value
  35.   for (int i=0; i<5; i++) {
  36.     current += analogRead(PIN) - VQ;
  37.     delay(1);
  38.   }
  39.   current = map(current/5, 0, 1023, 0, 5000);
  40.   return float(current)/sensitivity;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement