Reading DC RMS voltage from a PWM signal.

Status
Not open for further replies.

ddmods

Active member
Hi, my first post here. :) I'm new to all of this, but picking it up. I apologize in advance if any of my wording is wrong, and corrections are greatly appreciated. In one part of my project, I'm switching an N Channel MOSFET @ around 2khz via teensy digital pin. Controlling the power to a load My questions is, how can I read RMS voltage from a PWM signal? Only worried about if this will work this way right now, no need for code.

Any help would be greatly appreciated, thanks!
 
Last edited:
Why would you need RMS for DC? if you know the PWM value 0 to 255 and the voltage potential you can calculate the approximate voltage. No need to measure anything.

So if your mosfet is controlling 12VDC and you set the duty cycle to 50% (127 in 0 to 255 range) then your approximate voltage will be 6VDC.

If you really need to measure it then put a low pass filter on the PWM signal and measure the DC voltage. Make sure you use a voltage divider and do not load the PWM too much. However, the duty cycle and voltage calculation should be really close.

This should cover RMS:
http://en.wikipedia.org/wiki/Root_mean_square

Still do not know why you would need it for DC. I think the RMS for a DC voltage is the DC voltage.
 
RMS of a PWM'ed DC signal is just the DC value * sqrt(Duty cycle).

If your FET is turning on and off the load with essentially a square wave, this will be correct. If the load is 'strange' -- a motor, or has a large capacitance in parallel with it, the voltage across the load may not look like a square wave, and the calculation won't be correct.

Be aware that the Teensy's digital output voltage is only 3.3 V -- be sure to select a MOSFET that turns on completely with this low a VGS.
 
Or use a MOSFET driver like the TC442x series. I am actually using these (TC4425) 3A MOSFET drivers with the Teensy 3.1 and a PWM signal on a small inductive load. If you need more than 3A you can use the TC442x drivers to control the MOSFET itself.
 
Be careful using a high speed driver such as this with an inductive load on the MOSFET. The fast turn-off of the MOSFET forces the inductive load to generate a (possibly very) large voltage spike which can easily exceed the supply voltage and the FET's ratings. Even the inductance of long wiring can cause this.
 
Hi, I figured out that that a low pass filter should work. I was googling RMS, and you're right, not what I was looking for. I came up with tons more results without it. I can't simply guess what the voltage is, since it's a resistive load and very low resistance at that. Other things come into play like battery sag. Thanks for your patience and replies! I'm seriously just starting to learn about electronics. The mosfet I'm using fully turns on @ 2.5v, so I'm good there. I'm not 100% on how high of frequency I can go with a resistor in series from the pin. I may have to use a gate driver like someone said, but we'll see. If I'm switching at 2khz do you think it's needed with the series resistor? I see most people use 100ohm and call it good, but I think the actual value should be higher to protect the digital pin, but then again, noob....

I see there's a DAC onboard the teensy, but the posts I see from people they are using it for output. Can't find much info on that.
 
A low pass filter won't give you the same result as an RMS calculation -- why do you think you need rms ?

What voltages and currents are you driving with the FET ?

A gate resistor is sometimes used to prevent any high frequency oscillation -- it physically has to be close to the FET (< 1 inch). If the wire from the Teensy to the FET is already short, then an R won't help. Also, the R is not there to protect the digital pin -- it's OK without it.
 
I'm using the resistor because I read this and similar things when I was doing research.

http://forum.pjrc.com/threads/24799-Teensy-3-0-stopped-working-after-2-weeks

The low pass filter is working great so far @ 2929hz. I just have to write the code. :) I appreciate the help and patience.

It's hard to help any more without any details about what you are doing. Please show a schematic, list the FET part number, and describe the load you are using. Also, why exactly do (did ?) you want RMS ?
 
I just didn't know what I was talking about.. I just needed the average voltage that could be read with the teensy adc. It's working just fine now with the low pass filter, so the problem is solved. :)Switching the mosfet with teensy digital pin is working great as well. If you're curious, the mosfet is IRLB3813. I really appreciate the replies.
 
I just didn't know what I was talking about.. I just needed the average voltage that could be read with the teensy adc. It's working just fine now with the low pass filter, so the problem is solved. :)Switching the mosfet with teensy digital pin is working great as well. If you're curious, the mosfet is IRLB3813. I really appreciate the replies.

The on resistance of this FET will be ~10x larger when you use the Teensy's 3.3 V digital I/O to drive it. It will also take about 1 us to turn on and off; be careful it doesn't cause it to overheat.
 
RMS of a PWM'ed DC signal is just the DC value * sqrt(Duty cycle).

If your FET is turning on and off the load with essentially a square wave, this will be correct. If the load is 'strange' -- a motor, or has a large capacitance in parallel with it, the voltage across the load may not look like a square wave, and the calculation won't be correct.

Be aware that the Teensy's digital output voltage is only 3.3 V -- be sure to select a MOSFET that turns on completely with this low a VGS.

This is absolutely correct. :) I don't know how I missed it. I'm trying again, and the average is definitely not correct. I already get accurate peak voltage and can use the map function to map the duty cycle from 0-100%. Also saves another voltage divider and analog pin + code.

I only used 3 if statements to try to regulate the rms voltage to a target voltage. Basically, just if RMS voltage is less than target voltage, increase duty cycle by 1 and so on. Only problem is, when the target voltage is much higher or lower than RMS voltage, it takes awhile for it to rise or fall, but amazingly enough, it keeps the rms voltage pretty close or exact(within 100mv)with just this simple idea. I'm going to tweak the code to make it ramp faster then slower when it's further or closer to the target voltage. Just wanted to say thanks again, really.
 
@ddmods,
Its sounds like you implemented a "integral" function (integration). That is one piece of a PID loop. If you make the iteration time deterministic the frequency response will be consistent.

http://www.automationdirect.com/static/manuals/d4user/ch8.pdf

This is the manual I used to implement a PID loop in a microcontroller years ago. I like how it lays out the math very simply and easy to implement. It also has math for filtering and PID loop optional functionality. Kind of fun once you get your head around it.

Edit:
BTW, it perfectly fine to implement an integral only PID loop. I have done this on parts of a process that just needed integral correction. I also did this in a video game hover craft. I used integrals to represent the springs on a vehicle. It just maintained a certain distance to a surface. The side effect is that the vehicle would ride on walls and ceilings!
 
Last edited:
Status
Not open for further replies.
Back
Top