Ceci est une ancienne révision du document !
This month, we are going to interface a servo motor to our RPi. This requires only a servo motor, the Raspberry Pi, the breadboard, some jumpers, and the battery pack we used last month. A servo motor is simply a motor that has a control circuit and a potentiometer to give the control circuit the position of the output shaft. MOST servos will rotate the shaft between 0° and 180°. There are some that go 360° and more, but they cost a lot. Gears do all the connections between the motor, the potentiometer, and the output shaft. We provide the power through batteries or another external power source, and the RPi will send out the control signals. Most servos have only three wires, Positive voltage, Negative voltage (ground) and Control Signal. Colors of the wires vary from manufacture to manufacture, but the voltage wires should be close in colour to red and black, and the control wire is the only one left. When in doubt, check for a data sheet from the manufacture. The control signals are expected in a very specific “format”, and we will use PWM (Pulse Width Modulation) for that. First, the pulses must come every 20 milliseconds. The width of the pulse determines where the output shaft turns to. If the pulse is 1 ms in width, the motor will move toward 0°. If the pulse is 1.5 ms, then the shaft moves toward 90°. If the pulse is 2 ms, the shaft moves toward 180°
The Wiring The connections are very simple this month. The battery pack powers the motor, so +voltage on the servo goes to the + voltage rail, and the negative servo wire goes to the negative rail. We connect the negative voltage from the battery pack (negative rail) to pin 6 of the RPi. GPIO pin 23 (pin 16) goes to the control wire of the servo. Now for some math. As we discussed earlier, the servo expects a signal every 20 ms in order to work, and we need to keep sending those pulses to keep the output shaft in the position that we want it in. The GPIO command to set the Pulse Width Modulation is Pwm = GPIO.pwm({RPi Pin},{Frequency}) We know the pin number (23), but we need to convert the 20 ms to Hertz in order to set the pwm setup command. How do we do that? It’s simple. Frequency = 1/time Frequency = 1/.02 (20ms) Frequency = 50 Hertz So now, when we set up our code, we can set the GPIO.pwm command to the control pin and use 50 for our frequency.
Our first program will start somewhere close to 0° and then move to close to 90° then move to close to 180°. I keep saying close, because every servo is a bit different. We don’t want to set the DutyCycle to 0 and have it slam to the limit and potentially damage the servo, so we will start off with a low number close to 0 and end with a number close to 12 in order to start to “dial in” a set of values that work. For my servo, the numbers 3 for 0° and 12 for 180° worked well. Servo1.py import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setup(23,GPIO.OUT) pwm = GPIO.PWM(23,50) pwm.start(2) pwm.ChangeDutyCycle(3) sleep(5) 3 should give you the first angle. You might need to try changing it to 2,1,0 or 4 to get it there. Write down this number. pwm.ChangeDutyCycle(6) sleep(5) This should put the rotor into the centre position (90°). If you have changed the first number or the next number, this will have to change as well.
pwm.ChangeDutyCycle(12) sleep(5) The final number should take you to the 180° position. Again, try a few numbers one side or the other to tweak this, and once you have it, write down the number. GPIO.cleanup() Finally we call GPIO.cleanup() to set everything to normal. Now comes the heavy duty math part. We will use the values 3 and 12 as y1 and y2 respectively for the formula. Substitute your numbers.
Offset = (y2-y1)/(x2-x1) Offset = (12-3)/(180-0) Offset = 9/180 Offset = .05 Now when we set the DutyCycle based on an angle between 0° and 180° we use the following formula DutyCycle = (Offset * angle) + 2.0 DutyCycle = (.05 * angle) + 2.0 When I did this, it worked, but I found the value of .061 worked a little bit better. Well, that’s about it for this month. Next time, we will be working with a stepper motor, somewhat a cross of both a servo and a regular motor. Until then, enjoy!