ACS712 AC Current sensor not sensing anything

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
easylab4kids
Posts: 20
Joined: Mon Aug 12, 2019 10:58 am
Location: Ellisras, Suid-Afrika ("South Africa")

ACS712 AC Current sensor not sensing anything

Post by easylab4kids » Sat Nov 06, 2021 4:43 pm

My ESp8266 seems unable to sense any current. The ADC value is 765, regardless if I put. a load on it or not.
When I bring a magnet close to the ACS712 30A-versin the read() value changes dramatically.

I live in Europa with 230V AC mains, and use a 2-wired connector blue&brown which doesn't care about ground-polarity.

Code: Select all

from machine import Pin, ADC
import utime

adc = ADC(0)
for counter in range(0, 200):
  print("A0.read()=", add.read())
  time.sleep_ms(0.1)

print("Program End")

With NO load readings:
A0.read()= 712
A0.read()= 715
A0.read()= 708
A0.read()= 713
A0.read()= 726
A0.read()= 727
A0.read()= 708
A0.read()= 724
A0.read()= 706
A0.read()= 720
A0.read()= 727
A0.read()= 710
A0.read()= 710
A0.read()= 725
Even with load there's no change in readings.
According tot Google with no load analog value should be (1024 / 2) = 512. Thats not the value measured without load. I tried also emonlib but thats no use too..

Am I using a too small load?

Wiring is like this, EXCUSE for showing an Arduino.
One wire goes from the wall connector to the load (LED light), the other routed via the ACS712.
Image

Anyone else have experience?
Last edited by easylab4kids on Wed Nov 17, 2021 6:51 am, edited 3 times in total.
met vriendelijke groet,
Easylab4kids.nl

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ACS712 AC Current sensor not sensing anything

Post by Roberthh » Sun Nov 07, 2021 7:32 am

The ACS712 is a current Sensor, not specifically for AC. Since the no-current reading is specified as Vcc/2 in the middle of the range, you can read current of both polarities. But it does not read an AC current per se. If you connect it to an AC current of 50Hz, the polarity will change 50 times per second. So to sense AC, you have either to sample a few hundred input values fast enough (>200 sample/sec) and feed that through a signal analysis algorithm to get the amplitude of the 50Hz signal, or you have to rectify the output signal of the ACS712, as shown in the data sheet in the "Typical Applications" section.
About the reading you get:
I do not know how you connect the sensor and which ESP8266 board you use. Let's assume you run the sensor at 5V, and the board has an input divider such that the ADC range is 0..3.3V. Then, the quiet output voltage of the AC712 is 2.5V, and the reading of the ESP8266 should be about 2.5/3.3 * 1024 = 775. That's what you get. So much about the no-current-reading, that you see.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ACS712 AC Current sensor not sensing anything

Post by Roberthh » Sun Nov 07, 2021 7:41 am

Just a warning I should add:
Most ACS712 breakout boards that I have seen are NOT electrically safe. While the isolation of the ACS712 itself is good, the boards do not meet the requirements for safe design. They do not route the tracks carefully, such that the required distance between tracks of the high voltage side and the low voltage side gets too small for a good isolation. Needless to say that there are no trenches in the PCB between the high and low voltage side, preventing leakage currents.

easylab4kids
Posts: 20
Joined: Mon Aug 12, 2019 10:58 am
Location: Ellisras, Suid-Afrika ("South Africa")

Re: ACS712 AC Current sensor not sensing anything

Post by easylab4kids » Thu Nov 11, 2021 10:41 pm

I'm not getting anywhere. I've watched countless YouTube vids where everyone seems to have the ACS712 working and reading current.
the quiet output voltage of the AC712 is 2.5V, and the reading of the ESP8266 should be about 2.5/3.3 * 1024 = 775. That's what you get
However, apparently one needs to account for the version of the ACS712 begin sued, I've got the 30A-version PartNr ACS712ELCTR-05B-T.
Accordingly one needs to take 0.185mV into account. At least, thats what Youtbebers do.

My formula is:
analogVal = A0.read()
volt= analogVal / 1024 * 3.3) * 0.185
Regardless of having a load or not, the read() value stays consistent unchanged .
A0.read()= 744 volt= 0.443566
A0.read()= 745 volt= 0.444162
A0.read()= 744 volt= 0.443566
A0.read()= 744 volt= 0.443566
A0.read()= 744 volt= 0.443566
A0.read()= 744 volt= 0.443566
A0.read()= 744 volt= 0.443566
A0.read()= 744 volt= 0.443566
A0.read()= 744 volt= 0.443566
Can anyone make sense of tis, or at least point me in a direction?
met vriendelijke groet,
Easylab4kids.nl

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ACS712 AC Current sensor not sensing anything

Post by Roberthh » Fri Nov 12, 2021 8:07 am

You have to understand and manage two aspects:
a) Voltage levels.
The ESP8266 ADC has an input range of 0..1V. The ACS712 sensor at a Vcc of 5V has an output range of 0..5V. That has to be adapted, which consists of two steps: You ESP8266 board may have already have an voltage divider that changes the boards ADC input range to 0..3.3V. So it is interesting, which board you have, to make the proper input range adaption for 0..5V to the board's range. If we know the board, we can deal with other issues as well.
b) Signal type.
The output signal of the ACS712 is a AC signal with the major component being the mains frequency, added to a DC signal of ACS712_Vcc/2 (2.5 V). You want to know the current going through the sensor. Therefore you have to determine the amplitude of the AC part. You can do that in software by spectral analysis (FFT or Goertzel Algorithm), or you can use hardware to turn the AC signal into a DC offset. A diode plus capacitor would do that.

