Go Down

Topic: Reading DC rms/average voltage from a pwm signal (Read 12551 times) previous topic - next topic

noobdude

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!

polymorph

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.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

MarkT

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).
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

polymorph

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.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

MarkT


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.

[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

noobdude

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.

raschemmel

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/
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

polymorph

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/

Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

raschemmel

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 ?
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

polymorph

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?
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

vffgaston

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.

raschemmel

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.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

polymorph


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.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

raschemmel

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.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

polymorph


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.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

Go Up