Skip to content

Add sensor on the Raspberry

Joseph Pereniguez edited this page Dec 27, 2018 · 2 revisions

1.Prerequisites

We need to activate two modules in the Linux kernel to communicate with the probe.

Capture_d_écran_2018-06-30_à_15.27.08

Add this to the /etc/modules file :

w1-gpio
w1-therm

Restart Raspberry Pi, or enable modules without reboot :

sudo modprobe w1-gpio; sudo modprobe w1-therm

Add to /boot/config.txt :

dtoverlay=w1-gpio

This change fixes a problem related to firmware updates of the Pi dating from early 2015.

2.First communication with the probe:

Under GNU/Linux devices are managed as files. You must go to the directory /sys/bus/w1/devices containing these devices to list them. In reality they are symbolic links but it will be transparent for you.

Each probe is identified by its unique 8-byte serial number preceded by a byte indicating the probe type (28h for the DS18B20). The activation of the modules generates the creation of the directory corresponding to the probe.

Example :

ls /sys/bus/w1/devices/
28-021561a1f9ff w1_bus_master1

The temperature is readable in the file w1_slave :

more /sys/bus/w1/devices/28-021561a1f9ff/w1_slave
27 01 80 80 7f ff 7f 80 a6 : crc=a6 YES
27 01 80 80 7f ff 7f 80 a6 t=18437

The temperature is displayed in celcius x1000. "t=18437" corresponds to 18.437 °C. If instead of "YES" you have "NO" then the bus has failed to communicate with the probe.

As I told you, this is not just a static file. It is not the sensor that writes into this file at regular intervals, when you display its contents the 1-wire bus will query the sensor and display you the value. Hence the slight delay before the temperature returns.

And now you can read the temperature thanks to the DS18B20 sensor!

3.Lecture de la température par script:

There are several languages to recover the temperature: python or shell (bash).

cat /sys/bus/w1/devices/28-*/w1_slave | grep "t=" | awk -F "t=" '{printf("%.1f\n", $2/1000)}'

No ID to specify, simple and effective!

If you prefer to have two digits after the decimal point:

cat /sys/bus/w1/devices/28-*/w1_slave | grep "t=" | awk -F "t=" '{printf("%.2f\n", $2/1000)}'

Insert the code in a.sh script if you would rather call it than enter the line in CLI.

Clone this wiki locally