Trying to understand PWM

joe paul

Senior Member
Hi Folks,

I have been playing around with PWM and the PWM Wizard, and put a 3 volt motor through it paces, with the 18M2.


Code:
main:
pwmout B.6, 99, 320
pause 10000
pwmout B.6, 99, 250
pause 10000
pwmout B.6, 99, 225
pause 10000
pwmout B.6, 99, 200
pause 10000
pwmout B.6, 99, 160
pause 1000
pwmout B.6,  off
pause 10000
goto main
I used the PWM Wizard then altered numbers myself for a couple lines.

So if I used "PWMOUT B.6, 255, 1023" then the motor would run at its fastest? (I reduced voltage to the motor with a diode, so between the diode and the Darlington array, I gather the motor is getting 3 volts approx., it is a 3 volt motor. Using a snubber diode across the motor's leads, too.)

So I want to achieve the equivalent of running a 14 volt DC model/toy train motor at between 8-12 volts. Is there any formula I can use, or is it much more complicated, or just trial and error testing?

I realize I'll need a MOSFET to run the 14 volt motor.

Thanks,

Take care, Joe.
 
Last edited:

erco

Senior Member
Mostly trial and error, it's easy enough to try. 80% duty cycle is not necessarily 80% of max speed, it depends on the motor and load. To keep max torque, use the lowest PWM frequency you can get out of the Picaxe. It doesn't go low enough IMO, I'd prefer down to 10-20 hz for small DC motors. To see what I mean, write your own "PWM" routine by bit-banging a high/low loop to your motor driver. When you can run the motor at 30 rpm and see the motor cog (start/stop), you've finally gone too far. :)
 

joe paul

Senior Member
Hi Erco,

Thanks for the info!

Been playing around a little more. I am a little leery of fooling around with the 14 volt motor when I am connected to the laptop. What I do is use the battery pack when I have the breakout board connected to the computer, then connect the regulated power supply to the circuit once disconnected. I only have the one laptop so I don't want to ruin my one and only.

Will keep you posted.

Take care, Joe.
 

inglewoodpete

Senior Member
The PWM configuration you use will depend on several factors. First, the characteristics of the motor that you are using. The second is the speed you want to run the motor at. And a third is the loading of the motor and whether the load will vary. At times, you may need to use some sort of feedback mechanism to manage the speed.

If you want to run your motor at low speeds, you may need to start it with a higher PWM mark/space ratio to get it moving. Then drop the PWM back after a few 10's of milliseconds.
 

erco

Senior Member
If you want to run your motor at low speeds, you may need to start it with a higher PWM mark/space ratio to get it moving. Then drop the PWM back after a few 10's of milliseconds.
Which brings me to one of my pet peeves. Modern motor PWM speed controllers (including RC car types) use very high frequencies for PWM. At low speeds, nothing moves, but you hear an audio frequency squeal. Current is flowing, but no torque, no motion. IOW, 0% efficiency. You must "goose" it (AKA overpower) to get it started, then back down to whatever speed you can muster. Every time I ask the experts, they talk about how high freqs are better for efficiency and not overheating, but I don't see it. For robotics and fine control, you want full torque at near zero RPM (which is exactly what electric motors do best).

One of my favorite old robots is an ~80 lb Hero 2000. GOBS of low speed torque, because the PWM freq is way down there in the 10-20 range I mentioned. Same with hobby servos, very torquey. I think that typical servo motor growl is actually less than their 50 hz control signal refresh rate.
 

joe paul

Senior Member
Hi InglewoodPete,

Thanks for the info and tip! I am not a fast runner, so good slow speed performance is important for me. What concerned me was that these motors are said to run hotter on PWM, so I'll have to see for myself. The PWM is done all the time in the hobby, but this is new for me.

I have a brand new motor identical to the one I'll be using in my train project, but I only tested it under the load of occasionally holding the worm gear between my fingers, but seems it responded as well as the 3 volt version to the same code. This second motor is rated up to 18 volts; I used a regulated 12 volt supply and MOSFET. I might have to beef up the MOSFET. I ordered one from a schematic I saw and it does seem rated well enough for the job. Time will tell. I wanted to get everything (regulator, HC-06 module, small relay, and PicAxe board) in a trailing car, so space isn't an issue just yet. I have fooled around with large capacitors and I hope the caps will help keep the circuit alive through momentary voltage loss from the track. I have done it before, but not for very sensitive circuits. In toy train circles, there are techniques for prevent voltage spikes from damaging electronics. I will have 18 VAC on the tacks, so rectifying and smoothing the DC is easy enough, just takes up space with those big electrolytic caps.

So far, so good. Waiting for the other shoe to drop.

Will keep everyone posted.

Take care, Joe.
 

joe paul

Senior Member
Hi Erco,

I have experienced that "singing" sound tonight. Hope it doesn't damage the motor. I bought some thermal breakers a while back and I will put one on the motor and see if it trips. These little permanent magnet motors are around $15, but I still need to be frugal. So once I get it in the rails, I'll tweak the code and see how much pulling power I get at slow speeds. That is supposed to be one of the advantages of the PWM? I'll see!

Thanks for the info!

Take care, Joe.
 

Dartmoor

