User Tools

Site Tools


tempsensr

The easiest way to measure temperature with a Raspberry Pi is to use a digital DS18S20 sensor. This sensor is working with three pins, two are for power supply (3,3V and GND) and the third is the digital data transmission pin. These type of sensors are from the family of the 1wire bus devices, which is a quite flexible and easy to implement bus.

Datasheet informations

Installation

Activate Sensor

Connect to raspberry and load the kernel modules

sudo modprobe w1-gpio
sudo modprobe w1-therm

You can also add these modules to /etc/modules to load them during the booting time.

Since kernel 3.18 the Device Tree (DT) is used and should be the prefered way to use it. Ensure it's enabled (using raspi-config) and then edit “/boot/config.txt” and add the following line:

dtoverlay=w1-gpio,gpiopin=4,pullup=on

The w1-therm module should be added to the file “/etc/modules” anyway.

Each sensor will be one device. So we should find our DS18S20 as a device in

/sys/bus/w1/devices/

My device has the identification “10-0008028d56e7”. Inside this folder we can find the file “w1-slave”.

2c 00 4b 46 ff ff 0e 10 17 : crc=17 YES
2c 00 4b 46 ff ff 0e 10 17 t=21875

The crc=17 YES indicates that the sensor is active and t=21875 is the temperature (21.875°C).

Warning: When trying to use an 1wire sensor together with the RaspberryPi camera by using a modern kernel (e.g. 3.10.24+ up to at least 3.12.26+), then please also read the article “Solving mmal error with RaspberryPi Cam and 1wire modules”, when getting mmal errors.

Shell Script

If you would like to read the temperature by using a shell script, you can use this short example:

#!/bin/bash
sensor=$(cat /sys/bus/w1/devices/10-000802bf3a5e/w1_slave | cut -c 30-)
crc=$(echo $sensor | cut -c 8-10)
rawtemp=$(echo $sensor | cut -c 12-)
temp=$(echo "scale=2; $rawtemp/1000" | bc -l | awk '{printf "%3.2f", $0}')
if [ "$rawtemp" = "" -o "$crc" = "NO " -o "$crc" = "" ] ; then
        echo "N/A";
    else
        echo $temp
fi
exit 0

The advantage here is, that the script only returns the temperature, if the CRC checksum was correct.

Probably you have to install bc first, before beeing able to execute the script:

sudo apt-get install bc
tempsensr.txt · Last modified: 20.06.2015 14:07 by andreas