IZ8MBW QSL

IZ8MBW Fabio

Amateur Radio Station in Napoli - Italy
ITU Zone: 28 - CQ Zone: 15 - Locator: JN70ct

Configure Raspberry Pi to read I²C (also known as I2C) sensors



This guide describe how to do all necessary for read temperature from I²C sensor chips. Gives all commands as root user.
Based on my experience with I²C I have tested Texas Instruments TMP275, Maxim DS7505, Texas Instruments LM92, Analog Devices ADT7410 and Bosch BMP085.

1. As usual, first, upgrade your packages
apt-get update
apt-get upgrade

2. Then, if you have not already done so, download and install rpi-update (later you will upgrade the Raspberry Pi system firmware)
apt-get install git-core
apt-get install ca-certificates
apt-get install rpi-update

3. Then, download and install I²C packages
apt-get install i2c-tools
apt-get install lm-sensors
apt-get install libi2c-dev

4. Now enable the bcm2708 chip for I²C and the I²C kernel modules
a. Open for editing the file /etc/modprobe.d/raspi-blacklist.conf
vi /etc/modprobe.d/raspi-blacklist.conf
b. Comment (add a '#') to the line: blacklist i2c-bcm2708
So the file will be:
# blacklist spi and i2c by default (many users don't need them)

blacklist spi-bcm2708
#blacklist i2c-bcm2708
c. Open for editing the file /etc/modules
vi /etc/modules
d. Add these lines at the end of the file (for Kernel version before 3.18):
snd-bcm2835
i2c-bcm2708
i2c-dev

d. Add these lines at the end of the file (for Kernel version equal or greater then 3.18):
snd-bcm2835
i2c-dev

So the file will be:
# /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-bcm2708  #remove this line if your Kernel version is equal or greater then 3.18
i2c-dev

5. Now, upgrade your system firmware
rpi-update

6. Enable the Device Tree (DT) Kernel parameter (IMPORTANT: do this step ONLY if your Kernel version is equal or greater then 3.18)
Open for editing the file /boot/config.txt
vi /boot/config.txt

Add this line at the end of the file:
dtparam=i2c_arm=on

7. Now, anyhow, reboot
reboot

Well, now your Raspberry Pi is configured for I²C.
Connect the I²C device (or devices) to the Raspberry Pi I²C bus.
The I²C bus has 2 wires, it's assigned to the GPIO #2 (I²C bus 1, signal SDA) and GPIO #3 (I²C bus 1, signal SCL) on the P1 Header. Unlike the 1-Wire bus, the I²C bus already has on board the pull-up resistors (1.8 kohm) to 3.3 V (GPIO #1), so no others resistors are needed. It's only suggested (but not so mandatory) to put a ceramic capacitor of 0.1 µF between 3V3 (GPIO #1) and GND. As Vcc (V+) use the 3.3 V (GPIO #1) and not the 5 V, mostly sensors are more precise with 3.3 V. To understand well the Raspberry Pi GPIO pins, see the RPi Low-level peripherals.

8. Query the device (or devices)
i2cdetect -y 1
(-y 0 for Rev. A boards)
The output will look like this:
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 49 -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- 77
As you can see in this output, actually I have three I²C devices: 0x48, 0x49, 0x77.

9. Read temperature
i2cget -f -y 1 0x48 0x00 w
(-y 0 for Rev. A boards)
[0x48 is the chip address] [0x00 is the temperature register address] [w stands for read word data, in others word it means read value in full resolution]
You should obatain an output like this:
0x8017
[equal to 23.5 °C]

10. Chips resolution
The Texas Instruments TMP275 but also others temperature chips such as Texas Instruments TMP100, Texas Instruments TMP101, Maxim DS7505 by default are setted to 9 bit of resolution (0.5 °C), to increase the resolution to 12 bit (0.0625 °C) you need to configure the Configuration Register of the chip. So to set the 12 bit resolution on TM275 chip:
i2cset -y 1 0x48 0x01 0x60
(-y 0 for Rev. A boards)
[0x48 is the chip address] [0x01 is the register address] [0x60 is the value to enable 12 bit resolution]

11. Convert the temperature
The command "i2cget -f -y 1 0x48 0x00 w" will give us a hexadecimal value and so it's not "human value", we need to convert it.
Convert with two decimal digit (not suggested):
i2cget -f -y 1 0x48 0x00 w | mawk '{printf("%.2f\n", (a=(("0x"substr($1,5,2)substr($1,3,1))*0.0625))>128?a-256:a)}'
(-y 0 for Rev. A boards)
Convert with one decimal digit (suggested):
i2cget -f -y 1 0x48 0x00 w | mawk '{printf("%.1f\n", (a=(("0x"substr($1,5,2)substr($1,3,1))*0.0625))>128?a-256:a)}'
(-y 0 for Rev. A boards)

12. Create a script that will read temperature from the sensor with errors control
I have made this shell script that will read temperature from the sensor, it contains also errors control.
The errors control will work if you will obtain "Read error", errors during transmission with my HF RTX on same bands. Note also this script is made for 0x48 as chip address.
The scirpt:
#!/bin/bash
temp=$(i2cget -f -y 1 0x48 0x00 w | mawk '{printf("%.1f\n", (a=(("0x"substr($1,5,2)substr($1,3,1))*0.0625))>128?a-256:a)}')
if [ "$temp" = "" ] ; then
        exit;
        else
        echo $temp
fi
exit 0

For more informations you can contact me via email at iz8mbw ]at[ gmail.com.
Enjoy!
Fabio Ancona, IZ8MBW

I don't ask for money for any guides and info provided, but if you want you can offer to me a beer. Thanks!