Servo Control with PICAXE-08 (NOT M)
July 31, 2009
One for TofuRobot.
1. The electronics:-
The Diamond on the left is the Picaxe pin, I normally use a resistor 100-200R just to protect the Micro just incase of short or plugging the servo in backwards (there are two types of people using servos, those who have plugged servos in backwards and those who are about too!).
Pin 1 is the signall and on the servo it will be Yellow or someing like that, 2 is the power between 4.8-6Volts is ok, it is often red, and the last (3) is ground and is normally black.
2. The Code
The PICAXE-08 Lacks the servo function in the language so we are going to need to do this by hand.
The first thing you need is an outside loop of between 18 and 25ms, the actual time is not all that critical, the servos will jitter if too long.
The position of the servo is set using Pulse Width Modulation, the pulse is between 1ms and 2ms and the width of the pulse sets the position.
The PICAXE-08 does have a pulsout function which takes two parameters:
pulsout pin, duration
here is the info from the Pixaxe manual
The pulsout command generates a pulse of length time. If the output is initially low, the pulse will be high, and vice versa. This command automatically configures the pin as an output, but for reliable operation on 8 pin PICAXE you should ensure this pin is an output before using the command.
Restrictions: The 14M only supports pulsout on outputs 0,1,2.
Affect of Increased Clock Speed:
4MHz 10us unit
8MHz 5us unit
16MHz 2.5us unit
So the code is going to look like so:
main: `this is our outside loop
pulsout 1,150 ‘ send a 1.50ms pulse out of pin 1
pause 20 ‘ pause 20 ms
goto main ‘ loop back to start
Now the caveats! ther are a few.
a. Outside loop time is governed by our code, you will have to adjust the pause duration to keep the loop time between 18-25ms
b. Loop time is set also by the pulse duration of the pulsout command, I dont think it returns until the pulse is over.
c. Debugging, single stepping is not going to work well ;-)
I hope that helps