PWM motor speed control?

teraflop122

New Member
I've been trying to control motor speed using PWM for quite a while now without success, and I just can't pin down the problem. I'm using a PICAXE 40X1 and the "Standard Circuits 2 - Darlington Driver IC" circuit straight out of manual 3. The motor terminals are bridged by a 0.1uf capacitor.

Code:
symbol timer3 = w2

main:
	pwmout 2, 255, 1
	for timer3 = 1 to 1023
		pwmduty 2, timer
		pause 20
	next
Rather than ramping up or down, the motor remains off for several seconds, then jumps directly to full speed. I've tried the same code with a L298 (testing one motor, one direction) and a simple 3904 transistor switch. All produce the same effect.

Code:
pwmout 2, 255, 1
Code:
pwmout 2, 255, 400
Code:
pwmout 2, 255, 800
When using the above lines of code by themselves, the motor jumps immediately to full speed and stays there.

Am I missing something obvious here?
 

boriz

Senior Member
Try putting a 1K resistor from the PICAXE output pin to ground. Does that make any difference?
 

inglewoodpete

Senior Member
There are a number of possibilities. Where inductors and/or commutators are involved, the most likely is that the PICAXE is unstable.

Without changing anything else in your wiring layout, connect a LED + resistor (Eg 330ohms or thereabouts) to a spare output pin. Flat side of LED (cathode) towards the PICAXE and the other towards +5v but with the resistor in series, of course.

I say "Without changing anything else in your wiring layout" because it is important to eliminate potential problems systematically. This helps with the learning process.

Change your code as follows:
Code:
symbol LED1 = x        'Where x is the pin that the LED is connected to
symbol timer3 = w2

Init:	PulsOut LED1, 20000    'If this LED flashes or stays on, the PICAXE has restarted
main:
	pwmout 2, 255, 1
	for timer3 = 1 to 1023
		pwmduty 2, timer
		pause 20
	next
             Goto Main
If the LED flashes or stays on, then the PICAXE is restarting. The most likely cause is electrical noise from the motor getting through the power supply.

Let us know how this goes. There are lots more pitfalls that you may encounter:)
 

Dippy

Moderator
Good plan Pete. I haven't checked code but I'd guess that its about 4kHz?

My limited experience has shown that much lower PWM frequencies give better motor response. But that's another topic.

TF: - Also you haven't told us about your power supply or prototype construction.
What size/type / voltage rating for motor?
Is the motor OK?

For example, if the Power supply is floppy the motor starting may cause little dips causing instability in the PIC.

Try a capacitor directly across PICAXE power pins. Try 100nF (0.1uF) and, if you've got one a 100uF.

Try a diode reverse biased across motor terminals.

Try reducing the value of the capacitor across the motor.

Try putting a debug or sertxd in your loop to see at what point the nothing-to-full happens.

Can you post your schematic precisely just so we can see if there is an issue?
 

teraflop122

New Member
Boriz: A 1k resistor connecting the output pin to groun has no effect.

Pete: Yeah, the LED stays lit. With the new code, the motors don't jump to full speed either- nothing happens at all... The LED stays on with the motor disconnected as well :confused:

On closer observation, the LED flashes once, stays dark for a fraction of a second, then lights and stays lit indefinitely, even with the motor disconnected.

Dippy: The motors are 6v with ~600mA stall current from solarbotics. The power supply is a 5v LDO regulator from parallax with a 25uF capacitor bridging the positive and negative output. Swapping out the 25uF cap for a 0.1uF cap doesn't change the on-off-stayon behavior I described.

Placing a diode reverse biased across the motor terminals doesn't change the on-off-stayon behavior.

Add sertxd just adds to the mystery.

Code:
symbol LED1 = 4
symbol timer3 = w2

init: pulsout LED1, 20000
sertxd ("-------------Init------------- ") 
main:
sertxd ("Main ")
	pwmout 2, 255, 1
	sertxd ("pwmout ")
	for timer3 = 1 to 1023
		sertxd ("for-next start ")
		pwmduty 2, timer
		sertxd ("timer incriment ", #w2)
		pause 20
	next
goto main
Even though the LED remains lit, the program continues to run, and timer increments correctly. The program never returns to Init, so I'm not sure why the LED would remain lit.

I'll get a schematic drawn up.

Thanks for all the help, BTW :)

 
Last edited:

Dippy

Moderator
Replace the motor with a small light bulb. Try that. What happens if you increase Pause 20 to Pause 200 say for example?

Can you use another image hosting thing please, like TinyPic.
I get fed up Poker pages coming up from Imageshack so I didn't see the schematic.
 

BeanieBots

Moderator
Got a part number for the 5v LDO or better still a spec?
Only 1v of headroom and only 25uF reservoir is not much when switching on a motor. Could well be power rail collapsing.
No decoupling for the 40X1. Asking for trouble at the best of times!
 

MPep

Senior Member
I would seperate the power lines between the PICAXE and the motor, i.e. have 2x 5V regulators.
The other possibility is that there is not enough current for the motor to work. Have you checked the current draw at 5V? Motor only that is.

Is the motor indeed rated at 5V only? What about running this of the 6V supply?
 

hippy

Technical Support
Staff member
Code:
for timer3 = 1 to 1023
		pwmduty 2, timer
Is that PWMDUTY command meant to be using 'timer' or should it be using 'timer3' ?
 

teraflop122

New Member
Is that PWMDUTY command meant to be using 'timer' or should it be using 'timer3' ?
....Wow. I can't believe I made such a stupid mistake. That solves half the problem. The ramping code works now, however issuing a pwmout command by itself does nothing.

This code causes the motor to ramp up slowly, then drop and start over again like it's supposed to:
Code:
symbol LED1 = 4
symbol timer3 = w2

init: pulsout LED1, 20000
sertxd ("-------------Init------------ ") 
main:
sertxd ("Main ")
	pwmout 2, 255, 1
	sertxd ("pwmout ")
	for timer3 = 400 to 1023
		sertxd ("for-next start ")
		pwmduty 2, timer3
		sertxd ("timer incriment ", #w2)
		pause 20
	next
goto main
This code will make the motor run at near-full speed continuosly, because timer3 just jumps back and forth between 1022 and 1023:
Code:
symbol LED1 = 4
symbol timer3 = w2

init: pulsout LED1, 20000
sertxd ("-------------Init------------ ") 
main:
sertxd ("Main ")
	pwmout 2, 255, 1
	sertxd ("pwmout ")
	for timer3 = 1022 to 1023
		sertxd ("for-next start ")
		pwmduty 2, timer3
		sertxd ("timer incriment ", #w2)
		pause 20
	next
goto main
Yet the following code doesn't do anything at all...:
Code:
Main:
	pwmout 2, 255, 1023
Or:
Code:
Main:
	pwmout 2, 255, 1
	pwmduty 2, 1023
Why would the motor run with pwmduty jumping between 1022 and 1023, but not with a single "pwmout 2, 255, 1023" command?

Sputz: I don't see how it would help, as I'd have no way of knowing whether the motor was running correctly. Unless I'm grossly mistaken, none of the picaxe chips can directly drive a motor.

BeanieBots: The spec sheet for the 5v LDO regulator is here. I hear what your saying about the capacitor being a little on the small side. As for no decoupling, are you saying not to use decoupling, or that with the capacitor so small I'm practically not decoupling?

MPep: I haven't directly measured the current at 5v, but I do no the stall current to be about 600mA at 6v. With the L298 H-Bridge, the motor supply current will come directly from the battery instead of sharing the 5v regulator.

Edit: Now I'm just confused. Putting the pwmout command in a do-loop makes the motor run continuously. I thought pwmout ran continuously in the background? Why would I have to loop it?

With this code, nothing happens until the picaxe is restarted, then the motor runs full:
Code:
Main:
	pwmout 2, 255, 1
	pwmduty 2, 400
In this code the motor runs at a very low speed as it should:
Code:
Main:
	pwmout 2, 255, 1
             Do
	           pwmduty 2, 400
             Loop
Also, I'll see what I can do about finding an alternative host for the schematic image. It's just too bad the forum restrictions on pixel x pixel size are so extreme.

There we go.

 
Last edited:

boriz

Senior Member
With those examples you say fail. Put DO:LOOP at the end of each and try again. Otherwise the parser just falls off the end of the program and all processing stops (I think), including PWM servicing.
 

MPep

Senior Member
teraflop122 said:
BeanieBots: The spec sheet for the 5v LDO regulator is here. I hear what your saying about the capacitor being a little on the small side. As for no decoupling, are you saying not to use decoupling, or that with the capacitor so small I'm practically not decoupling?
What BeanieBots is saying is that there is not enough capacitance. add, perhaps, 470uF, or even 2200uF close to the motor/ULN device.
Keep the 25uF near the X1 device. Might pay to add a 100nF capacitor beside it also. This is for high frequency suppression on the power rails.

In your code you have:
Code:
Main:
	pwmout 2, 255, 1
	pwmduty 2, 1023
From the manual, you should probably have:
Code:
pwmduty 2,255,1023
Please correct me if I am wrong as I have not used PWM commands.
 

teraflop122

New Member
With those examples you say fail. Put DO:LOOP at the end of each and try again.
Thanks boriz, that worked perfectly! I guess it even comes close to maybe making some small semblence of sense, too. All I know right now, is that this 11 dollar chip is turning out to be a much better value than a Basic Stamp 2 + Pololu motor controller.

I'll try to scrounge up a bigger capacitor and put the H-Bridge back in tomorrow.

Mpep: the pwmduty command was correct. The period is set by a pwmout command before it.

Thanks for your help, everyone. I honestly never would have figured this out on my own. :)

Edit: of course you'll hear from me tomorrow unless all goes well....but does all ever go well?
 
Last edited:

BeanieBots

Moderator
Mpep has correctly answered on my behalf.
Just to clarify.
When a program ends, ALL processing stops, that includes PWMout. Hence the need to keep a loop going.
Decoupling means what the word actually says. To break the link between components. This is done by the use of capacitors. The position of the capacitor on the power rail is actually more important than its value.
You should have about 100nF as close to the PICAXE power pins as possible.
The tests already suggested should let you know if decoupling or excessive loading is the problem. The regulator is close to the mark but should be just about OK.
I'd also suggest fitting about 470uF (min) on the input side of the regulator and 470uF close to the point at which power for the motor is taken.
 

boriz

Senior Member
If you consider that all conductors, including the tracks in your circuit and the legs on your components, have resistance, then you can see how putting the bypass capacitor close to your noise-sensitive-component forms a kind of low-pass filter - Dumping the high frequency currents straight to ground and causing them to be dissipated in the resistance of the upstream part of the power lines.

Note. Since you are using PWM, essentially an AC power system with lots of high frequency harmonics, any capacitance you add downstream of the ULN2803 will reduce efficiency, dumping part of your drive current to ground and heating up your ULN2803.
 
Last edited:

MPep

Senior Member
@BB: didn't mean to tread on your toes or anything like that. Just trying to help.:D

I like Boriz's explanation too.
As an extension to his explanation: the tracks can also be seen to have inductance, and again we end up with as low-pass filter filter. Heck, even a Pi-filter depending on how far the capacitors are seperated.

This is very copmplex to analiyse and wouldn't recommend doing so, unless you really, really, really like Maths!

@Boriz: I meant for the capacitor near the ULN device to be close to this, NOT across the motor.
Putting a capacitor across the load would indeed cause further problems.
 

BeanieBots

Moderator
@Mpep, no problem. The quicker a question is answered the better.

As you say, full explanation of cap/track/load interaction would be beyond the scope of any forum but a little understanding and a simple appreciation that components are not perfect will suffice in most cases.

Caps on motor. These ARE required for suppression. Generally, the most effective is to use 3 off 100nF. One between the terminals and one between each terminal and the case.
 

eclectic

Moderator
In the datasheet for the LM2940, page 1 “Typical Application”,
it mentions a capacitor of >22uF, with the words

“ESR is critical”

The graph on page 10, Output Capacitor ESR,
shows the very low Ohm values required.

Might this also be important for your project?

e.

Possibly something like this?

http://uk.farnell.com/1217614/passives/product.us0?sku=sanyo-20sp22m
I see that it's about x20 times the price of a cheapy!
 
Last edited:

teraflop122

New Member
I've updated my schematic with the suggested capacitors in place, and with a H-Bridge in place of the Darlington array. As for the actual circuit, I'm hardpressed to find the capacitors. Might have to make a run to Radioshack. I tend to avoid soldering directly to a motor's casing because my iron isn't up to the job.

eclectic: Your link leads to a page listing countries and companies.

The H-Bridge enable and current sensing pins aren't shown in the schematic. Enable will be connected directly to positive and current sensing directly to ground for now. With the H-Bridge in place, the 5v regulator will only be powering the picaxe and any sensors added later.

 
Last edited:

BeanieBots

Moderator
That's a good STARTING point to work from.
Should be OK but take a little care over layout/wiring so that the caps do their job properly. Make sure the PICAXE does not share any power lines with the supply to the H-bridge.
Might also be worth changing the two 2k//2k for 1k and the three 100Rs for 330R. (470R to about 1k8 is fine for most LEDs).

Try without the caps to motor case while you save up for a decent iron;)
Might be OK without but at the first sign of any unexpected resets, replace motor with a bulb. If the problem goes away, go and get that iron.

