Enabling The I2C Interface On The Raspberry Pi

26

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.

Step 1 – Enable i2c using raspi-config utility

From the command line type :

sudo raspi-config

This will launch the raspi-config utility.

Enable SPI Using Raspi-config

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

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 :

lsmod | grep i2c_

That will list all the modules starting with “i2c_”. If it lists “i2c_bcm2708” then the module is running correctly.

Testing Hardware (Optional)

Once you’ve connected your hardware double check the wiring. Make sure 3.3V is going to the correct pins and you’ve got not short circuits. Power up the Pi and wait for it to boot.

If you’ve got a Model A, B Rev 2 or B+ Pi then type the following command :

sudo i2cdetect -y 1

If you’ve got an original Model B Rev 1 Pi then type the following command :

sudo i2cdetect -y 0

Why the difference? Between the Rev 1 and Rev 2 versions of the Pi they changed the signals that went to Pin 3 and Pin 5 on the GPIO header. This changed the device number that needs to be used with I2C from 0 to 1.

I used a Pi 2 Model B with a sensor connected and my output looked like this :

pi@raspberrypi ~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

This shows that I’ve got one device connected and its address is 0x20 (32 in decimal).

Share.

26 Comments

  1. I try all this and I got this error. I have Rpi B+

    Error: Could not open file `/dev/i2c-1′ or `/dev/i2c/1′: No such file or directory

    • Same here with Raspberry Pi 2 with latest Raspbian (kernel 3.18.6-v7+).
      Using raspi-config results in absolutely no modifications to the config files.
      Modifying the config files manually also gives no good news.

      SOLUTION: Searching the web, it appears that with new kernels (3.18) it’s necessary to modify another file. Edit /boot/config.txt and add the line:
      dtparam=i2c_arm=on

      It seems there’s another line to add, but I got i2c working without this one:
      dtparam=i2c1=on

      Good i2cing 😉

      • After a bit of experimenting I’ve got my i2c hardware working. I’ve updated my articles so hopefully they now reflect the changes made in Raspbian.

  2. There is a mistake in your /etc/modules script. You need to change the fallowing for it to work on the new Rpi2 board.

    “i2c-bcm2708” -> “i2c-bcm2835”.

    2708 is for the original Rpi1 board, but for it to work on the new Rpi2 board. You need to change it to 2835.

    • i2c-bcm2708 works for both boards. i2c-bcm2835 works for the Pi 2 but stops a B+ booting. So at the moment I can’t see why you wouldn’t use i2c-bcm2708 and have your SD card work in a B+ and Pi 2.

  3. Have followed the procedure as shown above, however, when I type “sudo i2cdetect -y 1 I get an error message “could not open file ‘/dev/i2c-1’ no such file or directory. I am totally new to the Raspberry Pi and it’s operating system so don’t know if the directory has been updated or how to correct it. Any help would be appreciated.

  4. Thanks – best article I’ve seen on this subject. It’s great to see that you keep it up to date. I have booked marked your site for future reference once I have got my IMU working.

    BTW: What does the i2c-bcm2835 module do that the i2c-bcm2708 doesn’t? (I have a PI 2 B board, I gather from your comments that it really doesn’t matter but it would be nice to know).

  5. Here’s a tip: If you get I2C errors make sure that keep the leeds between the IMU and the PI short. I had made a test cable for various Arduino modules, it was quite long so I could move the model around to test it. I was able to successfully run the IMU on the Arduino and it worked fine but when I connected it to the PI, I would get “I2C 113, Unable to read FIFO Counter”, errors and none of the supplied software would work.

    I found that my Freetronics 9-DOF IMU (http://www.freetronics.com.au/collections/modules/products/9-dof-imu-accelerometer-gyroscope-magnetometer) has two 4.5K pull-up resistors and these should be disabled with the Pi because it has it’s own pulp resistors, 1.8K, so you really don’t want to add any extra ones into the circuit because they become connected in parallel and reduce the total pull values considerably (1/(1/1.8K+1/4.5K) = 1.285K, yikes!).

  6. I have the new improved Pi2 1gb. I am using Raspbian and I have tried all of the steps to enable i2c. The statement- lsmod | grep i2c_ does return “i2c_bcm2708″ and one other module.
    But, sudo i2cdetect -y 1 does not work just the same. “sudo: i2cdetect: command not found” is the result. I am wondering if the problem is the RTC that I have installed. It came without any instructions of it’s own and other people are having trouble using a RTC on the Pi2.

    • If i2cdetect is not recognised then it sounds like i2c-tools is not installed. Are you using a “Tiny RTC” module?

      • Yes. Mini RTC or Tiny GPIO device. The real time clock works great now but I sure wish they had provided instructions with it.
        It was cheap though. Many thanks for your help.

  7. Well, I just got i2cdetect to work on my Pi2 running Raspbian. My troubles were all due to the fact that I was trying to use a Wi-Fi adapter when installing python-smbus and i2c-tools. I connected a network cable and tried again. This time all is well and I can go on to set up the date and time on my new RTC (real time clock)
    Thanks for the info. Your instructions are the best I have found on the Internet.

  8. Heya, I really can’t seem to get this right. the weird thing is, when connecting one device and scanning for i2c devices I get EVERY possible address filled instead of just 0x20. any ideas on what i might be doing wrong?

  9. After fallowing all steps I’m getting the error: Error: Could not open file `/dev/i2c-1′ or `/dev/i2c/1′: No such file or directory

    I tried every suggestion on this discussion without no luck. Can it be something with the wiring between the pi and the nano? Is there any picture or scheme on this?

    Thanks

    • Are you using the latest version of Raspbian? Does your /boot/config.txt contain the line :
      dtparam=i2c_arm=on
      If it does not then add it at the end by editing the file using :
      sudo nano /boot/config.txt

  10. Hi
    I had followed all the step above. It works fine until the lsmod |grep i2c_. However, when I key in the i2cdetect -y 1, I can’t detect the address of my device. I am using Raspi B+ with ina219. Can anyone help me with it???Thanks!!!

  11. Billy Bryant on

    I have followed every step here, I’m using a RaspberryPI 2 B+ 1GB and a DS3231 RTC Module. I am running the latest build version of Raspbian and have installed the i2c tools. When booting, it looks like it sees the module as the verbose booth shows it identifying the i2c controller and a device. However, when I run i2cdetect -y 1, it tells me that it cannot find `/dev/i2c-1′ or `/dev/i2c/1′. I am running on a Read-Only partition with UNIONFS for /etc and /var. I’m at my wits end, how do I get this working?

  12. Was trying to install a RTC and was going round in circles trying to connect to the IC2 bus. This guide helped me sort out everything, thanks very much. (there is a lot of very poor stuff out there which is either wrong o is asumming prior knowledge)

  13. I used I2C on an Arduino, and after considerable thrashing I got it to behave. The Pi looks a lot more difficult. Your excellent article is going to save me a great deal of grief trying to get the Newhaven NHD-0216K3Z-FL-GBW LCD to do what I want it to do.

  14. Thank you for this article
    my question is

    How many devices can i connect by i2c ?

    if i want to connect many arduino ? how many arduino can be connected to raspberry pi by i2c

    thank you so much

Leave A Reply