Electronic – ESP32 using acs712 give wrong values

#nodemcucurrent measurementcurrent-sensinghall-effect

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:

schematic

simulate this circuit – Schematic created using CircuitLab

My test code is:

enter dcode here

#include <stdint.h>
#include "esp_err.h"
#include "driver/adc.h"
const int analogIn = 34;
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);
  pinMode(analogIn, INPUT);
}

void loop(){
  RawValue = analogRead(analogIn); 
  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.

Best Answer

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)
  • Measure the ACS712 output voltage and correct your readings (calibration)
  • Add the capacitor as proposed by the ACS712 data sheet
  • Add the capacitor as proposed by the ESP32 data sheet
  • Improve the wiring to reduce noise
  • Use multi-sampling (i.e. use an average of several consecutive readings)

Update

The ESP-IDF programming guide contains a page about the Analog to Digital Converter with several ways to improve the accuracy. It will help to understand the overall architecture of the ESP32's ADC. And even though it's for the ESP-IDF environment, most of it should also work with the Arduino environment.

Related Topic