Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: noobdude on Sep 18, 2014, 12:27 am

Title: Reading DC rms/average voltage from a pwm signal
Post by: noobdude on Sep 18, 2014, 12:27 am
Hi, in one part of my project, I'm switching an N channel mosfet via digital pin PWM.  It's adjusting power from two 3.7v batteries in series to a load.  I need to be able to read the average voltage on the load side, but it's pulsing. I'm trying to figure out what the best way to achieve this is.  Thanks!
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 18, 2014, 12:43 am
RMS and average voltage are not always the same thing.... in this case, it is.

With a true square wave, Vrms = Vpk x percent/100, or in this case since analogWrite() is 0-255:

Vrms = Vpk x value/255

So you don't need to be able to measure it, as you know what you are sending to analogWrite().

So you don't need to be able to measure it.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: MarkT on Sep 18, 2014, 01:25 am
That's not true if the load isn't resistive.

Try a RC low-pass filter into an analog pin (probably with a voltage divider
too given the voltage).
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 18, 2014, 01:53 am
Actually, if driven by a very low resistance like a saturated MOSFET, that measure of RMS voltage -will- be accurate.

However, the power will not be. And using an RC lowpass filter will only give you the average/RMS voltage for a square wave, not the power.

For the power, you need to know the current. If it is a resistive load, it is a doddle as you now the voltage and so the power is simply V^2 / R.

If it is a capacitive or inductive load, you are going to need a series current sense resistor and a fast ADC to capture a lot of samples during a cycle.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: MarkT on Sep 18, 2014, 02:00 am

Actually, if driven by a very low resistance like a saturated MOSFET, that measure of RMS voltage -will- be accurate.


You might want to reconsider that remark, hint load=capacitor.

Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: noobdude on Sep 18, 2014, 02:19 am
It's a resistive load being powered by batteries.  I'd really like to get the actual voltage somehow, since battery sag/voltage drop will play an issue. I found more info here.

http://forum.arduino.cc/index.php/topic,204036.0.html

Just need to read more.. Not enough time. I really appreciate the replies.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: raschemmel on Sep 18, 2014, 04:43 pm
Quote
Hi, in one part of my project, I'm switching an N channel mosfet via digital pin PWM.  It's adjusting power from two 3.7v batteries in series to a load.  I need to be able to read the average voltage on the load side, but it's pulsing. I'm trying to figure out what the best way to achieve this is.  Thanks!   


Quote
Try a RC low-pass filter into an analog pin (probably with a voltage divider
too given the voltage).



R= 4.7 k ohm
C = 2.2uF


  http://provideyourown.com/2011/analogwrite-convert-pwm-to-voltage/  (http://provideyourown.com/2011/analogwrite-convert-pwm-to-voltage/)
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 18, 2014, 05:15 pm
Frack! Brain fart.

For a square wave, Vavg and Vrms are _NOT_ the same thing. Calculating average voltage is as simple as duty cycle times peak voltage. So 5V pulses at 50% dutycycle is 0.50*5 = 2.5Vavg. Vavg = Vpk*dutycycle/255

However, power is equal to V^2/R, and Vrms specifically is the equivalent DC voltage that would cause the same power to be consumed. So for 5V pulses at 50% dutycycle:

5V^2/1 = 25W
2.5V^2/1 = 6.25W
0.50*5V^2/1 = 12.5W  Obviously not the same as 2.5V

(dutycycle/255)*(Vpk^2)/R = P = Vrms^2/R
(dutycycle/255)*(Vpk^2)/R = Vrms^2/R
(dutycycle/255)*(Vpk^2) = Vrms^2  cancel R from both sides
sqrt(%*Vpk^2) = Vrms  square root of both sides
Assuming Vpk is 5V:
Vrms = ((dutycycle/255)*(5^2))^0.5 
Vrms = 5*(dutycycle/255)^0.5   separate out the square root of 5 squared

Someone correct me if I'm wrong.

I cross checked my answer with this website:
http://masteringelectronicsdesign.com/how-to-derive-the-rms-value-of-pulse-and-square-waveforms/

Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: raschemmel on Sep 18, 2014, 07:19 pm
Quote
Hi, in one part of my project, I'm switching an N channel mosfet via digital pin PWM.  It's adjusting power from two 3.7v batteries in series to a load.  I need to be able to read the average voltage on the load side, but it's pulsing. I'm trying to figure out what the best way to achieve this is.  Thanks!


