PDA

View Full Version : PWM vs. HIGH



ERMEGM
- 23rd February 2013, 15:06
Hi Guys,

Was wondering if there is any difference between PWM 255 vs. making an output HIGH. The output on an LED appears identical, but wanted to know if there was an advantage to using PWM over a constant output. The PIC would be driving a FET, so would this be helpful to the FET to PWM it or it just doesn't matter when PWM is at 255?

Thanks,
Tony

HenrikOlsson
- 23rd February 2013, 15:23
The difference is that HIGH PortB.0 sets the pin high and then continues with the next instruction while PWM PortB.0, 255, 1000 will keep the pin high for the duration of the command (ie 1000 PWM cycles in thic case). During the 1000 PWM cycles the pin will be high and the FET won't see any difference between that and using the HIGH command. But after the 1000 cycles the pin won't be high any more, in fact it will be set to work as an input according to the manual.

/Henrik.

ERMEGM
- 23rd February 2013, 22:52
Thank you for your response Henrik, but is there any benefit to using PWM to pulse an output as opposed to keeping it high? When driving a given load, I thought it was better to quickly pulse it rather than have a FET constantly on. Now when I say it out loud, it doesn't make much sense, however, stranger things have happened with electricity that we just accept certain results. Is there any difference?

Thanks,
Tony

HenrikOlsson
- 23rd February 2013, 23:12
Hi,
Not sure I understand....
If the output is ON the output is ON so to speak. There's absolutely no difference between a pin with a PWM signal with 100% dutycycle and a pin set to 'high' - in both cases the pin is just 'high' - all the time.

The benefit you might be thinking of is that with PWM you can drive a load (like a LED) with a much higher current at a low dutycycle so that the average current thru the load (LED) is the same as it would be when normaly driven by a "constant high". Ie, if your LED is designed for 20mA continous forward current you might be able to push 100mA thru it at 20% dutycyle resulting in an average current of 20mA.

Why would you want to do that? Because you can then multiplex many LEDs across pins and drive them without an individual pin for each LED.

If you'd apply a 100% dutycycle PWM to that LED the continous current thru it would then be 100mA and it would promptly die... (Obviously you need to check the datasheet for LED to find the maximum continous current, peak current etc)

/Henrik.

wdmagic
- 24th February 2013, 09:16
the only reason to use a PWM with a FET would be to control a load such as a light dimmer or motor speed controller. I do this alot with PWMs, not sure about any other PWN settings but if you set HPWM to 255 it will stay high just like HIGH. untill you change it, as the PWM runs at the frequency and rate you set it at untill you change it, and it does this in the background behind any program running, if you make a change to HPWM it adjusts that PWM to the new settings and continues with the program, the PWM continues in the background.

This is great for running lights or motors for brightness and speed if the frequency is set faster than say 40 for lights or 100 for motors, the higher the frequency the more smooth the transistions. if you set the frequency to say 10 hz you will see lights flash on and off and motors will be very jerky. I usually run PWMs at 1khz or more, if used with motors I usually want them above 15khz at at lower freq's they can Whine... also on motors you need to have a reverse voltage diode and capacitor in parallel with the motor for the inductive kick.

RossWaddell
- 4th March 2013, 21:21
the only reason to use a PWM with a FET would be to control a load such as a light dimmer or motor speed controller. I do this alot with PWMs, not sure about any other PWN settings but if you set HPWM to 255 it will stay high just like HIGH. untill you change it, as the PWM runs at the frequency and rate you set it at untill you change it, and it does this in the background behind any program running, if you make a change to HPWM it adjusts that PWM to the new settings and continues with the program, the PWM continues in the background.

This is great for running lights or motors for brightness and speed if the frequency is set faster than say 40 for lights or 100 for motors, the higher the frequency the more smooth the transistions. if you set the frequency to say 10 hz you will see lights flash on and off and motors will be very jerky. I usually run PWMs at 1khz or more, if used with motors I usually want them above 15khz at at lower freq's they can Whine... also on motors you need to have a reverse voltage diode and capacitor in parallel with the motor for the inductive kick.

I'm using HPWM to control motors in much the same way as you describe, but I'm using a SN754410 driver which allows me to reverse the motor's direction but switching the directional pin from high to low and the HWPM's dutycle to be reversed (i.e. high on directional pin means low dutycycle produces slow motor speed, low on directional pin means the same motor speed needs to use (255 - dutycycle)). I'm only using 0.1uF caps between each motor lead and it's casing (and a third between the motor leads) - do I need another cap? I do have a 100uF one on the 754410.

