# ESP32 using acs712 give wrong values

I'm trying to measure current with acs712 30A current sensor connecting to NodeMCU-32S but I'm getting wrong and unstable values. For zero current the ADC pin should read value of 2048 (esp has 12 bit sensitivity) but instead I'm getting values about 2740-2760. So first I have problem with incorrect value and second is unstable reading. Basically the whole thing look like:

simulate this circuit – Schematic created using CircuitLab

My test code is:

enter dcode here

#include <stdint.h>
#include "esp_err.h"
int mVperAmp = 66; // 66 mV/A output sensitivity
int RawValue= 0;
int ACSoffset = 5500/2;
double Voltage = 0;
double Amps = 0;

void setup(){
Serial.begin(115200);
}

void loop(){
Voltage = (RawValue / 4096.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);
Serial.print("Raw Value = " ); // shows pre-scaled value
Serial.print(RawValue);
Serial.print("\t mV = "); // shows the voltage measured
Serial.print(Voltage,3);
Serial.print("\t Amps = ");
Serial.println(Amps,3);
delay(250);
}


Thanks.

• How's your PCB layout? It looks like you're just getting noise. – Hearth Feb 13 '19 at 14:20
• I don't have PCB layout. It's all connecting with wires. – Electrician Feb 13 '19 at 14:23
• Well, there's your problem then. At least the unstable readout. If it's all just wired together I'm surprised you're getting only 20 counts variation. The offset is a different problem; these sensors are sensitive to magnetic fields, so that could be affecting it? – Hearth Feb 13 '19 at 14:26
• I saw lot of guides show how to use acs712 and getting good results just with simple wires. Just want to test the issue before layout. I think it's something else beside the wires. Maybe need to calibrate esp pins or put a capacitor. Not sure yet, that's why I'm asking. – Electrician Feb 13 '19 at 14:30

The ESP32 runs with a supply voltage of 3.3V. That's also the reference voltage for analog inputs.

The ACS712 requires 5V as Vcc and outputs 0.5 x Vcc at 0 A (no current), i.e. 2.5V. For the ESP32, that's 2.5V / 3.3V x 4093 units = 2703. So your measurement is not that far off.

Note however, the for higher currents the ACS712 will output a voltage of more than 3.3V. You won't be able to correctly read them as the ESP32 tops out.

To increase the accurracy, you probably need to improve several things:

• Measure the ESP32 reference voltage and correct your readings accordingly (calibration)