How to measure on a PWM load? - Page 1
Forum section: electronics / projects

author: zapta
author_rank: Super Contributor
timestamp: October 09, 2023, 05:59:52 pm
content: I would like to measure the power of a PWM load such that the MCU gets real time power readings.* Load power rail is in 12VDC or 24VDC.* Typical PWM frequency is 500Hz.* Support for loads of up to let's say 60W.* Loads can be PTC with significant resistance change over temperature, such that measuring duty cycle is insufficient.* Having real time power readings, let's say 10 - 50 times a second, to a STM32H7xx MCU.* The measurement circuit is a path-through with two wires in and two wires out. No access to other signals or information if the PWM switching is on the high or low rails.Ignoring practical issues such as grounds and isolation, what is a reasonable way to approach it? I performed simulations of straight forward current and voltage samplings but am getting significant noise due to interference between the PWM and the sampling frequencies. Another approach is to have RMS-to-DC such as AD737 before the current and voltage sampling ADC.Any comment/idea would be greatly appreciated.

author: bdunham7
author_rank: Super Contributor
timestamp: October 09, 2023, 06:10:12 pm
content: I think doing RMS conversions of each is problematic unless you can guarantee that the load is perfectly resistive, but a using a multiplier amp like the AD633 might work. How accurate do you expect this setup to be?

