ADC CT Power calculation on ESP32

1,814 views
Skip to first unread message

FransOv

unread,
Jan 29, 2021, 6:32:21 AM1/29/21
to TasmotaUsers
Will the calculation for ADC CT Power  work on an ESP32. I checked https://github.com/arendst/Tasmota/pull/7100 and see how I can change the parameter2 calculation to accommodate the ADC upper value of 4095 instead of 1023. But not how the take into account the higher value of the bias voltage of 1.65V.
Message has been deleted

FransOv

unread,
Jan 29, 2021, 7:36:47 AM1/29/21
to TasmotaUsers
I checked the code and see that it does not matter, as it is an alternating current. The code checks the upper and lower values in the samples, assuming it gets the top and the bottom of the sine wave that way. The difference is scaled by the factor.
So why am I getting values that are way off.


Philip Knowles

unread,
Jan 29, 2021, 8:16:15 AM1/29/21
to TasmotaUsers, FransOv
Most CTs have a built in resistor, etc so that you get a DC voltage output so there shouldn't be a 'bias voltage'. If there isn't I would use a bridge rectified to convert the output to DC. Then what you need to think about is 2 things what is the full scale output of the CT in Volts and how does that compare to the full scale of the ADC. It's easy enough to connect a known load say 100W incandescent bulb and measure the voltage at the ADC and then scale from there.

Regards

Phil K


From: sonof...@googlegroups.com <sonof...@googlegroups.com> on behalf of FransOv <frans.ove...@gmail.com>
Sent: Friday, January 29, 2021 12:36:47 PM
To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: ADC CT Power calculation on ESP32
 
I checked the code and see that it does not matter, as it is an alternating current. The code checks the upper and lower values in the samples, assuming it gets the top and the bottom of the sine wave that way. The difference is scaled by the factor.
So why am I getting values that are way off.


--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/62c2e4b5-ac44-420d-920c-4b67274ba154n%40googlegroups.com.

FransOv

unread,
Jan 30, 2021, 8:21:16 AM1/30/21
to TasmotaUsers

Hi Phil,

 

I always look for software solutions first before going the hardware way. Just because I’m more comfortable with software than with hardware.

The current transformers I use, SCT013-30A, deliver 1V at 30 Ampère. They do have a burden resistor built in, but no rectifier.

 I agree that it would be better to rectify the voltage and than input it to the ADC. However, bridge rectifiers have a forward voltage drop of up to 0.6V which makes them not suitable in this case. I would have to amplify the AC voltage 10x then rectify it and after that drop it down to a level that the ADC can handle.

 However your suggestion made me look  further and I found this solution to rectify the AC with only a very small (0.03V) forward voltage drop. It comes from a publication at https://arxiv.org/vc/arxiv/papers/1205/1205.4604v1.pdf.

Transistor based rectifier.jpg

I’m going to try this, unless someone can suggest a simpler solution.
Op vrijdag 29 januari 2021 om 14:16:15 UTC+1 schreef knowles...@gmail.com:

Justin Adie

unread,
Jan 30, 2021, 9:39:00 AM1/30/21
to FransOv, TasmotaUsers
When you say that your values are way off what are you seeing vs expecting?  




FransOv

unread,
Jan 31, 2021, 10:37:16 AM1/31/21
to TasmotaUsers
With ADC CT Power, I do not get the results I expect.

The code of xsns_02_analog.ino at lines 328 to 340 every second gets 32 samples from the ADC and records the maximum and minimum value from those samples. It then calculates the current as (max-min)*adcparam_factor/100000. 
line 339: Adc[idx].current = (float)(analog_max-analog_min) * ((float)(Adc[idx].param2) / 100000)

For a test I used a load of about 1000W (4.25A) and attached a scope to the CT013. For ease of calculation I set the factor to 1000. The scope shows that peak to peak voltage at the ADC input is about 500 mV. The web interface of the ESP32 shows a current varying from 6.2 to 6.8 A. If the measurement of the peak to peak voltage by the 32 samples was correct, I would expect something like 5A.

The conclusion, I think is, that you do not get a reliable peak to peak voltage this way. Thus rectifying the input before connecting it to the ADC seems to be the better way. At this voltage level, rectifying is not going to be easy. In addition to the above circuit I need to invert the signal and to smooth the ripple. The full wave rectifier based on a transistor as specified in  the above publication is rather complex.
I'll check for an integrated solution. Digital circuits I can understand to a certain degree, analog circuits not so much.

CT013 output at 1000W.jpg
CT-013 test.jpg

 

Op zaterdag 30 januari 2021 om 15:39:00 UTC+1 schreef justi...@adieandco.com:

Philip Knowles

unread,
Jan 31, 2021, 11:08:20 AM1/31/21
to FransOv, TasmotaUsers