wdmagic
- 6th March 2013, 03:01
first here is a good layout for using dc motors with a FET and a PIC, NOTE C3 and C4 are optional if your using motor case to ground.
6875
now as for using that other chip, I dont have the datasheet here right now to look at, but I usually use either a DPDT relay or a FET H-Bridge circuit for reversing, depends on the amps needed. for tiny DC motors a relay is a pretty cheap way to go, and requires no special circuitry. an Hbridge requires 4 fets and a couple transistors but can handle any amperage based on the FETs used. you can also use a lm298 I beleive, as it has dual hbridges built in. but only allows for a certain wattage.

RossWaddell
- 6th March 2013, 03:56
The SN754410 (http://www.ti.com/lit/ds/symlink/sn754410.pdf) has an H-Bridge built in and eliminates the FET and diode.

6876

(Ignore the bumf to the bottom left - it's for a button input to change the direction of one of the motors)

HenrikOlsson
- 6th March 2013, 06:23
Just a quick note regarding the circuit in post #7.
For basic on/off control it should be fine but be careful IF you're going to use it to control the speed of the motor by PWM'ing the gate of the MOSFET. The 330uF cap across the motor terminal will present a very low impedence path (basically a short) at any considerable frequency.

Perhaps the capacitor was meant to bypass the power rails (like C2) and not across the motor?

/Henrik.

wdmagic
- 6th March 2013, 07:05
without the capacitor in there it becomes erratic, similar to not having a diode there, ive ran these up to 20khz with no problem, its electrolytic.
your not shorting the capacitor, the charge is bled off from by the motor and when the fet is on the cap charges again. when the fet is off the motor draws power from the cap.
it makes a easier transistion from the off mode to on. its not so much a squarewave that gets to the motor its more of a square on, sawtooth off waveform. the more the pwm is on the better the wavefor get s from 50/50 square/saw, gets to more of a saw, then at the end near the 90% mark it starts looking similar to a pulsed sinewave.
but in the end, It just works better with the cap. if you run it higher than 20khz, well I dont know how that would behave.

HenrikOlsson
- 6th March 2013, 07:38
Hi,
I'm not saying you're shorting the capacitor, I'm saying the capacitor is providing a short across the motor when the MOSFET is switching at high frequency.
At 20kHz, the capacitive reactance of the capacitor is Xc=(1/(2pi*f*C)) or ~0.02ohm, add to that the ESR of the cap. To me it looks the same as placing a very low resistance right across the motor but I'm obviously missing something since you're actually running it a 20kHz.

I've had 100nF ceramic capacitor across motor terminals literally going up in smoke, but then the motor was driven by a H-bridge.

/Henrik.

Ioannis
- 6th March 2013, 07:48
Agree with Henrik. I would not place there an electrolytic capacitor. Even a nF range is questionable.

I really wonder how it still works without rising temperature of the capacitor itself or the MOSFET.

Ioannis

wdmagic
- 6th March 2013, 11:02
well its just charging and discharging its not having a signal go through it, the diode protects it from the inductive kick of the motor coils. that also protects the mosfet. I have made about 50 of these, without the cap it acts up, sometimes right then sometimes after 30 seconds, but with it it runs fine, no heat, just a good clean transistion of speed.
you have to remember without the motor its just charging the cap slower or faster based on the PWM duty cycle, the diode is there just for the inductive kick of the motor, and the cap just helps filterthe DC pulses, just like it would in a ac to dc circuit. before learning about PICs, i used this same circuit for the last 20 years with 555 timers.

wdmagic
- 6th March 2013, 11:11
heres some pics of one that Loan out to people to try the circuit out, it runs a waterpump 12v @ 6.4a and doesnt warm up at all, if I put a resistive load on it it handles well to, ive got up to 18amps at 12v and it just barely gets warm without a heatsink. most of this is due to the FET I am using, FDP8440

6877
6878
6879

HenrikOlsson
- 6th March 2013, 12:23
I believe you. There's obviously something I don't understand here.

Ioannis
- 6th March 2013, 13:50
Me too. I had built a simple PWM circuit to drive a power LED about 5A max, and if I had such a capacitor in parallel to the load, it would nearly explode. My frequency was a bit higher (2KHz) and this could be the problem. In either case I think, capacitor under stress.

Ioannis

peterdeco1
- 6th March 2013, 17:14
I strongly suggest a high speed diode across the motor. We have had 1N4001's burn out. On small hobby motors we use 1N4148. On our larger 12V systems we use FR603. On one of our larger 12V systems we parallel 2 of the FR603's to keep them cool.

wdmagic
- 6th March 2013, 19:36
yea my circuit board program didnt have my FET listed and I wasnt sure if it had some of the other diodes, if I run the circuit at less than say 1 khz then i use just a standard diode, but if I run higher Ill find one that has a recovery time that meets or exceeds my circuit, ive used shottkey 5818's and such. When choosing my FETs I try to make sure they have a super low on resistance, it makes a huge difference on how hot the FET gets. The ones I use for motors is usually a FDP8440 which only has a .0022 ohm resistance, but its higher cost. some of you out there that do stuff with Electric RC cars may remember speed controls that had like 8 FETS in parallel, this was to lower the ohm and to spred the current between the 8 so there was no need for a heatsink, it still got hot. The cool thing about these new FETs are that they can handle all this on there own so weight is cut down, and they work better than the others combined.

I have tryed different capacitors from 1uf to 1000uf, the higher the voltage/current the motor is, the higher the capacitor should be used. for instance one of these tiny dc motors that take only a AA battery to run, could use a 1uf cap or probobly none. I use a 180-470uf @25v for a 18V Drill motor. If your useing a purely resistive load like a LED strip lighting or an incandesent bulb, you dont need the diode or the cap, however using the cap does help with brightness pulsing at lower frequencies.

HenrikOlsson
- 6th March 2013, 20:44
Hi,
I find these latest couple of posts quite interesting and I'm having a hard time letting it go.... I'm in no way trying to second guess anyone, the circuit is obviously working. However, I did a cople of virtual tests in LTSpice and I'll show you the results here.

First a look at the schematic:
6882
* The motor is substituted for a 2mH inductor in series with a 2ohm resistor.
* The capacitor across the motor is 330uF with an ESR of 250mOhm.
* The MOSFET is driven with a 10V PWM signal with 50% dutycycle.


First, lets look at the current thru the MOSFET without the 330uF capacitor present across the motor:

6881

As can be seen the peak current is around 6A which makes sense since the series resistance of the "motor" is 2ohm and the powersupply voltage is 12V. We can basically neglect the Rds-on of the MOSFET and the internal series resistance of the supply.


Now, lets put that 330uF capacitor back in the circuit and again look at the current thru the MOSFET:

6883

First note the change in scale. The peak current thru the MOSFET, disregarding the very high peaks at the beginning, is now around 18A.


Finally lets look at the current thru that 330uF capacitor:

6884

30A peaks, average current of around 15A. Since ESR of the cap is modeled as 250mOhm the power dissipated by the caps ESR alone would be 15*15*0.25=56W - in reallity it just wouldn't work. Yet it obviously does....



So, why does it work in real life when it doesn't in the virtual world?

One thing I noticed is that the MOSFET in wdmagics schematic is driven directly by the PIC output, something I didn't think of at first....
Since it's a standard MOSFET (and not a logic level one) that's likely not going to make it turn on fully and the limited drive capacity of the PIC I/O might slow down the turn on/off times. So I tried that in LTSpice by changing the amplitude of the PWM signal (from 10V) to 5V and the series resistance of the "generator" to 200ohm (in an effort to limit the current to 25mA). I then run the simulation again and this time the peak current thru the MOSFET with the capacitor in circuit was around 2.6A. However the voltage at the MOSFET drain toggles between 11.2V and 12V showing that the MOSFET is barely starting to turn on, ie the voltage across the motor is only 0.8V.

Removing the capacitor in this setup barely makes a difference, the peak current thru the MOSFET is around 2.1A.


So, my guess is that in reallity the circuit works because the MOSFET isn't saturating and therefor limiting the current. If the MOSFET was driven "properly" I still think the 330uF capacitor would be toast within seconds - but I'm prepared to be proven wrong.

I'm attaching the LTSpice file as well in case someone wants to play with it.

Thank you all for the interesting discussions!

/Henrik.

EDIT: OK, yet another forum issue. Those screenshots looked just fine when I previewed the post but now they are barely usable.....

languer
- 7th March 2013, 00:19
30A peaks, average current of around 15A
You do know those numbers do go down quite a bit if you use 330uF as opposed to 330F.

And yes, adding capacitance across the motor reduces noise but the part has to be derated accordingly. In some cases a resistor is added in series with the capacitor to help the derating (i.e. make it a lossy element; yet still improving the noise characteristics of the motor). This may be counter-intuitive; but the same care that goes into selecting capacitors for a switching power supply should apply here as well.

wdmagic
- 7th March 2013, 01:19
Ok because of all the replys, i've got my scope out and did some testing, the circuit does work with the cap connected however it does change one item, the speed control is logrithmic. so control at very low speeds is very sensitive. removing the cap automaticly reduces the speed of the motor and control becomes virtually linear. Using the scope I get a ~90% square wave with motor running without the Cap, with the capacitor on it increases the voltage and the scope cannot sync with the waveform. I am wondering if this effect is similar to when you use a capacitor with a full wave bridge, for instance inputing 12vac into a FWB you get ~12vdc pulsed, but adding a cap makes it about 17vdc with a little ripple. could the cap be holding the voltage spikes? not sure. and my original issue with the erratic speed without the cap has disappeared, not sure what was causing it but it looks like I can remove the Cap from the boards, and redesign future boards. Soooo my boards will cost less, shrink in size, so thanks everyone for their input.

I didnt test the circuit with the cap installed but with a resistive load only. not sure what results would be for that.

HenrikOlsson
- 7th March 2013, 06:09
languer,
Crap, thanks for spotting THAT mistake! I kept changing the circuit, removing the cap, putting it back in etc and apparently I missed the "u" when it mattered the most.... Yes that does of course make a big difference, thanks again for spotting it!

6886

Green trace is MOSFET current, 10A peaks, blue is capacitor current +/-4A. That makes it much more manageable but yes, selecting the proper cap would be important.



/Henrik.

Ioannis
- 7th March 2013, 07:31
Regarding erratic control, maybe it was from spikes that were upseting the control circuit. A good power supply, grounding (in star) and decoupling may help.

Ioannis

wdmagic
- 7th March 2013, 18:09
Ioannis, I was using batteries at the time originally. here were my stats of that circuit at the time (this has now changed)

18v Drill motor removed from housing and mounted on stand.
1N4001 Diode
180uf Cap @ 25v
FDP8440 Mosfet 30V 277A (theoretical)
12F675 (has been replaced with 12F683 with HPWM @18Khz)

now, when I was using the 12f675 I was using the code from http://www.picbasic.co.uk/forum/showthread.php?t=17550 (http://www.picbasic.co.uk/forum/showthread.php?t=17550) which I gave up trying to calculate the frequency on, I had about 3 sets of code that went about simulating a PWM and it was accurate but very slow. one of them only ran at 4hz I think, I didnt have my scope so it was all guesswork. I tried calculating it out in my head based on what little info I could get online and from datasheets. my thinking was if the internal OSC ran at 4mhz, somewhere in the docs it said it only used 1/4 of the OSC som that put me at 1mhz, then I had my pwms accuracy, so say i wanted a 255 positions that took the 1mhz / 255 = 4khz, and if I had other code it changed that, before coming up with that code it was even worse. now that I know how to use HPWM its irrelevent.

when I first hooked everything up I didnt use a diode or a cap, it was awful. it was so erratic and forgetting all my electronics knoledge I grabed the first item available a Cap. adding this worked a little better. as from a dead stop it started fine but as I increased the speed it increased but when i tried to decrease it became erratic. going back to my electronics knoledge I realised, oh yea I need a diode in there for the reverse voltage spikes, so that worked great. it was a little troublesome with the non linear actuation but I thought that could be part of the code I came up with or perhaps the instability of the motor/diode/cap settings. a larger cap worked better and after building 5 or 6 boards I tweaked it to where it was working pretty good, I still use those boards in some clip on 12v automotive fans I made for using in my lab. they still work fine, I dont know why. when I tryed removing the cap on my original circuit it became erratic again sometimes going into runnaway mode with no control.

I am not sure what caused the original issues, perhaps it was my code, or a solder break, a clip lead, or that stupid 1N4001 diode, it could have been anything. but without the cap it does work perfect on a new circuit so I will be leaving the caps off from now on. I was just starting to learn PICs back then, when I ordered my programmer it came with a 12F629 chip so thats what I started to learn on. It didnt have a PWM so I was unaware that they even had these. untill much later. Im guilty of trying to keep using old tried but not really true code. Its part of that if it works why change it attitude. Which by the way since joining this forum has woke me up and shook me. I read posts all the time that I remark to myself, hey thats kinda cool I wonder where I can use that bit of code at. And then theres some like, Ok thats way over my head...

So everyone, read these posts, try to understand where code can be used, WATCH for mistakes like mine, listen to those that know. and if you dont get something ask, or breadboard it, dont give up, and if you come up with a great piece of code share it, there might be something you dont notice, or someone may be able to show another way or cut the code down in size.

Thank you everyone again for analysing my circuit, its woke me up and I am going back through several circuits and code, cleaning up.

Ioannis
- 7th March 2013, 18:27
I think the diode is a critical part here as peterdeco1 spoted. Maybe the 1N4001 was too slow to catch the spikes and the cap made it more easy to filter all the mess from the coil.

A fast diode there is a must. Then get rid off the capacitor and do a thourough check of the power lines that are well decoupled and the ground has no loops.

Ioannis