Raspberry Pi I2c Interfacing and Programming

I2C is a multi-device bus used to connect low-speed peripherals to computers and embedded systems. The Raspberry Pi supports this interface on its GPIO header and it is a great way to connect sensors and devices. Once configured you can connect more than one device without using up additional pins on the header.

Before using I2C it needs to be configured. This technique has changed slightly with the latest version of Raspbian so I’ve updated this article.Enabling The I2C Interface On The Raspberry Pi.jpg

Step 1 – Enable i2c using raspi-config utility

From the command line type :

sudo raspi-config

This will launch the raspi-config utility.

Now complete the following steps :

  • Select “8 Advanced Options”
  • Select “A7 I2C”
  • Select “Yes”

The screen will ask if you want the interface to be enabled :

  • Select “Yes”
  • Select “Ok”

The screen will ask if you want the module to be loaded by default :

  • Select “Yes”

The screen will state the module will be loaded by default :

  • Select “Ok”
  • Select “Finish” to return to the command line

When you next reboot the I2C module will be loaded.

Step 2 – Manually Edit Module File

Next we need to edit the modules file using :

sudo nano /etc/modules

and add the following two lines :

i2c-bcm2708
i2c-dev

Use CTRL-X, then Y, then RETURN to save the file and exit.

Step 3 – Install Utilities

To help debugging and allow the i2c interface to be used within Python we can install “python-smbus” and “i2c-tools” :

sudo apt-get update
sudo apt-get install -y python-smbus i2c-tools

Enabling The I2C Interface On The Raspberry Pi schematic.jpg

Step 4 – Shutdown

Shutdown your Pi using :

sudo halt

Wait ten seconds, disconnect the power to your Pi and you are now ready to connect your I2C hardware.

Checking If I2C Is Enabled (Optional)

When you power up or reboot your Pi you can check the i2c module is running by using the following command:

 

For more detail: Raspberry Pi I2c Interfacing and Programming

Scroll to Top
Read previous post:
raspberry pi networking cookbook.jpg
Raspberry Pi Networking Cookbook -E-book

E-Book description: Back in 2006, Eben Upton and his colleagues at the University of Cambridge's Computer Laboratory noticed a disturbing...

Close