Raspberry PI – GPIO and temperature sensor

Some new hacking on Raspberry PI, the objective is to get a TC74A5 3.3VAT temperature sensor working with a RPI. This sensor is a digital sensor type 2 wires I2C operating at 3.3V with a precision of 2°C.

Read next to get details

Hardware cabling

First of all, here is the Raspberry PI Pinout :

Raspberry PI gpio pinout

Raspberry PI gpio pinout

I’ll try to use I2C to control the sensor. So i’ll use the Pin indicated as GPIO0(SDA), GPIO1(SCL), 3.3V Power and Ground. They are all on the left corner, it’s easy to create a cable.

The TC74A5 is directly connected to the corresponding PIN.








  Software aspects

The first step is to activate I2C module as it is blacklisted by default. Edit file /etc/modprobe.d/raspi-blacklist.conf and comment the line blacklisting i2C :

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

blacklist spi-bcm2708
#blacklist i2c-bcm2708

Then install the i2c tools

#apt-get install i2c-tools

Next load the i2c module on raspberry

#modprobe i2c-dev

Verify if i2c device is detected by running i2cdetect, according to the documentation the 7b address is 100 1101 = 0x4d

root@raspberrypi:~# i2cdetect 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0.
I will probe address range 0x03-0x77.
Continue? [Y/n] Y                   
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- 4d -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

Here is the address of the device, to read the temperature just try :

#i2cget -y 0 0x4d 0x00 b
-y to force Yes to the question ask by default
0 is the i2c bus number
0x4d is the address of the sensor
0x00 is the address for the temperature
b is to read a byte

Result is returned as an hex number and is directly degrees.

Back to reality

That was for the hack as you can found it in many places, now, let’s go back on reality : once you have close your sensor in a box, what you measure is totally changed by the heat of the processor. The good news is that if you do not use your CPU for many task, its temperature is quite stable and you can correlate the sensor temperature with external temperature. (If the precision you need is not thin).

So to give you an idea of what is this correlation, lets see what happen when you put the PI in the fridge and let it goes out :


In yellow the CPU temperature, in blue an internal sensor and in red an external sensor we will assume as the real temperature.

We can see that is stable case, the difference of each is about 10°C



The same remark can be applied here.

We could also assume that external temperature can be evaluated from the CPU temperature.

The problem of this system is mainly the time to get on stable situation and get approximate temperature. The next test will be to add a sensor in between box and outside. Unfortunately this last result is not as expected and it is really close to internal sensor results.

After different tests, it seems that we can get a good correlation between external temperature and CPU temperature with the simple formula : External Temp = CPU Temp -26°C. This work with a delay for adaptation, good enough if want to measure temperature directions. It also requires to have a stable processor activity to avoid CPU heating due to computations. I assume this could vary function of the box used, the place where the device is installed …

Another correlation I obtained between external temp, internal temp and CPU temp is External Temperature = Internal Temperature – ( CPU Temperature / 4 ). This is not perfect but not totally wrong estimate.



Some references used


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.