Picture of Raspberry Pi I2C (Python)
In this instructable, I will explain how to use I2C on the Pi, with the examples of the CMPS03 compass module and SRF08 Ultrasonic range, using python. I will explain right through installing the OS, to ensure that the dependencies and everything is installed.

I2C is a communication bus designed by Philips, for chips to communicate with each other on a PCB. It is commonly used, however, for connecting sensors, such as the two examples later in this instructable and port expanders, because you can have multiple devices on the same two pins. 

Step 1: Install R-Pi Image

Picture of Install R-Pi Image
Go to the Raspberry Pi website, and download the latest Raspbian image and follow the instructions burn it to the SD card.


There is an easy setup guide on the wiki, just follow it through.

When you have got it installed, run the config tool, and get everything going nicely. In my case, I am running it headless via SSH, which is enabled as default, at pi@192.168.0.X (check on your router to find the IP).

How did you get your nano that cool green color?

edrandall8 months ago

Where do I find the SDA and SCL lines on my Pi

you can find them on pin 3 and 5, 3 is the SDA and the SCL is the 5th pin

For the 512mb of ram USE

i2cdetect -y 1

instead of the other.

Thank you! I've been trouble shooting for almost an hour and was about to give up when i saw your post. I thought I had killed my BMP180 while soldering it or that it was broke for some other reason. =)


Same goes for the Pi 2, which is also a B+.

A good ible - another great example would be connecting MCP23008 or MCP23017 chips and controlling them either via python-smbus (by sending bytes to registers) or via wiringpi2 (high level driver providing setup routines and an abstraction layer).

bacoms7 months ago

Please forgive a complete noob commenting here but, as I read more and more
on I2C, the noobier I'm getting.

What's got me confused is how to connect, for example, several SRF08 Range Sensors.
Presumably they all have the same address (0x70) so confusion reigns, right? I
suspect you can only connect devices that have different addresses which, if
I'm right, severely limits the usefulness of I2C.

Am I right and is there a cost-effective way around this limitation?

P.S. I'm relatively new to all this hardware stuff. I'm retired now but was a mainframe programmer who has since discovered Python and loves it.

paulhill13 bacoms7 months ago

You can configure the address that the SRF08 uses, there a 16 possibles,
meaning you could use up to 16 SRF08's on one bus. Scroll down on this
page to see how http://www.robot-electronics.co.uk/htm/srf08tech.shtml

laffoleyb8 months ago

There is a very simplai I2C level shifter that will save your port.

see for example http://playground.arduino.cc/Main/I2CBi-directionalLevelShifter, you will probably have the pull up resistors on the boards anyway so you probaly just need the two FETs.

pdfsman10 months ago

If I use another sensor for example IMU breakout-LSM9DSO, What is the change in the python file?

Gorgoth11 months ago

Great bit of info got my almost totally unrelated project working as you had all the important stuff

rkhedkar1 year ago

very easy explanation... I easily get confused with Rpi tutorials on web but this one is an exception....

thank you sir... @


sakko3031 year ago

as a lurker who is considering a pi for all sorts of nefarious purposes, I find this very helpful!

kkjensen2 years ago
What purpose does the write(0x51) provide? Is it just activating the module on the i2c bus so values can be read from it?

0x51 is the write register to command data returned in centimeter vales. 0x50 commands values in inches and 0x52 commands values in microseconds (flight time.

See http://spaces.atmel.com/gf/download/frsrelease/228/1344/SRF08%20Ultra%20sonic%20range%20finder.pdf

TNX for the great instruction but 1 question

i didn't understand if the 5V i2c device has damaged your device or not ,

trollies2 years ago
When you do:

bear2 = bus.read_byte_data(address, 3)
bear = (bear1 << 8) + bear2
bear = bear/10.0
return bear....
why do you divide by 10? I am assuming you have a signed 15 bit number and you are getting rid of the last 0? Why not use >> 1?

john12212 years ago
I could only run i2cdetect with the command "sudo i2cdetect -y 0"
Otherwise it would not run..
dgateley2 years ago
Hey, AntMan. Should you be so inclined, a similar article on how to get i2s (much different than i2c functionally) working on Raspberry Pi would be enormously appreciated. I'd like to attach a Wolfson audio codec but getting up to the configuration point where I could basically do that and begin talking to it is daunting to say the least.
vynylpooh2 years ago
Thank You!
dcastor2 years ago
To make this 'bile better, you might explain in step 1 what I2C is for.
AntMan232 (author)  dcastor2 years ago
Ah, whoops, thanks!
chrimel2 years ago
Would the external GPS cable for a generic Android tablet or an old XM radio possibly work? I have a couple of those that I might be able to part with if they could be made to work.
As a supplement, I have a similar write-up that I buried perhaps a little too deep on the eLinux wiki:


Includes: the official (probably overkill) way of interfacing 3.3V and 5V I2C applications, software for a few languages, and kernel considerations.

Range sensor sounds fun, I'll give that a try!
1-big-dog2 years ago
Thanks for a very straight forward step by step explanation!
sparten112 years ago
all we need now is gps and radar. and viola we are ai nav.. kewl. let me know when you get GPS up and running..

AntMan232 (author)  sparten112 years ago
If you give me a GPS module, I will gladly oblige. :D If only I had the money...
syseng2 years ago
Thanks for sharing this,it means i could use my temperature sensors
AntMan232 (author) 2 years ago
Thanks guys, it's nice to be able to help! It geniunely took me about a week to realise that the Adafruit code was overkill, and that it only took one line to read over i2c...
antares722 years ago
Nice and clean examples! You have a new follower.
randofo2 years ago
Thanks for sharing this. This will surely come in handy.