Raspberry Pi I2C (Python)

 by AntMan232
Featured
IMG_20121023_125107.jpg
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. 
 
Remove these adsRemove these ads by Signing Up

Step 1: Install R-Pi Image

IMG_20121023_125107.jpg
Go to the Raspberry Pi website, and download the latest Raspbian image and follow the instructions burn it to the SD card.

http://www.raspberrypi.org/downloads

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).
kkjensen says: May 12, 2013. 11:46 PM
What purpose does the write(0x51) provide? Is it just activating the module on the i2c bus so values can be read from it?
trollies says: Mar 31, 2013. 8:02 PM
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?
utest_megha500 says: Feb 17, 2013. 3:55 PM

john1221 says: Nov 23, 2012. 5:03 PM
I could only run i2cdetect with the command "sudo i2cdetect -y 0"
Otherwise it would not run..
dgateley says: Oct 26, 2012. 9:02 PM
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.
vynylpooh says: Oct 26, 2012. 1:40 AM
Thank You!
dcastor says: Oct 25, 2012. 11:37 AM
To make this 'bile better, you might explain in step 1 what I2C is for.
AntMan232 (author) in reply to dcastorOct 26, 2012. 12:24 AM
Ah, whoops, thanks!
chrimel says: Oct 25, 2012. 10:05 PM
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.
treefingers 10 says: Oct 25, 2012. 5:24 PM
As a supplement, I have a similar write-up that I buried perhaps a little too deep on the eLinux wiki:

http://elinux.org/RPi_Tutorial_EGHS:Communicating_With_Other_Micro-controllers

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-dog says: Oct 25, 2012. 12:42 PM
Thanks for a very straight forward step by step explanation!
sparten11 says: Oct 25, 2012. 9:45 AM
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) in reply to sparten11Oct 25, 2012. 10:50 AM
If you give me a GPS module, I will gladly oblige. :D If only I had the money...
syseng says: Oct 25, 2012. 7:26 AM
Thanks for sharing this,it means i could use my temperature sensors
AntMan232 (author) says: Oct 24, 2012. 11:07 AM
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...
antares72 says: Oct 24, 2012. 10:56 AM
Nice and clean examples! You have a new follower.
randofo says: Oct 23, 2012. 10:14 AM
Thanks for sharing this. This will surely come in handy.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!