Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for apparent power calculation with current transformer connected at A0 #7100

Closed
wants to merge 10 commits into from

Conversation

DillonJ
Copy link

@DillonJ DillonJ commented Dec 4, 2019

Description:

This implements support for a simple AC apparent power monitor using a current transformer connected to the analog input A0. The full circuit details can be found here but in summary, you must have a CT with an applied DC bias connected to A0 so the raw DC reading is near 512 (for zero input) and an appropriate burden resistor such that the peak to peak voltage does not exceed 1V or 3.3V depending on whether or not your board has a voltage divider at A0.

The code measures the peak to peak range of the ADC input signal over 32 samples at 1ms intervals which is plenty to calculate the peak to peak range of a 50Hz or 60Hz signal.

You supply the appropriate scaling factor via adc_param2 to convert the raw adc range (0..1023) to the RMS current. Details of the relevant calculations are here. For a current transformer and burden resistor that reads 1V RMS for 20A RMS input current, the calculations are as follows:
1V RMS = sqrt(2)x1=1.41V peak to peak. Apply DC bias of 3.3/2 = 1.65 => max input voltage of 3.06V for 20A RMS input current. Therefore, the appropriate scaling factor is (20*(3.3/3.06))/1024=0.021063.

The adc_params must be integers, so this is multiplied by 100000 to give 2106 and is supplied via adc_param2.

Using a voltage supplied via adc_param3 in tenths of volts (so 2205 for 220.5 V) this is converted to apparent power.

A running energy total is calculated and displayed. At the moment, this can be reset by sending a value of 1 for adc_param1 which then gets reset back to zero.

The code

Related issue (if applicable): fixes #
Implements feature request: #7092

Checklist:

  • The pull request is done against the latest dev branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR.
  • The code change is tested and works on core 2.6.1
  • The code change pass travis tests. Your PR cannot be merged unless tests pass
  • I accept the CLA.

Implements calculations for CT based current measurement.
ADC peak peak range is calculated over 32 samples and this is converted to an RMS current reading using adc_param2 as a scaling factor
Power is calculated using current as above and a voltage provided via adc_param3
adc_param1 allows display of current (0), power (1), both (2) or none (> 2).
Updated comments for scaling factors
@DillonJ DillonJ changed the title Dev ct on a0 Support for apparent power calculation with current transformer connected at A0 Dec 4, 2019
@digiblur
Copy link
Contributor

digiblur commented Dec 4, 2019

Nice! Looking forward to seeing this one.

@arendst
Copy link
Owner

arendst commented Dec 4, 2019

For my understanding.

You measure the voltage once and store it fixed in a adc parameter. Then you measure the current using the ADC input and apply a formule to calculate the apparent power which is equal to true power.

It does not expect or calculate reactive or capacitive loads as it cannot measure phase shifts.

So only AC resistive loads will provide valid power values.

Correct?

@DillonJ
Copy link
Author

DillonJ commented Dec 4, 2019

For my understanding.

You measure the voltage once and store it fixed in a adc parameter. Then you measure the current using the ADC input and apply a formule to calculate the apparent power which is equal to true power.

It does not expect or calculate reactive or capacitive loads as it cannot measure phase shifts.

So only AC resistive loads will provide valid power values.

Correct?

Yes, correct. It's not intended to be a high quality precision measurement. In my case, I have a PZEM connected elsewhere from which I can send the voltage measurement - and in any event, from measurements I have taken over long periods of time, the voltage doesn't vary that much.

Using the scaling parameter, you could correct for power factor if the nature of the load is known.

The intention here is to provide an estimate of power using a clip on current transformer where precision isn't that important.

@digiblur
Copy link
Contributor

digiblur commented Dec 5, 2019

Perfect for those "is the clothes dryer running or not?" I did a build of the CT clamp on ESPHome a while back when they started supported it, always good to have options and do it in Tasmota too.

@arendst
Copy link
Owner

arendst commented Dec 6, 2019

Can we decide that this solution will be used as the only power monitoring feature on a device?

If yes, then I want it to integrate with the energy monitoring code to leverage the energy code and shrink code size too.

@arendst arendst self-assigned this Dec 6, 2019
@arendst arendst added the Awaiting feedback from Project Owner Awaiting feedback / input from Project Owner label Dec 6, 2019
@meingraham
Copy link
Collaborator

@arendst Seems fair to me. If one is choosing the "cheap" route for monitoring energy, I would not think they would invest in a separate energy monitor to add to the device. And, if the device is already a power monitoring device, Tasmota already discouraging adding any external peripherals.

@DillonJ
Copy link
Author

DillonJ commented Dec 6, 2019

I actually have a PZEM 004T connected to a sonoff communicating over the serial connection that works nicely... and in fact I am sending the voltage from this device over to the device with the CT to use in the power calculation.

So it would seem a pity to limit people to this solution, particularly since it is just calculating apparent power and doesn't calculate the power factor.

@meingraham
Copy link
Collaborator

@DillonJ

What I understood Theo to be suggesting is to "limit" a particular Tasmota device to have connected to it only one energy monitoring peripheral at most. In your case, one Tasmota device has the PZEM attached and reads the voltage and the current. On another Tasmota device you have this A0 current monitor and it receives the voltage measurement from the other device (the one with the PZEM). I think this fits what Theo suggests as each Tasmota device only has one energy monitor attached.

@arendst
Copy link
Owner

arendst commented Dec 6, 2019

It also allows for code re-use. The display of values could be done by the energy code already present. No need for another Voltage, Current or Power translation

@DillonJ
Copy link
Author

DillonJ commented Dec 6, 2019

Ah, ok, I get you now. I suppose you could conceive a use case where someone has a PZEM connected and also uses a CT connected to A0 on the same device as a second power channel and rides on the back of the PZEM to get power factor and voltage.... but that is quite specific.

@DillonJ
Copy link
Author

DillonJ commented Dec 6, 2019

If we went that route - how would the interfacing with adc_params work?

@arendst
Copy link
Owner

arendst commented Dec 6, 2019

Don't worry. I'll integrade your PR one of these days.

@arendst arendst added on hold by dev team Result - Feature request put on hold by member of development team and removed Awaiting feedback from Project Owner Awaiting feedback / input from Project Owner labels Dec 17, 2019
@arendst arendst added this to the v8.2.0 milestone Dec 17, 2019
arendst added a commit that referenced this pull request Dec 30, 2019
- Bump version to 8.1.0.2
- Add support for ``AdcParam`` parameters to control ADC0 Current Transformer Apparent Power formula by Jodi Dillon (#7100)
@arendst arendst removed the on hold by dev team Result - Feature request put on hold by member of development team label Dec 30, 2019
@arendst
Copy link
Owner

arendst commented Dec 30, 2019

Thx for your input.

I've merged it in the latest dev branch my way.

Pls give it a try as I do not have the CT handy ;-)

@stackner
Copy link

stackner commented Feb 2, 2020

u can not send calibration commands like powerset i get unknown command responce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants