ACS712 sample code

Have you got the greatest 48 bit multiplier ever conceived? Prove it - post your code here.

Moderator: phalanx

ACS712 sample code

Postby flegmatoid » Fri Sep 28, 2012 8:25 am

Code: Select all
int VQ;
int ACSPin = A2;

void setup() {
  Serial.begin(9600);
  VQ = determineVQ(ACSPin); //Quiscent output voltage - the average voltage ACS712 shows with no load (0 A)
  delay(1000);
}

void loop() {
  Serial.print("ACS712@A2:");Serial.print(readCurrent(ACSPin),3);Serial.println(" mA");
  delay(150);
}

int determineVQ(int PIN) {
  Serial.print("estimating avg. quiscent voltage:");
  long VQ = 0;
  //read 5000 samples to stabilise value
  for (int i=0; i<5000; i++) {
    VQ += analogRead(PIN);
    delay(1);//depends on sampling (on filter capacitor), can be 1/80000 (80kHz) max.
  }
  VQ /= 5000;
  Serial.print(map(VQ, 0, 1023, 0, 5000));Serial.println(" mV");
  return int(VQ);
}

float readCurrent(int PIN) {
  int current = 0;
  int sensitivity = 185.0;//change this to 100 for ACS712-20A or to 66 for ACS712-30A
  //read 5 samples to stabilise value
  for (int i=0; i<5; i++) {
    current += analogRead(PIN) - VQ;
    delay(1);
  }
  current = map(current/5, 0, 1023, 0, 5000);
  return float(current)/sensitivity;
}
flegmatoid
 
Posts: 3
Joined: Fri Sep 28, 2012 8:23 am

Re: ACS712 sample code

Postby waltr » Sun Oct 07, 2012 12:29 pm

Which PIC is this code for?
Which C compiler is it wirtten for?
What does it do?
waltr
Support Volunteer
 
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: ACS712 sample code

Postby elik745i » Thu Oct 10, 2013 9:24 am

Nice, but I'm using 30A version and noticed that multimeter gives almost 2ce readings, what could be wrong with this code?
elik745i
 
Posts: 3
Joined: Thu Oct 10, 2013 8:54 am

Re: ACS712 sample code

Postby flegmatoid » Wed Oct 16, 2013 5:12 am

you've missed the comment which says: "//change this to 100 for ACS712-20A or to 66 for ACS712-30A"
so your sensitivity line should look like:
int sensitivity = 66.0;
flegmatoid
 
Posts: 3
Joined: Fri Sep 28, 2012 8:23 am


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest