Measure AC with SCT013, ADS1115 and ESP8266

If doesn't fit into any other category ....
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Measure AC with SCT013, ADS1115 and ESP8266

Post by Fernando Perez »

I have connected a split core current transformer model SCT013 to an ADS1115 converter, which communicates via I2C with an ESP8266 WEMOS D1 mini:
image.png
The SCT013 carries a built-in 55 ohm resistor and delivers 1 volt for a 30 amp current, supposedly in a very linear fashion:
image.png
I have configured the ADS1115 in differential mode, gain ± 1.024V and rate 250 SPS:
image.png
My problem is that the SCT013 provides an alternate voltage, not continuous, and I think that I am not interpreting the results correctly.
According to your datasheet, the ADS111x provide 16 bits of data in binary two's complement format. A positive full-scale (+FS) input produces an output code of 7FFFh and a negative full-scale (–FS) input produces an output code of 8000h.
This seems to mean that it is actually a 15-bit converter and that bit 16 represents the sign.
On the Internet I have seen somewhat confusing subroutines to measure AC voltages, for example:

Code: [Local Link Removed for Guests]

read_ADS:
  for n = 1 to 1024
    i2c.read_iobuff(0), &H48, 0, 2
    sample = iobuff.read(0,0) << 8 + iobuff.read(0,1)
    if sample > 32768 then sample = sample - 65536
    offset = (offset + (sample-offset) / 1024)
    filter = sample - offset
    sqV = filter * filter
    suma = suma + sqV
    Amp = sqr(suma/1024) * 0.0000625 * 30 * 1.33
  next n
  suma = 0    
return
But I think that it would be enough to select the maximum value obtained from a significant sampling:

Code: [Local Link Removed for Guests]

ADS1115_addr = &h48 : ADS1115_conv = 0 : ADS1115_conf = 1

i2c.setup 4, 5
iobuff.dim(0,2) = &B00000110, &B10100011 ' Mux = 0, Gain = 1.024V, Rate = 250 SPS, Comparator disable
i2c.write_iobuff(0), ADS1115_addr, ADS1115_conf

volt = 230
factor = 30   ' 30 Amp = 1 Volt
adjust = 704  ' empirically
tracker = 1

WHILE 1

  t= millis
  do while (millis - t) < 1000 

    if tracker = 1 then duration = millis

    Vmax = 0
    for n = 1 to 250
      i2c.read_iobuff(0), ADS1115_addr, ADS1115_conv, 2
      Vins = iobuff.read(0,0) << 8 + iobuff.read(0,1)
      if Vins >= 32768 then Vins = abs(Vins - 65536)
      if Vins > Vmax then Vmax = Vins
    next n
    
    Vmax = convert.map(Vmax, 0, 32767, 0, 1.024)
    Vmax = Vmax * adjust
    Amp = Vmax * factor
    Wat = (Amp * volt) / 1000

    if tracker = 1 then
      duration = millis - duration     
      wlog duration
      wlog time$, str$(Vmax, "%3.2f"); " mV", str$(Amp,"%3.2f"); " mA", str$(Wat,"%4.2f"); " W"
    end if

  loop

WEND
My questions are:
Am I correctly converting negative values and convert.map ()?
In general, are the concept and formulas that I use adequate?
I have obtained the value of the "adjust" variable by measuring the voltage at the terminals of the SCT013 using a digital multimeter on the millivolt scale true RMS.
And by connecting old (and currently banned) 230V (25w and 40w) incandescent bulbs, a hair dryer, a toaster, etc.
But for what is measured in the voltmeter to coincide with what is measured by the program, despite the correction factors, I am still missing something.
Can you think of something that can help me?
Thanks.
You do not have the required permissions to view the files attached to this post.
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by bugs »

Hi,

Just a quick point. I think you will have to use a potential divider to provide an input offset voltage as the ADS1115 cannot tolerate negative voltages below about 0.3V.
Connect the "ground" end of the coil to the junction of (say) two 4.7k resistors across the power supply and decouple with an electrolytic.

luck,
User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by Fernando Perez »

Something like this? But is it necessary if I use differential mode?
image.png
You do not have the required permissions to view the files attached to this post.
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by bugs »

