# Controlling a Servo with a BeagleBone Black

## Overview

In this tutorial, you will learn how to control a servo from Python using a BeagleBone Black (BBB).

![](https://cdn-learn.adafruit.com/assets/assets/000/009/653/medium800/beaglebone_project_big_servo_web.jpg?1396894751)

A Python test program will allow you to set the angle of the servo between 0 and 180 degrees.

```
# python servo.py 
Angle (0 to 180 x to exit):90
Angle (0 to 180 x to exit):180
Angle (0 to 180 x to exit):0
Angle (0 to 180 x to exit):x
# 
```

# Controlling a Servo with a BeagleBone Black

## You Will Need

To try out this tutorial, you will need:

The 1 kΩ resistor is not strictly necessary, but will protect your BBB from damage is something should go wrong in the servo.

# Controlling a Servo with a BeagleBone Black

## Installing the Python Library

This tutorial uses Ångström Linux, the operating system that comes pre-installed on the BBB.  
  
Follow the instructions here, to install the Python IO BBIO library. [http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black](http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black "Link: http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black")

# Controlling a Servo with a BeagleBone Black

## Wiring

Wire up the solderless breadboard using the header leads as shown below.

![](https://cdn-learn.adafruit.com/assets/assets/000/010/760/medium800/beaglebone_fritzing_big_servo.png?1378201406)

You can use male to male jumper wires from the screw terminal to the breadboard. Connect an external 5VDC power supply to DC power jack  
  
We will use pin P8\_13 as the PWM output to control the servo. The only other connection that we need from the BBB is GND.  
  
  
There is more information about all the pins available on the P8 and P9 connecters down each side of the BBB here: [http://stuffwemade.net/hwio/beaglebone-pin-reference/](http://stuffwemade.net/hwio/beaglebone-pin-reference/ "Link: http://stuffwemade.net/hwio/beaglebone-pin-reference/")

# Controlling a Servo with a BeagleBone Black

## The Python Console

Before writing a Python program to allow us to set the servo to an angle between 0 and 180, we can try some experiments in the Python Console.  
  
To launch the Python Console type:

```
# python
Python 2.7.3 (default, Apr  3 2013, 21:37:23) 
[GCC 4.7.3 20130205 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; 
```

First, we need to import the library, so enter the command:

```
&gt;&gt;&gt; import Adafruit_BBIO.PWM as PWM
```

Now enter the commands below into the Python Console one at a time, and you should see the servo change position.

```
&gt;&gt;&gt; PWM.start("P8_13", 95.0, 60)
&gt;&gt;&gt; PWM.set_duty_cycle("P8_13", 97.0)
&gt;&gt;&gt; PWM.stop("P8_13")
&gt;&gt;&gt; PWM.cleanup()
```

 **NOTE: If your servo doesn't move you might need to change the polarity of the PWM signal and try again.** Add a 4th parameter to the PWM.start function with a value of 1 (one), this will invert the PWM signal so it's low when normally high and vice versa.   
  
Here's the same code but with the inverse parameter set:```
&gt;&gt;&gt; PWM.start("P8_13", 95.0, 60, 1)
&gt;&gt;&gt; PWM.set_duty_cycle("P8_13", 97.0)
&gt;&gt;&gt; PWM.stop("P8_13")
&gt;&gt;&gt; PWM.cleanup()
```

# Controlling a Servo with a BeagleBone Black

## Writing a Program

Exit the Python Console by typing:

```
&gt;&gt;&gt; exit()
```

This should take you back to the Linux prompt.  
Enter the following command to create a new files called servo.py

```
nano servo.py
```

Now paste the code below into the editor window.   
  
**NOTE: Don't forget to add the inverse parameter to the PWM.start function if you found it was required to make your servos move in the previous page!**

```
import Adafruit_BBIO.PWM as PWM

servo_pin = "P8_13"
duty_min = 3
duty_max = 14.5
duty_span = duty_max - duty_min

PWM.start(servo_pin, (100-duty_min), 60.0)

while True:
    angle = raw_input("Angle (0 to 180 x to exit):")
    if angle == 'x':
        PWM.stop(servo_pin)
        PWM.cleanup()
        break
    angle_f = float(angle)
    duty = 100 - ((angle_f / 180) * duty_span + duty_min) 
    PWM.set_duty_cycle(servo_pin, duty)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/009/664/medium800/beaglebone_nano.png?1396894972)

To start the program, enter the command:

```
# python servo.py 
Angle (0 to 180 x to exit):90
Angle (0 to 180 x to exit):180
Angle (0 to 180 x to exit):0
Angle (0 to 180 x to exit):x
# 
```

Entering a value between 0 and 180 will set the servo's angle accordingly.   
  
When you want to stop the program, enter 'x'.  
  
You may find that your servo judders at one end of its range or does not give a full 180 degree range of movement. If this is the case, try tweaking the values in duty\_min and duty\_max.  
  
When you enter 'x', the PWM is stopped and 'cleanup' is run, otherwise the PWM signal would continue in the background even after the program had stopped running.

# Controlling a Servo with a BeagleBone Black

## Servo Motors

The position of the servo motor is set by the length of a pulse. The servo expects to receive a pulse roughly every 20 milliseconds. If that pulse is high for 1 millisecond or less, then the servo angle will be zero, if it is 1.5 milliseconds, then it will be at its centre position and if it is 2 milliseconds or more it will be at 180 degrees.

![](https://cdn-learn.adafruit.com/assets/assets/000/009/665/medium800/beaglebone_servos.png?1396895051)

Info: 

This example uses the PWM feature of the GPIO library to generate the pulses for the servo. The PWM frequency is set to 60 Hz so that the servo will receive a pulse roughly every 17 milliseconds.  
  
The length of the pulse is changed by adjusting the duty cycle over the fairly narrow range of 3 to 14.5 percent. These figures were estimated and then tweaked a bit to give a maximum range of the servo being used.

# Controlling a Servo with a BeagleBone Black

## Next Steps

If you wanted to, you could attach three more servos to the GPIO pins P8\_19, P9\_14 and P9\_16. They could all share the same external 5-6VDC power supply without any problem.  
  
  
  
**About the Author.**  
As well as contributing lots of tutorials about Raspberry Pi, Arduino and now BeagleBone Black, Simon Monk writes books about open source hardware. You will find his books for sale [here](http://www.adafruit.com/index.php?main_page=adasearch&q=simon+monk) at Adafruit.


## Featured Products

### BeagleBone Black - Rev B

[BeagleBone Black - Rev B](https://www.adafruit.com/product/1278)
**[Adafruit is no longer shipping the BeagleBone Black Rev B, it has been replaced with the Rev C as of 5/12/14](https://www.adafruit.com/products/1876) - the Rev C now has 4G flash and also comes with Debian, it also costs slightly more. There are no exchanges or...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1278)
[Related Guides to the Product](https://learn.adafruit.com/products/1278/guides)
### Standard servo - TowerPro SG-5010

[Standard servo - TowerPro SG-5010](https://www.adafruit.com/product/155)
This high-torque standard servo can rotate approximately 180 degrees (90 in each direction). You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without building a motor controller with feedback & gearbox. Comes with 3...

In Stock
[Buy Now](https://www.adafruit.com/product/155)
[Related Guides to the Product](https://learn.adafruit.com/products/155/guides)
### Micro servo

[Micro servo](https://www.adafruit.com/product/169)
Tiny little servo can rotate approximately 180 degrees (90 in each direction) and works just like the standard kinds you're used to but _smaller_. You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without...

In Stock
[Buy Now](https://www.adafruit.com/product/169)
[Related Guides to the Product](https://learn.adafruit.com/products/169/guides)
### Female DC Power adapter - 2.1mm jack to screw terminal block

[Female DC Power adapter - 2.1mm jack to screw terminal block](https://www.adafruit.com/product/368)
If you need to connect a DC power wall wart to a board that doesn't have a DC jack - this adapter will come in very handy! There is a 2.1mm DC jack on one end, and a screw terminal block on the other. The terminals are labeled with positive/negative assuming a positive-tip configuration...

In Stock
[Buy Now](https://www.adafruit.com/product/368)
[Related Guides to the Product](https://learn.adafruit.com/products/368/guides)
### 5V 2A (2000mA) switching power supply - UL Listed

[5V 2A (2000mA) switching power supply - UL Listed](https://www.adafruit.com/product/276)
This is an FCC/CE certified and UL listed power supply. Need a lot of 5V power? This switching supply gives a clean regulated 5V output at up to 2000mA. 110 or 240 input, so it works in any country. The plugs are "US 2-prong" style so you may need a plug adapter, but you can pick one...

In Stock
[Buy Now](https://www.adafruit.com/product/276)
[Related Guides to the Product](https://learn.adafruit.com/products/276/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Premium Male/Male Jumper Wires - 40 x 3" (75mm)

[Premium Male/Male Jumper Wires - 40 x 3" (75mm)](https://www.adafruit.com/product/759)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 3" (75mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/759)
[Related Guides to the Product](https://learn.adafruit.com/products/759/guides)

## Related Guides

- [SSH to BeagleBone Black over USB](https://learn.adafruit.com/ssh-to-beaglebone-black-over-usb.md)
- [Measuring Light with a BeagleBone Black](https://learn.adafruit.com/measuring-light-with-a-beaglebone-black.md)
- [Character LCD with Raspberry Pi or BeagleBone Black](https://learn.adafruit.com/character-lcd-with-raspberry-pi-or-beaglebone-black.md)
- [Bonjour (Zeroconf) Networking for Windows and Linux](https://learn.adafruit.com/bonjour-zeroconf-networking-for-windows-and-linux.md)
- [MPR121 Capacitive Touch Sensor on Raspberry Pi & BeagleBone Black](https://learn.adafruit.com/mpr121-capacitive-touch-sensor-on-raspberry-pi-and-beaglebone-black.md)
- [Introduction to the BeagleBone Black Device Tree](https://learn.adafruit.com/introduction-to-the-beaglebone-black-device-tree.md)
- [BeagleBone Black: Installing Operating Systems](https://learn.adafruit.com/beaglebone-black-installing-operating-systems.md)
- [LED Backpack Displays on Raspberry Pi and BeagleBone Black](https://learn.adafruit.com/led-backpack-displays-on-raspberry-pi-and-beaglebone-black.md)
- [Setting up IO Python Library on BeagleBone Black](https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black.md)
- [Using the BMP085/180 with Raspberry Pi or Beaglebone Black](https://learn.adafruit.com/using-the-bmp085-with-raspberry-pi.md)
- [BeagleBone](https://learn.adafruit.com/beaglebone.md)
- [Adding a Real Time Clock to BeagleBone Black](https://learn.adafruit.com/adding-a-real-time-clock-to-beaglebone-black.md)
- [RePaper eInk Development Board for ARM + GNU/Linux](https://learn.adafruit.com/repaper-eink-development-board-arm-linux-raspberry-pi-beagle-bone-black.md)
- [Adafruit WebIDE](https://learn.adafruit.com/webide.md)
- [TMP006 Temperature Sensor Python Library](https://learn.adafruit.com/tmp006-temperature-sensor-python-library.md)
