32

How can I talk to digital sensors over the I²C interface?

Hardware:
Which pins on the Raspberry Pi's GPIO can I use?

Software:
What I²C libraries are available?

Alex L
  • 7,605
  • 12
  • 42
  • 53

4 Answers4

23

There's a lot of information about RPi's GPIO here: http://elinux.org/Rpi_Low-level_peripherals

According to it, you can program any GPIO pins for I²C, but:

Pin 3 (SDA0) and Pin 5 (SCL0) are preset to be used as an I²C interface. So there are 1.8 kilohm pulls up resistors on the board for these pins.

That wiki page also has some low-level GPIO code examples for various languages that should get you started. If you need a refresher on what I²C actually is, here's one which also takes the RPi into account.

For specific I²C controlling, this python library might be helpful, it's discussed in this blog post, which includes a code sample.

Tapio
  • 693
  • 1
  • 6
  • 9
7

Lots of discussions going on about this. Here's what my reading and messing around with shows me:

  1. I2C support is not built into the default kernel with the Debian "squeeze"
  2. A custom kernel will have to be compiled with the i2cspi module (Here is a GIT tree excerpt)
  3. The drivers that have been written so far are only for the 3.2 kernel and later, and the author has no intention of supporting the 3.1.9+ due to gaping security holes (Chris Boot discusses the driver)
  4. With luck, the Debian "wheezy" distribution, currently in beta for the RasPi, will have this driver on it. It comes with the 3.2 kernel.
  • 2
    Yes, as you mention it is built into the standard kernel now - both official Raspbian and Debian wheezy distros. I followed some advice here which is easy to follow and I have the I2C devices showing up in /dev ... http://www.raspberrypi.org/phpBB3/viewtopic.php?f=37&t=15511 There's a bit more detail here if required: http://www.robot-electronics.co.uk/files/rpi_i2c_setup.doc – dodgy_coder Sep 17 '12 at 06:14
  • To get more detailed, keep data here rather than on weblinks, and hoping this doesn't get out of date: As of the Wheezy 2012-09-18 image, you edit /etc/modprobe.d/raspi-blacklist.conf and comment out the entry for i2c-bcm2708; then edit /etc/modules and append i2c-dev; then reboot. You should now have a /dev/i2c-0 special file, which is very easy to use. – damian Nov 13 '12 at 08:57
2

Here's some examples I've done

John La Rooy
  • 11,947
  • 9
  • 47
  • 75
2

Hardware:

As mentioned by Tapio, the hardware is well documented at http://elinux.org/Rpi_Low-level_peripherals

Software:

There is a i2c-dev header in the Linux userspace (#include <linux/i2c-dev.h>). Furthermore you need a character decive to read from. This can be done by loading the correct modules. i2c_bcm2708 for the low level driver and i2c-dev for generating the character decives for the busses. Apply ...

sudo modprobe -r i2c_bcm2708
sudo modprobe i2c_bcm2708 baudrate=<your preferred baudrate>

for loading them on the fly. Apply ...

sudo sh -c 'echo "i2c-dev" >> /etc/modules'
sudo sh -c 'echo "options i2c_bcm2708 baudrate=<your preferred baudrate>\n" > /etc/modprobe.d/custom.conf

and unblacklist i2c_bcm2708 in /etc/modprobe.d/raspi-blacklist.conf to make /dev/i2c-0 and /dev/i2c-1 show up permanently.

From now on you can follow the hints on how to use I²C del maestro himself.

I prefer this method over others, because it is platformagnostic. Your can use linux/i2c-dev.h with other devices too, as long as there exists a low level I²C driver.

Regards

ManuelSchneid3r
  • 200
  • 1
  • 7