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?
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.
Lots of discussions going on about this. Here's what my reading and messing around with shows me:
/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
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