author: zapta
author_rank: Super Contributor
timestamp: October 09, 2023, 06:18:06 pm
content: @bdunham71. The load is expected to be mostly resistive (it's a heater)2. Any problem with sampling the two RMS-to-DC independently and then multiplying in software? Instead of an analog multiplier. The time between current and voltage ADC sampling can be kept for minimal.3. No hard accuracy requirements but my goal is to get as better as possible using a 'reasonable design'. E.g. 1% of full scale. I plan to calibrate the current and voltage readings.4. I presume that the momentary power is the product of Vrms and Irms. is it so?

author: bdunham7
author_rank: Super Contributor
timestamp: October 09, 2023, 06:25:40 pm
content: Quote from: zapta on October 09, 2023, 06:18:06 pmThere's the problem. RMS is of necessity not a momentary or instantaneous value, but rather by definition integrated over a period of time. Instantaneous power is simply V * I. Power over a period of time is VRMS* IRMS* PF (Power Factor). Your method would assume that PF = 1.00 and your power readings would not be truly instantaneous but rather averaged over some period of time that would be determined by the size of the integration capacitor used with the AD737. That last issue is probably OK if your integration time is on the order of a few sample periods.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 09, 2023, 06:27:03 pm
content: Thought not sure if this is applicable here:Quote from: zapta on October 09, 2023, 05:59:52 pmBeing resistive loads, simply read the current sensor just after the PWM cycle has begun, the ADC must be fast enough for the smallest PWM duty possible, disable any averaging, you want the instanteous reading.Knowing the pwm duty cycle and the voltage, the rest is just basic math (P = V * I * PWM %).Maybe you can set full power for a single PWM cycle every few seconds, this would be 2ms, hopefully enough for the ADC.Another way would be to use shunts, op-amps and the STM32 ADC. That's fast enough for sure!Maybe a hall current sensor? Not sure if they can react fast enough, never used them.

author: zapta
author_rank: Super Contributor
timestamp: October 09, 2023, 06:47:45 pm
content: @bdunham7, the averaging will be at a slower rate than the underlying PWM such that the ADC rate will be at the order of the PWM frequency or lower, very achievable. And yes, assuming PF of 1. Will confirm that assumption with lab measurements.What do you think about this approach:1. Feed the current sensor through peak detector and sample with ADC2. Measure the duty cycle accurately with timers,3. Compute I RMS from 1 and 2, e.g. for each PWM cycle.

author: zapta
author_rank: Super Contributor
timestamp: October 09, 2023, 06:56:29 pm
content: @DavidAlfa, if I understand correctly you suggest to measure the duty cycle independently and accurately, which can probably done with the MCU timers.As for on current, since it doesn't changes fast (depends on temperature, assuming stable DC rail), I wonder if I can just sample it at let's say x10 of the PWM rate, and use the last sampling that hit a on-current as the multiple for the PWM cycle. In other words, I don't even have to hit each short pulse, just occasionally, to estimate the on-current.BTW, I also have the option to record that sampled data and process later by a batch program so will be able to propagate on-current also from future hits, not just from past hits.As for sensor bandwidth this is not an issue, with shunt or with magnetic current DC sensors that come with 80Khz and even with 1Mhz bandwidth. At this stage, my design dilemma is mainly on the measurement methodology and not the actual design.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 09, 2023, 07:20:28 pm
content: Ah, isn't the PWM generated by the MCU itself? Yeah it could be done anyways with a capture input.

author: zapta
author_rank: Super Contributor
timestamp: October 09, 2023, 08:08:00 pm
content: @DavidAlfa, no, the PWM is generated by a black box. The only access available is a path through on the two wires between the blackbox and the resistive load.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 09, 2023, 08:15:10 pm
content: Ah, sorry I got it wrong! Yeah I think measuring the pwm width would be a simpler method to measure the power.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: October 09, 2023, 08:25:45 pm
content: Quote from: zapta on October 09, 2023, 06:56:29 pmIf you have fast enough sampling of V and I, you do not need RMS, if you detect the ON time.You can simply sample over the whole PWM period, and 30% may be 0 power and 70% max power, and the average power is easily derived.You could sync the average window with the PWM, to reduce the quanta effects, tho if it is a heater, your update rate is likely quite slow ?5 readings a second, will be 100 PWM cycles.

author: tszaboo
author_rank: Super Contributor
timestamp: October 09, 2023, 10:21:15 pm
content: Place a shunt or a hall sensor to measure current.Then I would route it into a power meter ASIC and forget sampling it quick. Let it do the multiplication and integration.

author: zapta
author_rank: Super Contributor
timestamp: October 09, 2023, 11:06:54 pm
content: @tszaboo, can you point me to such a "power meter ASIC" ?

author: tszaboo
author_rank: Super Contributor
timestamp: October 09, 2023, 11:17:26 pm
content: Quote from: zapta on October 09, 2023, 11:06:54 pmSure, for example from AD:https://www.analog.com/en/parametricsearch/10577#/

author: zapta
author_rank: Super Contributor
timestamp: October 10, 2023, 03:39:17 am
content: Thanks @tszabooThis ICs seem to be intended for AC power, and probably sine wave (?).Is any of those ICs cable of measuring power in A PWM circuit that toggles between 0 and some DC level?EDIT: Looking at this one, it seems to expect input line frequency of count 50-60Hz.https://www.analog.com/media/en/technical-documentation/data-sheets/ade9153a.pdf

author: duak
author_rank: Super Contributor
timestamp: October 10, 2023, 04:06:22 am
content: After I retired and people thought I had time on my hands, I designed a real power meter for a start up company. They were developing a Stirling engine with a 500 W output at 120 VAC at 500 Hz and needed to measure the output power. I whipped up an instrument to do so. It works by multiplying the instantaneous voltage by the instantaneous current with a four quadrant multiplier and then filtering or averaging the product. I've attached a pdf of the schematic of the guts of it. The only CAD system I had available was a freeware 2D package, so WYSIWYG.It should be self explanatory. Here's a few notes:- The components in the input filters are matched to improve common mode rejection at high frequencies.- The multiplier is good for a few megahertz but the INA and input filters may have to be changed to handle the harmonics of the PWM waveform.- Since the voltage is taken across the load resistor, any inductance in the resistor or wiring could cause an error.- The current sensor is an LEM or Microswitch nulling hall effect device. In retrospect, the load voltage should be taken after the current sensor, in case it introduces any series inductance.Hope this gives you a few ideas,

author: PCB.Wiz
author_rank: Super Contributor
timestamp: October 10, 2023, 04:29:03 am
content: Quote from: zapta on October 10, 2023, 03:39:17 amYes, power metering ICs are heavily AC mains focused.Your application is relatively simple, so code could be as simple as appx sync to the PWM, and sample 50 times inside each PWM period (25ksps) and over 25 cycles of 500Hz for 20sps reporting.You can then just sum each ADC reading into a u32 number, for I and V, and send the upper 12-16 bitsThe 1250 sample points resolve to < 0.1%, and it would help to slightly wander the samples so you get a true average.

author: zapta
author_rank: Super Contributor
timestamp: October 11, 2023, 03:11:26 am
content: Thanks @duak. Your schematic is very readable. ;-)If I got your design, it applies to my case as these stages1. I and V sensing2. Analog multipier3. Low pass filter4. ADC sampling at a relatively low rate (good)That's a very interesting design. I will think more about it and try to adapt to my case.Can you explain how the offset and scale calibrations are done with just two pots? With scale and offset errors in X and in Y channels, the output Z isZ = (AX +B) * (CY + D)Z = (AC)XY + (AD)X + (BC)Y + (BD)So I am not sure how offset and scaling of Z can result in XY alone.

author: zapta
author_rank: Super Contributor
timestamp: October 11, 2023, 03:19:43 am
content: @PCB.Wiz, yes, the key for sampling PWM directly is to have high rate. I guess it's because it contains much higher frequency that sine wave. I will run a few python simulation and see what kind of sampling rate I need.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: October 11, 2023, 03:37:05 am
content: Quote from: zapta on October 11, 2023, 03:19:43 amThe oversampling does not need to be super high, to get the best precision you just need care to avoid exact phase lock to the PWM.in the example I gave of 25 periods and 50 samples in each, you get 2% quanta in a single period, but if perfect sync is avoided, you can average over 25 periods to improve that < 0.1%If you needed 1% that's likely 'good enough'.If you wanted a lot better than 0.1%, you could extract the PWM via timer capture on another pin, but will likely then be ADC limited (12b is 0.024% full scale)

author: duak
author_rank: Super Contributor
timestamp: October 11, 2023, 05:12:46 am
content: Zapta, thanks for the thanks!I have to flog my memory back to the design requirements; whatever notes I made are long gone. The users of this gizmo wanted 1% full scale accuracy and it delivered better than 0.2% for DC inputs. I couldn't quite measure AC signals that accurately with my equipment but for their purposes it was good enough. It helped that the voltage was relatively constant and usually in the top 20% of its range and the current was in the top 50% of its range.I convinced myself that input offset errors could be compensated for to the first order with an output offset because of the limited dynamic range of interest. In essence, at zero and at full scale. If better accuracy was needed at lower levels, I'd re-examine the input scaling and offset requirements and add some adjustments. Analog Devices did a great job on the design and trimming of these parts. BTW, this thing was truly artisanal as I built only one on vector board. The company folded and I asked for it back so I could use it my home lab. They couldn't do that but gave me a PCB version of it. They were happy with it enough to make copies. I should try it out some day.Hope this makes sense,

author: Zero999
author_rank: Super Contributor
timestamp: October 11, 2023, 08:12:33 pm
content: Quote from: zapta on October 11, 2023, 03:19:43 amHow often does the power reading need to be updated?The sample rate doesn't have to exceed the PWM frequency. It can be much lower.Suppose the duty cycle is 50% and the frequency is 1kHz. Now take a random sample, roughly at a frequency of 10Hz, noting just whether it's high or low. You'll find that 50% of the time, it'll be high and 50% of the time it'll be low.You of course want power, so need to sample the voltage, as well as the current and multiply it to get the power. It isn't even necessary to store all of the readings in an array. Simply add each reading to a variable, then after every 2nsamples shift it by n bits, update the display and clear the variable.

author: tszaboo
author_rank: Super Contributor
timestamp: October 12, 2023, 08:40:45 am
content: Quote from: zapta on October 10, 2023, 03:39:17 amThey are AC focused, and most of them wouldn't work, but for example the ADE7751 has an AC/DC selection pin, which allows it to be used in DC systems. It also has 14KHz sampling frequency. Maybe the PWM needs to be fed through a LPF for it to work, I haven't tried it.

author: Dave
author_rank: Super Contributor
timestamp: October 12, 2023, 11:48:28 am
content: STM32H7 is a powerful microcontroller. You can set up some timers to by in sync with the PWM generator and sample both voltage and current at say 10 or 20 kHz. You can even set up DMA to copy the ADC results into an array, and you can then process the data periodically at your own pace, without bugging the rest of your software with interrupts. You already have the hardware at your disposal, make use of it.Adding external hardware such as ADE7751 or AD633 is nonsense. And expensive on top of that.

author: tszaboo
author_rank: Super Contributor
timestamp: October 12, 2023, 12:22:06 pm
content: Quote from: Dave on October 12, 2023, 11:48:28 amThe STM32H7 is 2x the price in single and 3x the price of the ADE7751 in large quantities. And the circuit works right after you soldered it together, faster than you setting up the CubeMX and the GCC or whatever the compiler of the day is.

author: Dave
author_rank: Super Contributor
timestamp: October 12, 2023, 02:03:24 pm
content: Quote from: tszaboo on October 12, 2023, 12:22:06 pmHe already has an STM32H7, the ADI chips are just going to be an additional cost.Also, side note, CubeMX is trash. Manually writing to registers is the way to go. It takes more time, but you avoid the garbage spaghetti bloat.Quote from: zapta on October 09, 2023, 05:59:52 pm

author: zapta
author_rank: Super Contributor
timestamp: October 12, 2023, 10:43:32 pm
content: Thank you everybody for the feedback. I plan to examine both approach,1) An analog circuit that reduce the required ADC sampling rate and2) High speed ADC sampling, will try this first in simulation to estimate the necessary sampling rate and the resulting sampling noise, and accuracy.For the analog approach I plan to try the circuit below. With a relative slow ADC sampling rate and multiply the sampled RMS current by the known DC rail voltage. Any feedback will be greatly appreciated. E.g. do I need an amplifier between the shunt and the AD737? (If you wonder, the 'F' in the rail names indicates that this is the floating/isolated part of the board).