Introduction: How to Enable I2C on RaspberryPI

Hi to everybody, who is reading my text.

In his guide i will try to explain step by step how to enable i2c on rpi. Make sure your rpi is connacted to internet and also any connection to your computer. Because we will use ssh for it. If you don't have ssh in your computer

you can download putty.

The Raspian distros have i2c installed but they are disabled. we want to enable and configure them.

First we have to login in rpi over ssh. If you don't know how to make ssh connection to rpi, please google it. it is not bi deal :D. For ssh connection you need ip-address from your rpi. You can use nmap to find out which ip-address has your rpi. You can install nmap in your computer.

  • For Windows users (Windows key + R and type cmd, it will open a command shell)
    • nmap -sP 192.168.1.1-254 (type it in command shell)
  • For Ubuntu users (open Terminal Ctrl+Alt+T or dashboard)
    • sudo nmap -sP 192.168.1.1-254

Now we are good to configure our rpi over ssh. Next step is configuration.

Step 1: I2C Configuration

If you find out ip-address and setup your putty, we can start. First we have login in rpi over putty. If you start your ssh connection, it will appear something below.

As a default user name = pi

login as: pi

After then type your password default password = raspberry

pi@192.x.x.x's password: raspberry

Linux otter 4.1.7+ #817 PREEMPT Sat Sep 19 15:25:36 BST 2015 armv6l

The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Thu Nov 26 11:39:29 2015 from bla bla

pi@raspberrypi~ $

Now we login to rpi. :D Well done

Now we need a file to enable i2c pins. Please type this in cmd shell.

  • pi@raspberrypi~ $ sudo nano /etc/modprobe.d/raspi-blacklist.conf

Ii will appear something like this. If you don't see any thing, which means all file is blank, don't worry write this code in your file or copy+paste :D

#blacklist spi and i2c by default (many users don't need them)
#blacklist spi-bcm2708
#blacklist i2c-bcm2708

We remove i2c from blacklist. I removed also spi :D. Then you have to save your changes. If you use nano Editor to save your changes:

  • Press Ctrl+X then Y for save and press Enter.

Next we have to edit the modules file and add a new line i2c-dev at the end of the module file. Now edit our file and type this in cmd shell (terminal)

  • pi@raspberrypi~ $ sudo nano /etc/modules

After then it appears something like this below in your cmd shell.

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

snd-bcm2835
i2c-dev

After add i2c-dev in module file, we use nano editor, press crtl+x and y and enter. Module files is saved. :D Well done.

Step 2: Test Your I2C With Modprobe

This is optional step, if you don't want to do it, skip this step. If you want, nothing to lose. Just test.

In cmd shell type these:

  • pi@raspberrypi~ $ modprobe i2c-bcm2708
  • pi@raspberrypi~ $ modprobe i2c-dev
  • pi@raspberrypi~ $ lsmod

With lsmod command we can test, whether both modules loaded or not. If the modules loaded, you will see something in your cmd shell. It means, it is working. :D

Module Size Used by
cfg80211 501663 0
rfkill 22491 1 cfg80211
i2c_dev 6737 0
snd_bcm2835 22339 0
snd_soc_bcm2708_i2s 7619 0
regmap_mmio 3556 1 snd_soc_bcm2708_i2s
snd_soc_core 168382 1 snd_soc_bcm2708_i2s
snd_compress 8824 1 snd_soc_core
snd_pcm_dmaengine 5778 1 snd_soc_core
snd_pcm 92573 3 snd_bcm2835,snd_soc_core,snd_pcm_dmaengine
snd_seq 62040 0
snd_seq_device 5130 1
snd_seq snd_timer 23456 2
snd_pcm,snd_seq snd 68161 7 snd_bcm2835,snd_soc_core,snd_timer,snd_pcm,snd_seq,snd_seq_device,snd_compress i2c_bcm2708 6252 0
spi_bcm2835 7980 0
bcm2835_gpiomem 3675 0
uio_pdrv_genirq 3690 0
uio 10009 1 uio_pdrv_genirq

After this message we can check i2c visible or not. type this command in shell.

  • pi@raspberrypi~ $ ls -l /dev/i2c-*