So to start with a simple question: Which ESP8266 board do you use?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ACS712 AC Current sensor not sensing anything

Post by pythoncoder » Sun Nov 14, 2021 10:51 am

Roberthh wrote:
Fri Nov 12, 2021 8:07 am
...Therefore you have to determine the amplitude of the AC part. You can do that in software by spectral analysis (FFT or Goertzel Algorithm)...
Assuming a reasonable sinewave an alternative approach is to create an array comprising samples from an integer number of cycles. Then remove the DC component: calculate the mean and subtract that value from each sample. Finally calculate the RMS value: square each value, add them together, divide by the number of samples and take the square root.

My power meter uses this approach and I found it quite accurate.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ACS712 AC Current sensor not sensing anything

Post by Roberthh » Sun Nov 14, 2021 1:07 pm

Assuming a reasonable sinewave
That's may get the problem. I tried a similar approach for a while using an ESP32 board, where the ADC is comparable noisy. For a large signal, the RMS approach worked. When the signal got smaller, it failed. The poster uses the 30A version of the sensor. If the smallest current to be measures is about 5 A, RMS can work. Fro smaller values, you have to filter out the noise. Obviously that leads to another problem with the goertzel approach, which just probes a single frequency. If you take sufficient many samples, you can determine pretty small signals. But the bandwidth gets very small, leading to errors when the mains frequency changes. The usual fluctuations of +/-0.02 Hz up to +/- 0.2 Hz will change the reading.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ACS712 AC Current sensor not sensing anything

Post by pythoncoder » Sun Nov 14, 2021 4:49 pm

I cheat by using a Pyboard 1.1 which has decent ADC's. One measures voltage, the other current. The voltage is a good sine wave, but current often isn't. The voltage is of course an almost constant RMS value and I can get accurate zero crossing points, which removes the issue of frequency errors. I also have a switched gain amplifier on the current input to provide multiple ranges - but even on the highest 15 amp range the measurement of low powers is surprisingly accurate.

The point is that if you can analyse the current waveform synchronously, over a precise N cycles, the calculation of the RMS value rejects noise and allows for distortion in the waveform. A narrowband filter (e.g. Goertzel) will reject noise, but will also discard harmonics which do contribute to the RMS value, sometimes substantially. For example old style linear PSU's often had a current waveform that was a pulse as the bridge rectifier conducted only near the peaks of voltage. Goertzel would discard most of the energy in this pulse, giving an incorrect reading.

As an example of this, I worked on a project with a linear 1KW PSU. When it was commissioned, the engineers noticed that the wiring feeding it was getting hot. The wiring had been designed on the assumption of a sinusoidal current. The actual current had an RMS value many times that of a 1KW sine wave.
Peter Hinch
Index to my micropython libraries.

easylab4kids
Posts: 20
Joined: Mon Aug 12, 2019 10:58 am
Location: Ellisras, Suid-Afrika ("South Africa")

Re: ACS712 AC Current sensor not sensing anything

Post by easylab4kids » Sun Nov 14, 2021 4:57 pm

My main concern now is I dont get any adc.read()-value fluctuations with or without load its always 745, when activating a 10W led lamp has no effect, the ADC remains in 745,746 range.
Which ESP8266 board do you use?
- ESP8266 D1 Mini with Micropython 1.17.
- ACS712 60A version "ACS712ELCTR-30A-T" with sensitivity 0,66. (correction from previous post!)
create an array comprising samples from an integer number of cycles.
Then remove the DC component: calculate the mean and subtract that value from each sample.
Which translates in the below code, if I have it correct?

Code: Select all

# This code calculates a mean value every 10th cycle
from machine import Pin, ADC
import time

print("Program start")
endCounter = 2010
arValues = []
valTotal = 0
adc = ADC(0)

for counter in range(1, endCounter):
  analogVal = abc.read()
  volt= analogVal / 1024 * 3.3) * 0.66
  time.sleep(0.02)
  valTotal += volt
  arValues.append(volt)

  #Display the 
  if (counter % 30 = 0):
   print(counter, "A0.read()="m counter)

    arValues = []
    valTotal = 0

print("Program End")
The resulting output is.
30 A0.read()= 745 volt= 1.58458
60 A0.read()= 745 volt= 1.58458
90 A0.read()= 744 volt= 1.58245
120 A0.read()= 744 volt= 1.58245
150 A0.read()= 744 volt= 1.58245
180 A0.read()= 745 volt= 1.58458
210 A0.read()= 744 volt= 1.58245
240 A0.read()= 745 volt= 1.58458
270 A0.read()= 745 volt= 1.58458
300 A0.read()= 744 volt= 1.58245
As one can see I get a near consistent 745 reading, weather there is a load or not. The change to 744 us spontaneous.
Bringing a magnet closeby to the sensor causes spikes & peaks in above read().
The poster uses the 30A version of the sensor. If the smallest current to be measures is about 5 A
Make me wonder, if using a lower amps version, such as the ACS712 ParNr ACS712ELCTR-05B-T, would that make a differnce.
Last edited by easylab4kids on Sun Nov 14, 2021 5:47 pm, edited 5 times in total.
met vriendelijke groet,
Easylab4kids.nl

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ACS712 AC Current sensor not sensing anything

Post by Roberthh » Sun Nov 14, 2021 5:26 pm

10W with a 30A sensor at 230V would create a maximum reading change of the ADC of 2 digits,like from 745 to 747, if you happen to pick the right moment. That's defifitely within the noise floor of the ADC. What happens if you connect a higher load, like a 1000W heater of hair dryer. That shoud have a more significant change.
The 5A sensor has a six times larger output variation at the same current.

Post Reply