Good luck, let us know how you get on.
 

teraflop122

New Member
The H-Bridge appears to be running correctly. When running a standard test for directional control, the picaxe reset twice, but I forgot to stop one of the pwm outputs, and fixing the code seems to have fixed the problem.

Of course one motor isn't going to cut it for mobile robots. Are there any special considerations for hpwm output? I've tried and failed with simple things like:

Code:
main:
         hpwm 0, 0, %0100, 255, 400
         do
         sertxd ("running...")
         loop
In this case I used the same circuit as shown in my previous post, except with the H-Bridge inputs connected to hpwm B and C. The program runs, but the motor doesn't respond.

BeanieBots: the motor supply for the H-Bridge comes directly from the battery. However, the logic supply is on the same rail as the Picaxe. I have all the capacitors in the above schematic in place. According to the L298 datasheet, additional 0.1uf capacitors are needed for the H-Bridge's individual logic and motor supply inputs, but I currently have none extra on hand.

My robot seems to be sprouting capacitors all over the place, yet the picaxe starter kits appear to only have a few. The 28x starter pack has only one that I can see, and it even includes a slot for an H-bridge! Are breadboards THAT much worse than circuitboards?

eclectic: Yeah, few of the numbers on that page mean anything to me. Also, when I see hundreds of people around me having no trouble with common ratshack parts, I'd consider having to buy some special part to be a personal failure. Once again the apparently simply 28X starter pack comes to mind.
 
