Current sensor ACS712 error


#1

Hello,

I installed the ACS712 current sensor, but when it is connected appears every 97 110 mA. If I take it the A0 door, I get 0.0 mA.

The connection of the wires is:
VCC = VIN spark 5V
GND GND = spark
OUT = A0

code:

int VQ;
int ACSPin = A0;

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 = 100.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;
  }

Where is the problem?


#2

One thing I noticed is this line

  current = map(current/5, 0, 1023, 0, 5000);

Since the Spark Core sports a 12bit ADC with 3.3V you’d need to map the range 0..4095 to your voltage range 0..3300mV rather than 10bit 0..1023 to 0..5000mV.

One word of caution, too. Since you got your sensor supplied with 5V (minus about 0.2V voltage drop across protective diode) you have to be careful that your output never exceeds 3.3V otherwise you’ll possibly fry your analog pin!
Either you limit the output or you see if the sensor also works with 3V3 supply (e.g. ACS711).

Before looking at it closer, could you please reformat your code as outlined in this thread


#3

Thank you very much, now works fine.


#4

Sorry, I’ve just edited my previous post. Have you seen the word of caution?

But :+1: that it works now.

Have you done the remapping in you determinVQ(), too?


@rbbahia, a quick search on this forum came up with this thread, giving some more background and even suggesting a 3V3 solution for the ACS712

It’s a good rule of thumb to first search the forum before posting a question - this would safe others the work of answering the same questions over and over again.
Since I’ve assumed you’ve done it already, I didn’t first search but immediately answered - please don’t do this to us.


#5

Mapping to 3300mV instead of 3.3V helped me a lot with reducing erroneous fluctuation in values after subsequent calculations that required multiplying by 1000. Thanks!