Member
This thread is of interest to me because I have previously got confused by all the different views on which frequency/frequencies to use for PWM.

I have built several model train speed controllers (for battery powered engines) using logicator (Picaxe flowchart).
The default freq in logicator is set to 10KHz, which produces the 'singing' which is so common & very annoying to me.
My assumption was that anything from around 20KHz down would be audible, so I changed the setting to 30KHz.
As far as I am concerned, that works fine. Many others think differently & I have since read somewhere that the Picaxe will not work with PWM above 20KHz?
I am sure there are sound scientific reasons why what I have done is wrong, but being 30+ years since I studied electronics I cannot remember!

As for the original post asking about a formula, I agree with ERCO that it is mainly trial & error. Fun though! :)
 

AllyCat

Senior Member
Hi,

IMHO 30 kHz (or even 10 kHz) is far to high a "PWM" frequency to send to a railway track. What you have effectively created is an (illegal) radio transmitter. :(

What you should use is a low pass filter (inductor + capacitor) very close to your swtiching transistor / FET. Then you would effectively send dc to the track, which would be more efficient, predictable, silent and legal. Probably use an even higher frequency (which makes the filter components smaller and cheaper), but ultimately you must watch the switching losses (heat). However, the losses should be much lower than when sending all that RF power through the rails to a motor (using iron cores) which wasn't designed for high frequency operation.

However, there is one reason for using (very) low frequency PWM (less than 100 Hz). Then, the PWM pulses give the motor individual "kicks" which can overcome static friction. Thus the motor can turn very slowly without stalling (i.e. with subjectively high torque). But in principle, it's possible to modulate high frequency PWM with low frequency pulses (rather like an IR remote control) to obtain the best of both worlds.

The required PWM duty cycle should be quite predictable. Normally, the effective dc voltage is simply the supply voltage multipled by the pulse period and divided by the overal cycle (i.e. mark + space) time. A "proper" filter will convert the PWM to dc, so the motor should behave exactly as if it were being fed from the corresponding dc voltage. The only exception is if the current falls to zero between every pulse (which is not good practice because it implies that the peak current must be at least double the average, or effective, current). But that's only likely to occur if using very low frequency pulses (e.g. to overcome stiction) or a badly designed or inadequate low-pass filter. ;)

Cheers, Alan.
 

joe paul

Senior Member
Hi Erco, InglewoodPete, Dartmoor, and AllyCat,

O.K., here is a video followed by my code:


Code:
setfreq m8  
 
main:
  
serin C.0, T9600_8,b0,b1 

if b1 = 48 then goto zero  
if b1 = 49 then goto one
if b1 = 50 then goto two
if b1 = 51 then goto three
if b1 = 52 then goto four
if b1 = 53 then goto five
if b1 = 54 then goto six
if b1 = 55 then goto seven
if b1 = 56 then goto eight
if b1 = 57 then goto nine
if b0 = 99 then goto Direction 
if b0 = 109 then goto ZeroZero
if b0 = 110 then goto ZeroReset

goto main 

zero:
pwmout B.6, OFF
Let b2=0
goto main
one:
pwmout B.6, 99, 200
Let b2=1 
goto main
two:
pwmout B.6, 99, 250
Let b2=2
goto main
three:
pwmout B.6, 99, 275
Let b2=3
goto main
four:
pwmout B.6, 99, 300
Let b2=4
goto main
five:
pwmout B.6, 99, 325
Let b2=5
goto main
six:
pwmout B.6, 99, 350
Let b2=6
goto main
seven:
pwmout B.6, 99, 375
Let b2=7
goto main
eight:
pwmout B.6, 99, 400
Let b2=8
goto main
nine:
pwmout B.6, 99, 450
Let b2=9
goto main
 
Direction:		; reverses leads to the tracks
 pwmout B.6, OFF
 pause 2000
 toggle B.4
 goto ZeroReset
  
 
ZeroZero:            ;PMW OFF
 
 pwmout B.6, OFF
 
 goto main
 
ZeroReset:           ; for release of STOP/direction button
 if b2=0 then goto Zero
 If b2=1 then goto One
 If b2=2 then goto Two
 If b2=3 then goto Three
 If b2=4 then goto Four
 if b2=5 then goto Five
 If b2=6 then goto Six
 If b2=7 then goto Seven
 If b2=8 then goto Eight 
 If b2=9 then goto Nine
    
    goto main
Well, it runs on DC; I wouldn't know how to use a TRIAC for AC. The handcar has a motor that is rated for 18 VDC; my power supply is 12VDC regulated (from an old scanner) and I tap the 4.5 VDC (for the HC-06 and the 18M2 board) off of it through a string of diodes. Using a MOSFET for the motor, reversing done through a 3 volt relay with a "snubber diode" across the coil (diode trick used here to reduce voltage to the relay and although I am using the Darlington array for the relay, the MOSFET goes directly to pin B.6 with 10K resistor on the pin to ground). Originally the handcar had a rectifier for AC/DC use (old Lionel backward compatibility issue), but I remove the rectifiers so I can get reversing on DC.

Let me have it -- I am braced!!! Used the PWM Wizard for the code, then tweaked it a little.

Thanks!!!!!!!!!

Take care, Joe.
 
Top