Have you adjusted AdcParam 7?

  7 = CT POWER parameter adjustments:

<param1> = ANALOG_CT_FLAGS (default 0 for a non-invasive current sensor). When value is >0 its sets the adcLow value as base for the measurement via OpAmp differential amplifier.
<param2> = ANALOG_CT_MULTIPLIER ( 2146 = Default settings for a (AC) 20A/1V Current Transformer.) multiplier*100000 to convert raw ADC peak to peak range 0..1023 to RMS current in Amps. Value of 100000 corresponds to 1
<param3> = ANALOG_CT_VOLTAGE (default 2300) to convert current in Amps to apparent power in Watts using voltage in Volts*10. Value of 2200 corresponds to AC220V. For DC its Volt/1000. Eg. 12VDC = 0.012.
AdcParam 7,406,3282,0.012

 

 

Sent from Mail for Windows 10

 

From: FransOv
Sent: 31 January 2021 15:37
To: TasmotaUsers
Subject: Re: ADC CT Power calculation on ESP32

 

With ADC CT Power, I do not get the results I expect.

 

The code of xsns_02_analog.ino at lines 328 to 340 every second gets 32 samples from the ADC and records the maximum and minimum value from those samples. It then calculates the current as (max-min)*adcparam_factor/100000. 

line 339: Adc[idx].current = (float)(analog_max-analog_min) * ((float)(Adc[idx].param2) / 100000)

 

For a test I used a load of about 1000W (4.25A) and attached a scope to the CT013. For ease of calculation I set the factor to 1000. The scope shows that peak to peak voltage at the ADC input is about 500 mV. The web interface of the ESP32 shows a current varying from 6.2 to 6.8 A. If the measurement of the peak to peak voltage by the 32 samples was correct, I would expect something like 5A.

 

The conclusion, I think is, that you do not get a reliable peak to peak voltage this way. Thus rectifying the input before connecting it to the ADC seems to be the better way. At this voltage level, rectifying is not going to be easy. In addition to the above circuit I need to invert the signal and to smooth the ripple. The full wave rectifier based on a transistor as specified in  the above publication is rather complex.

I'll check for an integrated solution. Digital circuits I can understand to a certain degree, analog circuits not so much.

 

 

 

Op zaterdag 30 januari 2021 om 15:39:00 UTC+1 schreef justi...@adieandco.com:

When you say that your values are way off what are you seeing vs expecting?  

 

 

 

On Sat, 30 Jan 2021, 14:21 FransOv, <frans.ove...@gmail.com> wrote:

 

Hi Phil,

 

I always look for software solutions first before going the hardware way. Just because I’m more comfortable with software than with hardware.

The current transformers I use, SCT013-30A, deliver 1V at 30 Ampère. They do have a burden resistor built in, but no rectifier.

 I agree that it would be better to rectify the voltage and than input it to the ADC. However, bridge rectifiers have a forward voltage drop of up to 0.6V which makes them not suitable in this case. I would have to amplify the AC voltage 10x then rectify it and after that drop it down to a level that the ADC can handle.

 However your suggestion made me look  further and I found this solution to rectify the AC with only a very small (0.03V) forward voltage drop. It comes from a publication at https://arxiv.org/vc/arxiv/papers/1205/1205.4604v1.pdf.

I’m going to try this, unless someone can suggest a simpler solution.

Op vrijdag 29 januari 2021 om 14:16:15 UTC+1 schreef knowles..@gmail.com:

Most CTs have a built in resistor, etc so that you get a DC voltage output so there shouldn't be a 'bias voltage'. If there isn't I would use a bridge rectified to convert the output to DC. Then what you need to think about is 2 things what is the full scale output of the CT in Volts and how does that compare to the full scale of the ADC. It's easy enough to connect a known load say 100W incandescent bulb and measure the voltage at the ADC and then scale from there.

Regards

Phil K

From: sonof...@googlegroups.com <sonof...@googlegroups.com> on behalf of FransOv <frans.ove...@gmail.com>
Sent: Friday, January 29, 2021 12:36:47 PM
To: TasmotaUsers <sonof...@googlegroups.com>
Subject: Re: ADC CT Power calculation on ESP32

 

I checked the code and see that it does not matter, as it is an alternating current. The code checks the upper and lower values in the samples, assuming it gets the top and the bottom of the sine wave that way. The difference is scaled by the factor.

So why am I getting values that are way off.

 

 

--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/62c2e4b5-ac44-420d-920c-4b67274ba154n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.

FransOv

unread,
Jan 31, 2021, 11:23:02 AM1/31/21
to TasmotaUsers
Yes, I did: {"AdcParam1":[7,0,1000,0.23]}
I'm checking for an integrated solution. I found an AD636 Low Level, True RMS-to-DC Converter that at first glance seems to be able to do the job. 

