Raspberry PI – How to use I2C

I tried to check my Raspberry PI board with I2C connection. I have I2C DS1307 (Real Time Clock module) on hand. And after check the voltage on SDA/SDL pin out is 3.3v, safe for use with Raspberry PI board.

IMG_0420

So, lets start with I2C. Install i2c-tools

pi@raspberrypi ~ $ sudo apt-get install i2c-tools

remove i2c-bcm2708 from modprobe blacklist

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

Then add i2c-bcm2708, i2c-dev into modules list.

pi@raspberrypi ~ $ sudo nano -w /etc/modules

And add following line

i2c-bcm2708
i2c-dev

Give all user access to i2c devices.

pi@raspberrypi ~ $ sudo touch /etc/udev/rules.d/99-i2c.rules

and add following line

SUBSYSTEM=="i2c-dev", MODE="0666"

Test all i2c module and check device on Raspberry PI

pi@raspberrypi ~ $ sudo modprobe i2c-bcm2708
pi@raspberrypi ~ $ sudo modprobe i2c-dev
pi@raspberrypi ~ $ sudo udevadm trigger
pi@raspberrypi ~ $  ls -l /dev/i2c*
pi@raspberrypi /etc/udev/rules.d $ ls -l /dev/i2c*
 crw-rw-rwT 1 root i2c 89, 0 Jan 15 11:07 /dev/i2c-0
 crw-rw-rwT 1 root i2c 89, 1 Jan 15 11:07 /dev/i2c-1

Ok now, your Raspberry PI is ready for use with I2C.

Connect your DS1307 I2C module with Raspberry PI. And check that you’re success for talk with i2c.

For revision 1

pi@raspberrypi ~ $ sudo i2cdetect -y 0

For revision 2

pi@raspberrypi ~ $ sudo i2cdetect -y 1

Screen Shot 2013-01-15 at 11.39.42 AM

If you get 68 as picture above. You’re good to go. If not check your connection and try again.

pi@raspberrypi ~ $ sudo modprobe rtc-ds1307

For revision 1

pi@raspberrypi ~ $ sudo bash
# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
# exit

For revision 2

pi@raspberrypi ~ $ sudo bash
# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device 
# exit

Now time to check time!

pi@raspberrypi ~ $ sudo hwclock -r
Tue Jan 15 18:10:17 2013 -0.480690 seconds
pi@raspberrypi ~ $ date
Tue Jan 15 11:12:17 ICT 2013

hwclock -r will show clock from your RTC module. And ‘date’ will show current date from your Raspberry PI. Please note that you need to sync Raspberry PI date/time with internet first.
If the time from hwclock -r not correct compare with ‘date’ command. Use hwclock -w to write Raspberry PI time back to RTC module

pi@raspberrypi ~ $ sudo hwclock -w

Now your RTC module ready to use. My goal is done. I only wish to test I2C function. But if you want to always use time from RTC modules. Please use following stem.

pi@raspberrypi ~ $ sudo nano -w /etc/modules

and add rtc-1307 to end of the file

rtc-1307

and edit rc.local file to add RTC I2C address on startup

pi@raspberrypi ~ $ nano -w /etc/rc.local

And add following line to end of the file (Please change i2c-1 to i2c-0 if you use revision 1).

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

One thought on “Raspberry PI – How to use I2C

  1. Hello,

    at your Post above.
    in /etc/modules.
    rtc-ds1307 instead of rtc-1307 of yours. Right?

    and,
    After reboot PI,

    pi@raspberrypi ~ $ sudo hwclock -r
    hwclock: Cannot access the Hardware Clock via any known method.
    hwclock: Use the –debug option to see the details of our search for an access method.
    pi@raspberrypi ~ $ sudo hwclock -w
    hwclock: Cannot access the Hardware Clock via any known method.
    hwclock: Use the –debug option to see the details of our search for an access method.
    pi@raspberrypi ~ $ sudo hwclock -s
    hwclock: Cannot access the Hardware Clock via any known method.
    hwclock: Use the –debug option to see the details of our search for an access method.

    I can’t read/write/set hwclock as above.

    So I can read/write/set it after run the following again,

    pi@raspberrypi ~ $ sudo bash
    # echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    # exit

    Now i can read/write/set it as the following.

    pi@raspberrypi ~ $ sudo hwclock -s
    pi@raspberrypi ~ $ sudo hwclock -r
    Fri 25 Jul 2014 18:49:04 KST -0.097332 seconds
    pi@raspberrypi ~ $ sudo hwclock -w

    Conclusion:
    Pi can not be set the HWCLOCK from DS1307 after reboot.
    Any idea?

    Thanks,
    SB YIM

Leave a Reply