Maybe I am oversimplifying it but it seems to me that if you output a 50% duty cycle PWM and adjust the R & C values of the RC filter until you get 2.5V , then you have a perfect PWM to analog converter that should output a voltage that correlates to the duty cycle.
ie:
25% duty cycle => 5V/4= 1.25 V
75% duty cycle = (2.5 +1.25=3.75 V

Even if you couldn't find R & C values to give you this relationship can do the correction in SW.
Why isn't it that simple ?
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 18, 2014, 07:42 pm
That is fine for the average voltage, but not RMS. Of course, you can correlate that to the RMS voltage.

But why do all that if you already know the duty cycle?
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: vffgaston on Sep 18, 2014, 07:52 pm
Quote
RMS and average voltage are not always the same thing.... in this case, it is.


I've read your latter posts. Any case: What would be the "average voltage" definition (if any) for a PWM signal?

Regards.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: raschemmel on Sep 18, 2014, 08:57 pm
The OP never said anything about RMS . He specifically said "average" , which is exactly what an RC low pass filter does if the values are chosen correctly. I've done it myself before and was able to get 2.5 V for 50% duty cycle.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 18, 2014, 09:01 pm

Quote
RMS and average voltage are not always the same thing.... in this case, it is.


I've read your latter posts. Any case: What would be the "average voltage" definition (if any) for a PWM signal?

Regards.


My apologies, RMS and average are -not- the same thing here.

Average voltage is just.. an average of the voltage. If 5V is On 25% of the time, the average voltage is 1.25V. Whereas the RMS voltage would be 5*(0.25^0.5) = 2.5Vrms. IE, the voltage that would cause the same power dissipation in the load as 5V that is on 25% of the time.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: raschemmel on Sep 18, 2014, 09:03 pm
Quote
Average voltage is just.. an average of the voltage. If 5V is On 25% of the time, the average voltage is 1.25V. Whereas the RMS voltage would be 5*(0.25^0.5) = 2.5Vrms. IE, the voltage that would cause the same power dissipation in the load as 5V that is on 25% of the time.


I think that is very useful info to know , regardless if it applies to this post.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 18, 2014, 09:04 pm

The OP never said anything about RMS . He specifically said "average" , which is exactly what an RC low pass filter does if the values are chosen correctly. I've done it myself before and was able to get 2.5 V for 50% duty cycle.


He specifically said "Reading DC rms/average voltage from a pwm signal" in the title of the thread, so there is some question as to which he wants. He's not said yet.

Yes, I know, an RC filter will get you the average voltage. But you already have the duty cycle and the voltage. Why add parts and use an analog input? You can calculate it in the program.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: raschemmel on Sep 18, 2014, 10:29 pm
This is what he said. Show me where it says "rms".

Quote
Hi, in one part of my project, I'm switching an N channel mosfet via digital pin PWM.  It's adjusting power from two 3.7v batteries in series to a load.  I need to be able to read the average voltage on the load side, but it's pulsing. I'm trying to figure out what the best way to achieve this is.  Thanks!  



Quote
It's a resistive load being powered by batteries.  I'd really like to get the actual voltage somehow, since battery sag/voltage drop will play an issue. I found more info here.


The above are the OPs only two posts.
I don't see anything about rms


I agree it is in his post title but he never said anything about it after that when asked for clarification. I don't think he knows the difference and thought they were the same. The fact that he lumped two different parameters together suggest that because if he was interested in one , he would not be interested in the other and vice versa.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 18, 2014, 10:47 pm
Oh, come on! It is in the title. He typed the title.

Yes, if he doesn't know the difference, isn't it important to educate him about that difference? Perhaps he says average in his message, but really wants to know the RMS, but doesn't know until he finds out what RMS voltage is.

Again, please tell me why it is necessary to waste extra parts and an extra pin just to find out what you can find with one equation. I'm just not getting it.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: raschemmel on Sep 18, 2014, 11:22 pm
Quote
Yes, if he doesn't know the difference, isn't it important to educate him about that difference? Perhaps he says average in his message, but really wants to know the RMS, but doesn't know until he finds out what RMS voltage is.  


Now we're getting somewhere.

Now we just have to find our which parameter he really needs. I think you already showed him how to convert from one to the other.

I maintain that by adding the RC LP filter and using an analog pin, he can measure the value.
In defense of my point , I submit that the OP's words indicate he is looking for a measurement and not a calculation:

Quote
I need to be able to read the average voltage on the load side,


You point is he should just add a line of code.  (which I submit is not a measurement)

I think that about sums it up.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: vffgaston on Sep 19, 2014, 10:05 am
Quote
My apologies, RMS and average are -not- the same thing here.

Average voltage is just.. an average of the voltage. If 5V is On 25% of the time, the average voltage is 1.25V. Whereas the RMS voltage would be 5*(0.25^0.5) = 2.5Vrms. IE, the voltage that would cause the same power dissipation in the load as 5V that is on 25% of the time.


You shoudnt apologise: We are here just to interchange ideas (and/or certainties; nevertheless I would prefere doubting on everything  :smiley-roll-sweat:).

The product of the high voltage by the duty cicle has physical significance, as long as it is the DC value that a RC low pass filter will show: Is it correct to call it "average" (I wonder: belive me, I'm honest)?.

Regards
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 19, 2014, 03:45 pm
As long as the drive to the RC filter is symmetrical in nature and a square wave, then the smoothed DC output will be the average voltage

What do I mean by "the drive ... is symmetrical in nature"?

I mean that it must charge and discharge it equally well.

So let's say you had a single-ended drive, maybe a low side driver such as a MOSFET with an On resistance of 50mOhms. But the load is only 1k. You then have a 3.3k resistor and a 100uF capacitor RC filter.

When the MOSFET is On, the total resistance discharging the capacitor is through the MOSFET. 3.3k + 50mOhm = 3.30005K.

When the MOSFET is Off, the total resistance charging the capacitor is through the load resistance. 3.3k + 1k = 4.3k.

So the voltage across the capacitor will NOT be the average voltage. It will be quite a bit lower.

But let's make the ratio between the smoothing resistance and the other resistances much greater.

MOSFET 50mOhm
Load 10 Ohm
RC  100k and 3.3uF (same time constant as above)

Now the discharging resistance is 100.010K, charging resistance 100.00005k. Very little difference, so the smoothed voltage will only be very slightly lower than the actual average voltage.

Again, this is only true for a square wave.

Well, to be clearer, it -will- work for a pulsed sine wave or other waveform, but ONLY with symmetrical drive. In other words, the waveform must be coming from something like an Op Amp that can source and sink to maintain the waveform.

Clearly, a power supply with a transformer and rectifier diodes is not averaging the voltage with its capacitor. It is smoothing to the peaks, because the diode only sources current.


Still, if the PWM is coming from the same Arduino that is "measuring" the average voltage on the load, why not just calculate it from the PWM?
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: raschemmel on Sep 19, 2014, 04:14 pm
I don't think your calculations are correct . Are talking about the arduino PWM output pin that is connected to the input of the RC LP filter ?

The average of a 25% duty cycle PWM is 25% of the Vcc voltage which in this case is 5V. If you are trying to correlate the average of the PWM pin using the LP filter to the average of the H-Bridge which always has two devices on, I think the conclusion is the same.
I think the averaging of the PWM pin using the LP filter accurately reflects the averaging of the power applied to the motor load.
I don't have any calculations to prove that but that is my conclusion.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: vffgaston on Sep 19, 2014, 06:55 pm
Quote
I think the averaging of the PWM pin using the LP filter accurately reflects the averaging of the power applied to the motor load.


Unless the "thing" connected to the PWM (with or without a H bridge) sinks constant DC current (i.e. not dependant on the DC voltage) the power is not proportional to the voltage (so the term "average" is misleading).

Regards
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: raschemmel on Sep 19, 2014, 07:06 pm
The "thing" the RC LP filter would be connected to is the PWM pin and when it is LOW it sinks current. When it is HIGH it sources current. In the case of the H-Bridge , connected to a motor load, when the signal is HIGH, the motor current passes through one transistor or mosfet to get to the motor and then through the motor and then through another transistor or mosfet to get to ground. When it is LOW, both devices are OFF and there is no path to ground and the voltage ACROSS the motor goes to zero.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 20, 2014, 12:23 am
The Arduino pin should be a symmetrical drive, and so should not be a problem.

I have seen people here try to generate something like a 0-10Vdc signal by using a high side PNP switch with an RC network after to smooth it out. It never quite works because of the effects of assymetrical drive currents.

If it is the output of an H-bridge, that should also work as it is a symmetrical drive.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 20, 2014, 12:24 am
vffgaston is right, power is not proportional to voltage. Hence all my futzing about to calculate RMS voltage.

But the original poster has still not told us if he's figured out yet if he means RMS voltage, or average voltage.
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: vffgaston on Sep 20, 2014, 09:40 am
Quote
vffgaston is right, power is not proportional to voltage. Hence all my futzing about to calculate RMS voltage.


Thanks: I was questioning my basics  :smiley-roll-blue:.

Regards
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: rmetzner49 on Sep 24, 2014, 04:39 am
P=I*I*R, so it is a second order effect.  If you DOUBLE the voltage you will QUADRUPLE the power.

2V/1ohm=2A      Since P=I*E  power=4W
4V/1ohm=4A     Since P=I*E  power=16W
Title: Re: Reading DC rms/average voltage from a pwm signal
Post by: polymorph on Sep 24, 2014, 04:23 pm
Exactly.