No - not if your transformer secondary is completely isolated from ground - the -0.3v limit is common mode.
I probably had in mind the circuit I used to measure refrigerator consumption but (until I just checked the old program) I had forgotten I had changed to an ESP32 and was using the ADC directly instead of via an ADS1115.
I have used the ADS1115 but not in differential mode.
The program was derived from an Arduino system found on the web and calculated RMS current by taking readings as fast as possible.
I will try and re-locate the original Arduino article later.
EDIT:- it was here:- https://gist.github.com/AgustinParmisan ... de15f5f51a
User avatar
PeterN
Posts: 366
Joined: Mon Feb 08, 2021 7:56 pm
Location: Krefeld, Germany
Has thanked: 171 times
Been thanked: 203 times
Contact:

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by PeterN »

Sorry ... no solution but some thoughts:
The pure current probe will output an AC voltage proportional to the input current if a resistive load is connected.
To overcome the reading of the AC voltage with a slow ADC (or at least a too slow software sampling ) that can even only detect/read positiv Voltages , you can change that AC-output to a DC-signal by rectifying the AC.
Simple passive rectifiers with diodes have a threshold and a non-linear characteristic - but that could be linearized by the software and could even be an DC input signal to the ADC of an ESP8266 or ESP32.

Linear rectifiers need active components, but can amplify and shift the signal as needed. That makes the circuitry more complicated but more accurate.

I found this useful: https://electronics.stackexchange.com/q ... diode-drop
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by bugs »

Or add an AD736 8 pin RMS-DC chip.
Ok it needs a negative rail as well but could be easily done e.g-
https://circuitdigest.com/electronic-ci ... nd-arduino

.
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by bugs »

Going back to the first post. I have not used the current transformer specified so not sure about the output voltage. If they specify 1 volt then presumably this is 1 volt RMS. This means +-1.414 i.e 2.8 volt swing.
The +-1.024 setting for ADS1115 should be ok up to about 0.7 v RMS ( 2.048/2.8) so maybe just over 20A?

.
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by bugs »

I hooked up ESP and ADS1115 and did some tests.
First got it set reading DC volts from 3 nicads - see pic of screen and dvm.
Next downloaded the scope program to laptop and used the audio generator to output 50Hz from the sound card.
The laptop was running on batteries so no common ground to the ADS1115.
I made some alterations to your program (lines marked with "***") and trimmed out some bits to just give a voltage reading.
Your empirical adjust value was pretty close to the 0.707 required for the peak-to-rms conversion.
Other pics show dvm reading with the AC input.

Rather than changing the ADS1115 range (to +-2.048) and losing the resolution it might be better to add a further burden resistor to the current transformer output so that the maximum output is reduced sufficiently to fit in the +-1.024 range.
You do not have the required permissions to view the files attached to this post.
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by bugs »

And of course I forgot the program...

Code: [Local Link Removed for Guests]


CODE: xxxx.bas

--------------------------------------------------------------------------------



CODE: xxxx.bas

--------------------------------------------------------------------------------

ADS1115_addr = &h48 : ADS1115_conv = 0 : ADS1115_conf = 1

i2c.setup 4, 5
iobuff.dim(0,2) = &B00000010, &B10100011 ' Mux = 0, Gain = 2.048V, Rate = 250 SPS, Comparator disable  '****
i2c.write_iobuff(0), ADS1115_addr, ADS1115_conf

adjust=0.707 'peak to rms  '****

WHILE 1
  t= millis
  do while (millis - t) < 1000
    duration = millis
    Vmax = 0
    for n = 1 to 250
      i2c.read_iobuff(0), ADS1115_addr, ADS1115_conv, 2
      Vins = iobuff.read(0,0) << 8 + iobuff.read(0,1)
      if Vins >= 32768 then Vins = abs(Vins - 65536)
      if Vins > Vmax then Vmax = Vins
    next n    
    Vmax = convert.map(Vmax, 0, 32767, 0, 4.096) '****
    vmax=vmax*adjust
    wlog Vmax
  loop

WEND


User avatar
Fernando Perez
Posts: 378
Joined: Mon Feb 15, 2021 10:09 pm
Location: Santander (Spain)
Has thanked: 195 times
Been thanked: 267 times

Re: Measure AC with SCT013, ADS1115 and ESP8266

Post by Fernando Perez »

Dear PeterN, your phrase "The pure current probe will output an AC voltage proportional to the input current if a resistive load is connected", made me reflect that in a real situation of measurements in a house, I should take into account not only the intensity in amperes but also also the voltage in volts (which fluctuates throughout the day) and the phase difference (φ) between voltage and current generated by electrical appliances with inductive and capacitive elements.
It could measure the instantaneous voltage taking advantage of the second pair AIN2 and AIN3, in differential mode, and with a conventional transformer:
image.png
But, for the reasons that I explain to Bugs, I will only take a measurement of intensity.
You do not have the required permissions to view the files attached to this post.
Post Reply