Servos

Is it possible to connect 2 mini servos on M2+ M2- {motor 2} & M3+ M3- {motor 3}?
If so, can it be controlled using ReverseKeyBorg using W, A, S, D keys?
This will be a perfect setup for the Raspberry Pi Camera!

piborg's picture

It should be possible to control RC servos using PicoBorg with any of the four channels, but the connections will be different from normal.

The servo supply (red) needs to be connected to M+, this means the supply to the PicoBorg needs to be the correct voltage for the servo (e.g. 5v).
The servo ground (black) should be connected to GND.
The servo signal / data (yellow or white) should be connected to M-, along with a weak pull-up resistor to M+ (say 4.7K or 10K)
See the attached image for clarity.

What you will need to do is PWM the GPIO pin for the corresponding motor channel to control the servo position, 0 % PWM will be fully one direction, 50 % PWM will be central, and 100 % will be fully the other direction.

Any of the channels can be controlled in Python using wiringPi as follows:

import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()
M1 = 7
M2 = 1
M3 = 10
M4 = 11
wiringpi.softPwmCreate(M1, 0, 100)
wiringpi.softPwmCreate(M2, 0, 100)
wiringpi.softPwmCreate(M3, 0, 100)
wiringpi.softPwmCreate(M4, 0, 100)
wiringpi.softPwmWrite(M1,50)     # 0 to 100
wiringpi.softPwmWrite(M2,50)     # 0 to 100
wiringpi.softPwmWrite(M3,50)     # 0 to 100
wiringpi.softPwmWrite(M4,50)     # 0 to 100

This will use software control to turn the line on and off at a fast rate, providing the control signal.

M2 is special, it is connected to the hardware based PWM, which should be more precise.
It can be used in hardware mode as follows:

import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()
M1 = 7
M2 = 1
M3 = 10
M4 = 11
wiringpi.softPwmCreate(M1, 0, 100)
wiringpi.pinMode(M2, 2)
wiringpi.softPwmCreate(M3, 0, 100)
wiringpi.softPwmCreate(M4, 0, 100)
wiringpi.softPwmWrite(M1,50)     # 0 to 100
wiringpi.pwmWrite(M2, 512)       # 0 to 1024, 512 is central
wiringpi.softPwmWrite(M3,50)     # 0 to 100
wiringpi.softPwmWrite(M4,50)     # 0 to 100

This is the preferred way of operating M2, however if you are using two or more servos it may be easier to use the first method.

This could be added in to one of the existing PiCy scripts which is not already using M2 or M3, but it would require code to be added to those scripts.

Images: 

Thanks Piborg! I can tryout the wiring.

One big problem is I'm not a programmer and I want to control these servos using AWSD keys together with the arrows keys on ReverseKeyBorgC.py :(

Subscribe to Comments for "Servos"