Op zondag 31 januari 2021 om 17:08:20 UTC+1 schreef knowles...@gmail.com:

Philip Knowles

unread,
Jan 31, 2021, 11:30:39 AM1/31/21
to FransOv, TasmotaUsers

What happens if you try

AdcParam 7,0,1073,0.23

Op zaterdag 30 januari 2021 om 15:39:00 UTC+1 schreef justi...@adieandco.com:

FransOv

unread,
Feb 1, 2021, 4:33:00 AM2/1/21
to TasmotaUsers
I tried, it only raises the amperage a bit. The root cause of the inaccuracy seems to be measuring the peak to peak voltage of the ac, changing the parameter does not affect that.
I found a cheap RMS to DC Converter module based on the LTC1966 chip on Aliexpress that looks promising (https://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20210201000221&SearchText=ltc1966). I ordered a couple. As soon as I receive them, will be a week or two, I'll report on whether they will do the job.


 

Tom Lafleur

unread,
Feb 1, 2021, 8:02:11 AM2/1/21
to FransOv, TasmotaUsers
https://cdn.hackaday.io/files/9676413977984/current%20sensing%20circuit%20collection.pdf

See app note 105, page 36

~~ _/) ~~~~ _/) ~~~~ _/) ~~~~ _/) ~~

Tom Lafleur

On Feb 1, 2021, at 1:33 AM, FransOv <frans.ove...@gmail.com> wrote:

I tried, it only raises the amperage a bit. The root cause of the inaccuracy seems to be measuring the peak to peak voltage of the ac, changing the parameter does not affect that.
I found a cheap RMS to DC Converter module based on the LTC1966 chip on Aliexpress that looks promising (https://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20210201000221&SearchText=ltc1966). I ordered a couple. As soon as I receive them, will be a week or two, I'll report on whether they will do the job.


 

--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.

FransOv

unread,
Feb 1, 2021, 9:38:04 AM2/1/21
to TasmotaUsers
The module has all the components of the application note on board. So no external components needed apart from the SCT013 current transformer. Makes me happy, no messy soldering required.
LTC1966 application note.png



Tom Lafleur

unread,
Feb 1, 2021, 12:15:21 PM2/1/21
to FransOv, TasmotaUsers
you will need to adjust the burden resistor value to stay within the limits of the chip's input, and if you read the datasheet you will see the need for the bias supply on the input

~~ _/) ~~~~ _/) ~~~~ _/) ~~~~ _/) ~~

Tom Lafleur


On Mon, Feb 1, 2021 at 6:38 AM FransOv <frans.ove...@gmail.com> wrote:
The module has all the components of the application note on board. So no external components needed apart from the SCT013 current transformer. Makes me happy, no messy soldering required.
LTC1966 application note.png



--
You received this message because you are subscribed to the Google Groups "TasmotaUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers...@googlegroups.com.

FransOv

unread,
Feb 1, 2021, 1:36:44 PM2/1/21
to TasmotaUsers
@Tom
Thanks for the hints . I checked, the burden resistor is  built in the current transformer and configured to to deliver 1V at 30 amps. The load on the ac circuit will never be larger than 15-20 amps. So that will not be a problem. The bias supply is on the module, same resistor values. It looks like they took the application note as the specification for the module.

Op maandag 1 februari 2021 om 18:15:21 UTC+1 schreef laf...@lafleur.us:

FransOv

unread,
Feb 22, 2021, 9:24:55 AM2/22/21
to TasmotaUsers
I tested the LTC1966 module that I received last week. I needed to add an extra capacitor between the SCT013 and the LTC1966 module, but otherwise it seems to work well. It's a pity that they tied ROUT to ground on the module as otherwise I could have shifted the output to the middle of the 3.3V of the ADC. For small loads, less then 200W, the input for the ADC is very low and in the region where the ADC is not at all linear. To partially compensate for this,  I changed the attenuation from the standard 11dB for the Esp32 to 0dB, giving a range of 1V for the ADC.
Below the output of the SCT013, the LTC1966 and the resulting value of the ADC at four different loads and a picture of the LTC1966 module:
SCT013-LTC1966 output.png  LTC1966 module.jpg


Op maandag 1 februari 2021 om 19:36:44 UTC+1 schreef FransOv:

FransOv

unread,
Feb 22, 2021, 9:27:51 AM2/22/21
to TasmotaUsers
Sorry I got the access titles on the graph wrong. Left is the Adc Value, right axis is the mV value.

Op maandag 22 februari 2021 om 15:24:55 UTC+1 schreef FransOv:
Reply all
Reply to author
Forward
0 new messages