Configuring Your Pi for I2C

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 Rasbian 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 Raspbian, 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:

Download: file
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 an Original Raspberry Pi (Sold before October 2012) - the I2C is port 0:

Download: file
sudo i2cdetect -y 0
If you have a second rev Raspberry Pi, the I2C is on port 1:
Download: file
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.
This guide was first published on Aug 16, 2012. It was last updated on Aug 16, 2012. This page (Configuring Your Pi for I2C) was last updated on Jan 17, 2019.