Last edited:

gbrusseau

Senior Member
The specs on the 5 volt regulator says the drop out is .5 volts to 1.0 volts max. With a 6 volt battery, even a small discharge of the battery and with the motor running, I would think would probably cause the regulator to shut down momentarily. Try using a 7.5 volt battery (five cells instead of four) at least until all the bugs are worked out of the hardware and code.
 

BeanieBots

Moderator
Are breadboards THAT much worse than circuitboards?
Stan, put your fingers in your ears.

There are many circuits which will not work at all when built on breadboard.
A lot depends on what you want to do and what is critical to that circuit.
Contact resistance can be very high. Hence, anything which requires high accuracy or low impedance will have problems. The capacitance between 'tracks' is high. Hence, noise will be coupled to most of your circuit. Makes RF projects almost impossible. (that's discrete RF, not sticking in a module).
If you are into SM power supplies, forget using breadboard altogether.

When NOT to use a breadboard.
If any of your CRITICAL components:-
a) Require low ESR.
b) are < 10R
c) are > 10M
d) < 1nF
e) I > 500mA
f) V > 32V
g) layout critical (goes without saying).

You can still use breadboard for much of the above but you will need to take into account the fact that it is on breadboard and compensate accordingly.

Stan, fingers out now.

For education, playing with LEDs and slow digital work, breadboard is a godsend. I do own several of them and I do use them.
 

Dippy

Moderator
"If you are into SM power supplies, forget using breadboard altogether."

- oh my Goodness. If Stan sees this he'll be making an effigy of BB and poking transistor leads into it. Then covering it in red and black felt tipped pen markings, and then putting 5 amps through it just to prove it can handle >500mA.

I agree with BB, but must confess my first S/mode basic proto was on breadboard and I bunged 4 amps through it for several minutes. Ooops. But then , of course, my Breadboard wasn't made in C....

I wonder if Stan has found a beadboard for surface mount yet?
 

BeanieBots

Moderator
I'm sure Stan will dig out a photo of his 1kW UHF transmitter powered by SM all built on breadboard;)
I can feel those transistor legs digging into my eyballs already:eek:

Breadboard has its place and it's great for some projects. There are simply some places it shouldn't go. I do use it. Honest.
 

teraflop122

New Member
Well I've given up on hpwm for now. Instead I'm using four npn transistors connected to standard output pins to direct the two pwm outputs to the correct H-Bridge inputs. The setup is working very well. Finally, I have the base of a robot which can move at varying speeds in both directions, without suffering from catastrophic interference problems or shutting down after half a second of use!

Thanks for the help everyone. If anything, I came away from this discussion knowing a bit more about decoupling capacitors :)
 

boriz

Senior Member
Two of the same capacitors in parallel have the equivalent capacitance of the total and an ESR of half. Sometimes you don’t need expensive low ESR caps, you can just parallel some instead. EG: 3 capacitors of 100uF and 3ohm ESR when paralleled, act as one capacitor of 300uF and 1ohm ESR. They will also divide the work between them so they should last longer than a single equivalent capacitor.

It looks like your PWM is about 4Khz. If it were a sine wave, that would mean that the 100n capacitor across your motor has an impedance of about 400ohms and will dump around 12mA of current at 5v (over 60mW of wasted power, more if you are using higher motor voltage). But since your 4Khz signal is actually a square wave, you can expect those figures to be higher, maybe double or more (I forget the math, something about harmonics). Double again for 2 motors. In theory, you could be wasting 50mA or more just on two little suppressor caps. Personally I would choose the smallest capacitor possible consistent with whatever suppression you need.
 
Top