1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ How's your PCB layout? It looks like you're just getting noise. \$\endgroup\$ – Hearth Feb 13 '19 at 14:20
  • \$\begingroup\$ I don't have PCB layout. It's all connecting with wires. \$\endgroup\$ – Electrician Feb 13 '19 at 14:23
  • \$\begingroup\$ 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? \$\endgroup\$ – Hearth Feb 13 '19 at 14:26
  • \$\begingroup\$ 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. \$\endgroup\$ – Electrician Feb 13 '19 at 14:30
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ +1; I had guessed something about voltage differences, but since the schematic had 5V coming from the ESP32 I assumed they were running on the same voltage. \$\endgroup\$ – Hearth Feb 13 '19 at 14:58
  • \$\begingroup\$ Well thanks for your reply. But it's 4096 units, and 2.5V / 3.3V x 4096 units = 3103. So the measurement is pretty far from the expected. It is make sense to calibrate manually with about 350 units to get the expected value? \$\endgroup\$ – Electrician Feb 15 '19 at 13:40
  • \$\begingroup\$ Indeed, my calculation is off. (Yours is almost correct: even though there are 4096 units, the maximum value is 4095. So you must multiply with 4095.) 350 units is indeed a lot and probably caused by several factors. Unfortunately, your design depends on the accurracy of the 5V supply, the ESP32's internal voltage reference, possibly the 3.3V voltage regular etc. Use a multimeter to measure the relevant voltages. Possibly there is some additional issue in the circuit. \$\endgroup\$ – Codo Feb 15 '19 at 14:07
  • 1
    \$\begingroup\$ The tolerances of the 5V supply and the 3.3Vref are not the main problem. The issue is the non-linearity of ESP32. Even if I'll calibrate it manually by adding some units to the analogRead reasult, it's won't be correct for different levels of voltage read by ADC. Couldn't find proper solution to the ADC non-linearity problem of ESP32. \$\endgroup\$ – Electrician Mar 26 '19 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.