Thursday, 20 September 2012

Raspberry Pi I2C Tutorial

In this tutorial I will detail how to interface to an I2C device using your Raspberry Pi. Specifically we will be reading the temperature from an I2C based TMP102 breakout board. You won't need any programming skills to do this, just the ability to connect up the simple circuit to your Pi and be able to use the command line. It will help to have some understanding on how I2C buses work.

Disclaimer: I take absolutely no responsibility if you blow up your Raspberry Pi or anything else while following this tutorial. 

You will need the following:

  • A Raspberry Pi with internet access, keyboard, power supply, etc.
  • An SD card with Raspbian “wheezy”.
  • A mini breadboard (example here).
  • Some Male to Female jump wires (example here).
  • An I2C based TMP102 digital sensor breakout board (example here). You will need to solder on some break away headers onto the breakout board so you can mount it on your breadboard.
(Please note, I have no affiliation to Proto-Pic other than being a regular customer.)

TMP102 Breakout Board

The TMP102 breakout board I am using is this one from SparkFun. This device can measure a temperature range of -40°C to +125°C, with a resolution of 0.0625°C and an accuracy of 0.5°C.
The TMP102 has 4 registers that are accessible through the I2C bus:
  • Register 0 Temperature Register (Read Only)
  • Register 1 Configuration Register (Read/Write)
  • Register 2 TLOW Register (Read/Write)
  • Register 3 THIGH Register (Read/Write)

We are only interested in Register 0, for further info on the other registers, the datasheet for the TMP102 device can be found here. Two bytes can be read from register 0:
  • Byte 1: Full degrees
  • Byte 2: Fraction degrees

So, we are going to connect our I2C device to the appropriate pins on the Pi's GPIO header:

Raspberry Pi GPIO Header TMP102 Breakout board Description
Pin1/3V3 V+ This will provide power to the TMP102 device.
Pin6/Ground GND and ADO Device power rail ground.
Pin3/SDA SDA Data line for the Pi's I2C interface 0.
Pin5/SCL SCL Clock line for the Pi's I2C interface 0

Note that AD0 in the TMP102 breakout board is used to define the least significant bit of the I2C address of the TMP102 device. If it is connected to ground, the address of the device is 0x48, if connected to 3v3 the address is 0x49. This allows up to two TMP102 devices to exist on the same I2C bus. In my case I have connected it to ground.

Enabling I2C and Installing I2C Tools
Assuming you have not enabled I2C on your Raspberry Pi before:

Enable I2C in modprobe:
Have a look at the /etc/modprobe.d/raspi-blacklist.conf file, comment out the two lines:
blacklist spi-bcm2708
blacklist i2c-bcm2708

Load the i2c kernel drivers:
sudo modprobe i2c-dev

Now check that the i2c devices have appeared in /dev:
ls -l /dev/i2c*
crw-rw---T 1 root i2c 89, 0 Sep 20 19:43 /dev/i2c-0
crw-rw---T 1 root i2c 89, 1 Sep 20 19:43 /dev/i2c-1

Install the i2c tool chain:
sudo apt-get install i2c-tools

Add your user to the i2c usergroup
This is so you don't need to use sudo to interact with the i2c device.
sudo usermod -aG i2c yourusername

Talking to the I2C device
We can now use the i2cdetect command to determine what devices are on the I2C bus
pi@raspberrypi ~ $ i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

The '-y' option disables interactive mode for the command and the '0' is the I2C bus to scan. You can also run this command on the Pi's second I2C bus by specifying '1' instead.
We can see that it has found our TMP102 device at address 0x48.

To read the temperature from our temperature sensor, we use the i2cget command to read a single byte (Byte 1 - full degrees) from the temperature register (0x00) of the device.
pi@raspberrypi ~ $ i2cget -y 0 0x48 0x00 b

Converting this hexadecimal value to decimal, we get our temperature of 22°C.

If you want a more precision on the temperature, you can read both the full and fractional bytes from register 0 as follows:
pi@raspberrypi ~ $ i2cget -y 0 0x48 0x00 w
This gives us byte 2 (0xa0) and byte 1 (0x15), but as a 16bit hexadecimal number and in the wrong order. To convert to °C, swap around the bytes, shift right by 4, convert to decimal and multiply by 0.0625.
dec(0x15a0>>4) * 0.0625 = 21.625°C

So, we have been able to set up the circuit to interface with the TMP102 temperature sensor, enabled I2C on the Raspberry Pi, install the necessary I2C tools and queried the temperature from the sensor.
As a further exercise, the reader can experiment with reading and writing (using the i2cput command) to the other registers available in the TMP102.


  1. Thanks, a ready to use downloadable code will be highly appreciated saving us a lot of tinkering :)

  2. But you need to tinker and learn with the Raspberry Pi :-)

    1. Hi Donal!
      I just blogged about doing this in C code here:

      We're working on a prototyping tool and it would be awesome to have your thoughts on it, if you could have the time!
      You're not easy to discover over the Internet, so please get in touch with me :)

    2. Hi Bobi,
      Looks like a very interesting project, I will send an email to the Agilart contact address.

  3. OK, will do that. thanks for the post :)

  4. Hi

    I have connected the TMP 102 to my Pi and the i2cdetect -y 0 shows the address to be 0x48. when I run i2cget -y 0 0x48 0x00 w I get a read error

    I tried from Python and I get IOError: [Errno 5] Input/output error

    I have another device on the bus that works Ok - I disconnected that unit but no change

    Please can you help

  5. Hi
    Tried another TMP102 but no change
    Also from reading the data sheet you can have 4 TMP102s
    on one bus
    A0 gnd = 0x48
    A0 V+ = 0x49
    A0 SDL = 0x4a
    A0 SCL = 0X4b

  6. This is a very good and simple tutorial. Helped me to know how to do it

  7. I'm interested why you don't use pull up resistors? Are they necessary? If so will a 4.7K ohm work? I have also heard that the raspberry pi has pull up resistors built in??? Is that true?

    1. Hi Justin,
      The TMP102 breakout board in the tutorial doesn't have any pullups, but the RaspberryPi has 1.8k pullups. So you don't need external pullups. In fact, having external pullups can lead to high current draw.
      Hope that helps,