Monday, June 23, 2014

Lets Control a Servo


Well hello there. In this tutorial I will be walking you through the process of controlling a continuous servo. How you control a continuous servo and a regular servo are the same.


  1. You have a BBB RevB (I don't know if the stuff I say or do will work on other version)
  2. You have already setup your OS on the BBB and have it configured the way you want. I have a tut for getting a TP-Link TL-WN722N USB wireless working if you want to do wireless. 
  3. You have a basic understanding of circuits. I probably won't do circuit diagrams but you never know I might get creative. 
  4. You know Python 2.X it is different than Python 3.X
  5. You have some type of bread board and jumper wires.
  6. You have installed the Adafruit Python Library

You will need

Your Bread Board, the Servos, 2 1 kΩ Resistor, 5v power supply, and Bread Board Jumper wires.


Connect the 5v power supply (upper right corner of picture) to the bread board's (bb) positive (red) and negative (blue/black).
Do once for each servo: 
  • Connect the servo's positive (red) to the bb's positive (red).
  • Connect the servo's negative (black) to bb's negative (black).
  • Connect the servo's control wire (Yellow) to one side of the bb
Put the 1kΩ Resistor in the middle of the 2 section of the bb to join them
Connect "P9_14" to one resistor and "P9_21" to the other.
Connect the Ground from the BBB to the negative (blue) of the bb and then run another wire across the bb so that you connect the ground of the BBB the the ground of the 5v power supply to complete the circuit.

The reason for the resistor is to make sure that the servo does not exceed the 250mA current that the BBB can handle.

Python Servo Code

import Adafruit_BBIO.PWM as PWM

servo_pin = "P9_14"
servo_pin2 = "P9_21"
Above we are importing the Adafruit library and setting up global variables the pins we are going to use.

PWM.start(servo_pin, (100), 60.0)
PWM.start(servo_pin2, (100), 60.0)
Now we setup the BBB so it knows which pins to us and that those pins are going to be used as PWMs. We are using the PWM.start method. This functions will take 4 parameters: channel, duty, frequency and polarity. The last 2 parameters frequency and polarity have default values thus are not required. The defaults are 2000 for frequency and polarity's is 0. Most servo's work with 50 and/or 60 Hz so I set the frequency to 60 Hz.
The first 2 parameters are required. The first one is the pin to be setup so 'P9_21' and 'P9_14'. The second one is the duty cycle of the pulse. The duty cycle is how long the pulse will be active. In this case we are setting it to 100 duty cycles which for my continuous servo means DON'T MOVE.

Note there are only 8 PWM pins P9_14, P9_16, P9_21, P9_22, P9_28, P9_29, P9_31, P9_42, P8_13, P8_19, P8_34, P8_36, P8_45, and P8_46 on a BBB RevB

PWM.set_duty_cycle(servo_pin, 0)
PWM.set_duty_cycle(servo_pin2, 0)
After we get it all setup we can change the direction and speed of the servo by using the method set_duty_cycle. It takes in the pin you want to change and the new duty cycle. By setting it to 0 the servo will now move clockwise at full speed. You can play around with your servo(s) to see what different duty_cycle values do on your servo.

Now the final code
import Adafruit_BBIO.PWM as PWM

servo_pin = "P9_14"
servo_pin2 = "P9_21"

PWM.start(servo_pin, (100), 60.0)
PWM.start(servo_pin2, (100), 60.0)

while True:
    speed1 = raw_input("Angle (0 to 100 x to exit):")
    speed2 = raw_input("Angle (0 to 100 x to exit):")
    if speed1 == 'x' or speed2 == 'x':
    speed_f1 = float(speed1)
    speed_f2 = float(speed2)
    duty1 = 100 - speed_f1
    duty2 = 100 - speed_f2
    PWM.set_duty_cycle(servo_pin, duty1)
    PWM.set_duty_cycle(servo_pin2, duty2)


No comments:

Post a Comment