Belowing line will show in your cmd shell.

  • crw-rw---T 1 root i2c 89, 1 Nov 25 22:17 /dev/i2c-0

  • crw-rw---T 1 root i2c 89, 1 Nov 25 22:17 /dev/i2c-1

Which means, i2c is there. In my PRI i have just /dev/i2c-1. It depends which RPI Revision you use.

Step 3: Install I2c-tools Package

After all steps we can update our system and install some packages which we will need later. Now we will type belowing commands in cmd shell. There is too much typing :D.

# I2C tools for cmdshell
pi@raspberrypi~ $ sudo apt-get install i2c-tools

# I2C python-Library

  • pi@raspberrypi~ $ sudo apt-get python-smbus

# I2C c-Library

  • pi@raspberrypi~ $ sudo apt-get install libi2c-dev
  • pi@raspberrypi~ $ sudo apt-get update

Installations and update take a few minutes. Be patient :D.

Now we will check our i2c-tools. Are they there where they are belongs or not. Type following command in your cmd shell again and it will show you i2c-tools. If you don't see them, you made some mistake.

pi@raspberrypi~ $ ls -l /usr/sbin/i2c*
-rwxr-xr-x 1 root root 14916 Apr 28 2015 /usr/sbin/i2cdetect
-rwxr-xr-x 1 root root 19332 Apr 28 2015 /usr/sbin/i2cdump
-rwxr-xr-x 1 root root 14448 Apr 28 2015 /usr/sbin/i2cget
-rwxr-xr-x 1 root root 18268 Apr 28 2015 /usr/sbin/i2cset

And now we add an user to i2c. If you do this, nothing will happen, because pi is already a member of i2c.

pi@raspberrypi~ $ sudo adduser pi i2c
The user `pi' is already a member of `i2c'.

Reboot your rpi

pi@raspberrypi~ $ sudo reboot

Step 4: Detect I2c on RPI

We can list also our bus. It appears following:

pi@raspberrypi~ $ i2cdetect -l
i2c-1 i2c 20804000.i2c I2C adapter

We are almost done. Now we can detect i2c on rpi. Again we type belowing commands in cdm shell. It delivers us something below.

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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

With this i2cdetect -y 1 command the I2C Bus 1 will search connected devices. In this case i haven't connected any devices because of this nothing to see. If you connect any devices to your rpi, it depends on address, it will seem like this.

0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- 21 -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

It means, an i2c slave device with 0x21 address is connected.

Other additional i2c-tools, which is installed in rpi: i2cget and i2cset

If you want to read from device, you can use i2cget

pi@raspberrypi~ $ sudo i2cget -y 1
pi@raspberrypi~ $ sudo i2cget -y 1 0x08
0x00

The i2cget -y 1 tells the rpi, that rpi should read an i2c device. 1 is used bus in rpi.
As a return value you will see 0x00. (In this case i connected a i2c device with 0x08 address.)

In the other hand if you want to write into your device, you can use i2cset

pi@raspberrypi~ $ sudo i2cset -y 1
pi@raspberrypi~ $ sudo i2cset -y 1 0x08 0x00

The i2cset -y 1 0x08 0x00 tells rpi, that he wants to write into 0x08 i2c device address and 0x00 is the data byte value for it.

Last tool is i2cdump.

pi@raspberrypi~ $ sudo i2cdump -y 1 0x08

Dumps values from an i2c peripheral. if you type this you can have something like that

0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: XX 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 X...............
10: 00 00 00 00 00 00 00 00 00 XX XX XX XX XX XX XX XXXXXXX
20: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
30: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
...
a0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX

........

Step 5: Conclusion

So we typed too much but we are done. If you think, why i wrote this?

Just i tried to combine stuffs from different sources about RPI - I2C.

Here is some sources from internet

http://skpang.co.uk/blog/archives/575

http://blog.oscarliang.net/raspberry-pi-arduino-co...

Following link is in German

http://www.netzmafia.de/skripten/hardware/RasPi/Ra...

I hope that, it will useful for everybody.

Regards

Chef

Comments

author
DIY Hacks and How Tos (author)2015-11-26

Great respberry pi project.

author

thanks

About This Instructable

909views

11favorites

License:

More by 13chef:How to enable I2C on RaspberryPI
Add instructable to: