CSCI 255 — Raspberry Pi I2C

Wiring up the Pi

Follow the pictures carefully. It isn’t that hard.

UNCA CSCI references

Checking your wiring

i2cdetect -y 1

You should see devices at 19 (LSM303 accelerometer), 1e (LSM303 magnetometer) and 40 (PCA9685).

Setting up the libraries

This should not be necessary because the libraries should have been installed in CSCI 178. If they are missing, try the following or look at last term’s CSCI 178 lab. (You can just proceed ahead until you encounter a problem.)

git clone
sudo python Adafruit_Python_LSM303/ install
git clone
sudo python Adafruit_Python_PCA9685/ install

Copying the examples

Adafruit libraries come with nice test programs. However, you meed to give them distinct names. Also, make them executable using chmod.

cp Adafruit_Python_PCA9685/examples/ ~/
cp Adafruit_Python_LSM303/examples/ ~/
chmod a+x
chmod a+x

Running the examples

And accelerating experience

Take a look at It’s pretty simple. I just prints the orientation.

Start, by typing ./, and move your Pi. You see the numbers change. Try to figure out what the X, Y and Z values mean. (Hint: Look on the 9DOF device.)

Now modify to print in g-forces. You can do this by dividing the X, Y, and Z values by 1024.

Turning the lights on and off

You will need to modify to use the right pin number. (Right now it is using pin 0.) Because the program is be run with a servo, while are in use in EGM 180 right now, the results will not be very interesting. Try change the range of teh pulse from 150 and 600 to 1000 and 4000. Better yet try a loop that starts with something like the following:
    for p in range(pulse_min, pulse_max, 50):
You’ll need to define pulse_min and pulse_max.

Getting the sensos to work together

If you are ready for a challenge, merge and into a single program that turns on the LED when the Raspberry Pi is stilted, that is, when the Z-axis acceleration is less that 0.9g.