Getting started with I2C

Reviewed by Editor: Jack Allison

Step-by-step

  • Connect your I2C device
  • Enable i2c.

sudo raspi-config
Use menu option ‘Advanced Options\I2C’ to allow I2C kernel module to be loaded automatically

  • Add i2c modules to configuration
    Add a line with ‘i2c-dev’ at  /etc/modules

sudo nano /etc/modules

snd-bcm2835
i2c-dev

  • Exclude i2c from the black list.

In the file ‘/etc/modprobe.d/raspi-blacklist.conf’ add # at the line related to i2c-bcm2708.

sudo nano /etc/modprobe.d/raspi-blacklist.conf
#blacklist i2c-bcm2708

  • Install appropriate python libraries.

sudo apt-get install python-smbus i2c-tools

  • Install ADCPi libraries

git clone https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

cd ./ABElectronics_Python_Libraries/ADCPi

sudo cp ./ABE_helpers.py /usr/local/lib/python2.7/dist-packages/

sudo cp ./ABE_ADCPi.py /usr/local/lib/python2.7/dist-packages/

  • Add ‘pi’ user to ‘i2c’ access group

; to prevent “Permissions denied” note when running from development environment w/o sudo
sudo adduser pi i2c

  • Reboot RPi

sudo reboot

  • Detect your device

Check if you have got i2c device enabled

ls /dev/i2c*

If all was done correctly in the previous steps you shall get at least one device name of format i2c-<index>

Now try to communicate with it: i2cdetect -y <index>

You should see something like this:

pi@raspberrypi ~ $ ls /dev/i2c*
/dev/i2c-1
pi@raspberrypi ~ $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — — — — — — — — — — —
10: — — — — — — — — — — — UU — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: — — — — — — — — — — — — — — — —
60: — — — — — — — — 68 69 — — — — — —
70: — — — — — — — —

Related references

Comments are closed.