Using the Adafruit Library

Author Gravatar Image KEVIN TOWNSEND
The Python code for Adafruit's PWM/Servo breakout on the Pi is available on Github at  https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code

This code should be a good starting point to understanding how you can access SMBus/I2C devices with your Pi, and getting things moving with your PWM/Servo breakout.

Before you start, you'll need to have the python smbus library installed, run apt-get install python-smbus

Downloading the Code from Github

The easiest way to get the code onto your Pi is to hook up an Ethernet cable, and clone it directly using 'git', which is installed by default on most distros.  Simply run the following commands from an appropriate location (ex. "/home/pi"):
Copy Code
$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
$ cd Adafruit-Raspberry-Pi-Python-Code
$ cd Adafruit_PWM_Servo_Driver 
Original (256MB) Raspberry Pi's use I2C bus 0, while Second Revision Pi's use I2C bus 1. The code attempts to determine the version of Pi it's running on, and will set the I2C bus automatically.
In rare cases, the automatic detection can fail. If you see an error message telling to to "check your I2C address", you can modify the code to specify which I2C bus to use.
If you see the "check your I2C address" error message...

First, make certain that you have configured the right I2C address in Servo_Example.py, and that the Servo Driver is visible at that address. Refer back to the tutorial page Configuring Your Pi for I2C for instructions on verifying your I2C address and I2C bus number.

If your Servo Driver is responding to I2C, but the code is still giving you "check your I2C address" errors, then modify the code as follows: In the file Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py,

change
self.i2c = Adafruit_I2C(address)
to
self.i2c = Adafruit_I2C(address, 0)
for 256MB Raspberry Pi's, or...

to
self.i2c = Adafruit_I2C(address, 1)
for 512MB Raspberry Pi's

Testing the Library

Once the code has be downloaded to an appropriate folder, and you have your PWM/Servo breakout and motor properly connected, you can test it out with the following command (the driver includes a simple demo program): 
Copy Code
sudo python Servo_Example.py
To stop the example, simple press CTRL+C.

Depending on if you are using a standard or continuous rotation servo, you should get results similar to the following (a continuous rotation servo is being used in this particular example):
Last updated on 2014-04-16 at 07.58.49 PM Published on 2012-08-17 at 01.47.27 AM