Picaxe motor control using a PWM
(Pulse Width Modulation)
The Picaxe is a easy way to create a controller for a motor. It has a pwm built in it to make the programming easier.
The Picaxe have two PWM commands
pwm and
pwmout
Only the ‘08’ and ‘08M’ have the pwm command.
I would not recommend using the
pwm command as it is not designed for controlling motors and all other commands can affect this type of pwm functions if used.
On the other hand
pwmout command is designed to operate in the back ground and there for will not be affected bye other programming commands.
Note: - If you’re using the setfreq command, or an 8MHz resonator, this will cause problems with the pwmout command. PWM frequency is double when running at 8MHz. This is not recommended to start with.
Note: - All information within this page is for a single direction motor controller.
refer to {page to be found} for bi directional motor controllers.
Command info
PWMOUT pin,period,duty cycles
- Pin is a variable/constant, which specifies the I/O pin to use.
(Only 3 on 18X, only 2 on 08M, 1 or 2 is available on 28X/40X)
- Period is a variable/constant (0-255), which sets the PWM period
(period is the length of 1 on/off cycle i.e. the total mark: space time).
- Duty is a variable/constant (0-1023), which sets the PWM duty cycle.
(Duty cycle is the mark or ‘on time’)
Figure 1
Something to remember is that the 'Period' value of the
pwmout command used a byte.
Where the 'Duty Cycle' value uses a word. A word is 2 bytes.
Byte = 0 to 255 value (hex FF)
Word = 0 to 1024 value (hex FFFF)
This is very important to remember, if using a byte for the duty cycle the pwm will never get to 100% of it's duty cycle. Motor will never get to its max rpm.
Calculating Period
The first thing needed, is what frequency are you going to run the motor at.
A lot pf pwm controllers run at 3KHz. Now the Picaxe can't do this so 4KHz is a good starting point.
Period = 249 (4KHz)
Most motors will work well around 4KHz frequency.
Formula for calculating period: -
((Clock speed MHz x 250000) / PWM Frequency) - 1 = Period
Clock speed MHz = the resonator frequency of the Picaxe. Normally 4MHz
PWM frequency = PWM output frequency
Period = Period value for the Picaxe (0 to 255)
Actual calculation: - ((4 x 250000)/4000)-1 = 249
Duty Cycle
The next item on the agenda
What is the minimum duty cycle required to start the motor running, without pausing?
If the starting duty cycle is to low, the motor could be in a stalled state. Drawing a lot of current. This is not good.
The motor could also be jerky due to a low duty cycle. This is also to be avoided.
The motor should be running smoothly.
I would recommend starting with a duty cycle of 200. This is about a 20% duty cycle.
Then during testing start to lower the duty cycle value to determine the minimum duty cycle.
Now you might want to use a current meter here, as another thing to look out for is high current draw.
Something that might be found when testing is that at a certain duty cycle the current draw is higher than it should be.
By increasing the duty cycle a little could reduce the current draw considerably, but not change the rpm much. The lower current draw is the desired minimum duty cycle to use.
PULSEIN and PWMOUT
Now that you're got the period and minimum duty cycle required for running the motor. How do you get the
pulsein value to change the duty cycle of the
pwmout
One problem that is accounted is that the
pulsein from a radio control unit works from a
pulsein value of 100 to 200.
If the
pulsein value is less than 100 or greater than 200 then it is classed as a failsafe condition
You could do this.
pulsein value + min duty cycle = Duty cycle
200 + 200 = 400 (this is about a 40% duty cycle)
The only problem here is that the maximum duty cycle of 1024 would never be reached. The motor would never run at its maximum rpm. This is not a good idea if the controller is for a weapon.
So how can we get around this problem, easy just with a couple of basic formulas!
First needed is the workable pulsein value. We know that a remote control unit operates from 100 to 200. Therefore the working value is 100.
Not quite. If this was used it would make the control of the motor very twitchy and some radio units don't get their full range.
One requirement is a dead band for the off position. 130, is a good on value. 130 suits my radio unit.
The other one is that some radio units don't go all the way to 200. I would suggest dropping the max value to 190.
This means the working pulsein value is from 130 to 190.
As we now have the min duty cycle, max duty cycle, and working pulsein range, we are able to work out a step size for the duty cycle increments.
Pulsein low = 130
Pulsein high = 190
Min duty cycle = 200
Max duty cycle = 1024
We need to know the pulsein range and the duty cycle range.
Formulas: -
pulsein high - pulsein low = pulsein range
190 - 130 = 60
max duty cycle - min duty cycle = duty cycle range
1024 - 200 = 824
With is we can calculate the duty cycle step
Formula: -
Duty cycle range / pulsein range = duty cycle step
824 / 60 = 13.73
This duty cycle step can be used to increase the duty cycle. But the Picaxe doesn't like decimals,
I would recommend, rounding up to 14. This will resolve the problem.
Using duty cycle in the program
To be completed