Configuring Your Pi for I2C

Author Gravatar Image KEVIN TOWNSEND
Before you can get started with I2C on the Pi, you'll need to run through a couple quick steps from the console.  

If you are running Occidentalis and are familiar with Terminal commands, then the description below will be sufficient.

If not, then to learn more about how to setup I2C with either Raspbian or Occidentalis, then take a minor diversion to this Adafruit Tutorial: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c

When you are ready to continue, enter the following commands to add SMBus support (which includes I2C) to Python:
Copy Code
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
i2c-tools isn't strictly required, but it's a useful package since you can use it to scan for any I2C or SMBus devices connected to your board. If you know something is connected, but you don't know it's 7-bit I2C address, this library has a great little tool to help you find it. python-smbus is required, it adds the I2C support for python!

If you have Raspbian, not Occidentalis check /etc/modprobe.d/raspi-blacklist.conf and comment "blacklist i2c-bcm2708" by running sudo nano /etc/modprobe.d/raspi-blacklist.conf and adding a # (if its not there).
If you're running Wheezy or something-other-than-Occidentalis, you will need to
add the following lines to /etc/modules
i2c-dev
i2c-bcm2708
and then reboot.

If you have an Original Raspberry Pi (Sold before October 2012) - the I2C is port 0:

Copy Code
sudo i2cdetect -y 0
If you have a second rev Raspberry Pi, the I2C is on port 1:
Copy Code
sudo i2cdetect -y 1
This will search /dev/i2c-0 or /dev/i2c-1 for all address, and if an Adafruit PWM breakout is properly connected and it's set to it's default address -- meaning none of the 6 address solder jumpers at the top of the board have been soldered shut -- it should show up at 0x40 (binary 1000000) as follows:
Once both of these packages have been installed, you have everything you need to get started accessing I2C and SMBus devices in Python.
Last updated on 2014-04-22 at 04.37.40 PM Published on 2012-08-17 at 01.47.27 AM