Raspberry Pi + ds18b20

Dallas Semiconductors, now part of Maxim Integrated, has some very good chips thermometer in their series DS1820. A unique feature of these thermometers is that they can be connected by a single digital interface one leading data bus (1-Wire). You can even place multiple sensors (not only thermometers) on the same wire if you want.

Connecting one or more digital temperature sensors ds18b20 to Raspberry Pi carried out through 4.7 ohms resistor (Figure 1).

raspberry_pi+ds18b20
Figure 1 – Wiring sensors to ds18b20 Raspberry Pi

Use 1-Wire thermometers sold in Paspbery Pi is easy. All you need is to download two special kernel modules and all can read the temperature. Modules loaded set of command line following commands:
sudo modprobe w1-gpio
sudo modprobe w1-therm

The first tape activates protocol module for 1-wire GPIO4. The second tape load module that actually reads the temperature of the 1-wire bus. To automatically download modules after boot or shutdown Raspberry Pi, the above two belts should be added to the file /etc/modules. This is done as follows:
sudo nano /etc/modules

And add to the end of the file (if they are not there), two lines:
w1-gpio
w1-therm

Thus the question of reading the current temperature reading is reduced to a special file. This file is located in the directory /sys/bus/w1/devices. This directory contains one or more subdirectories. One that is always present, called w1_bus_master1 . But it does not interest us. We are interested directories species 28-000800575c8b . For each connected to 1-wire temperature sensor creates such a directory. It may take a few seconds for their appearance. The name of each directory – a unique serial number sensor ds18b20.
If you connect more than one sensor on the bus, you have to figure out which directory serial number which belongs sensors. This is done by heating a sensor, a temperature reading of all sensors and check the temperature of which is changing. Or you can connect sensors to turn and burn, which corresponds to the directory where the sensor.

Read the temperature sensor can be run as follows (replace the serial number on your sensor number):
cat 28-000800575c8b/w1_slave

As a result, you will see something like this:
30 00 4b 46 ff ff 0f 10 b8 : crc=b8 YES
30 00 4b 46 ff ff 0f 10 b8 t=23812

Byte hexadecimal numbers reflect the information that your sensor returned the Raspberry Pi. The first tape checks whether correct checksum CRC (last byte response must be the same as the calculated CRC). If everything is correctly displayed “YES”, if not – output “NO”. If the answer is “NO”, it is necessary to re-read the data from the sensor, because this temperature is not correct. The second tape contains the read temperature in degrees Celsius, multiplied by 1000, so we divide it by 1000 to make readable to humans.
Here’s a sample script to read and play all sensors on the bus.

#! /bin/bash

# This script reads the temperature from all connected 1-wire temperature
# sensors of the DS1820 family.
# The script will answer nothing if it can't find any sensors.
#
# Author: San Bergmans
# www.sbprojects.com

W1DIR="/sys/bus/w1/devices"

# Exit if 1-wire directory does not exist
if [ ! -d $W1DIR ]
then
echo "Can't find 1-wire device directory"
exit 1
fi

# Get a list of all devices
DEVICES=$(ls $W1DIR)

# Loop through all devices
for DEVICE in $DEVICES
do
# Ignore the bus master device
if [ $DEVICE != "w1_bus_master1" ]
then
# Get an answer from this device
ANSWER=$(cat $W1DIR/$DEVICE/w1_slave)

# See if device really answered
# When a previously existing device is removed it will
# read 00 00 00 00 00 00 00 00 00, which results in a
# valid CRC. That's why we need this extra test.
echo -e "$ANSWER" | grep -q "00 00 00 00 00 00 00 00 00"

if [ $? -ne 0 ]
then
# The temperature is only valid if the CRC matches
echo -e "$ANSWER" | grep -q "YES"
if [ $? -eq 0 ]
then
# Isolate the temprature from the second line
TEMPERATURE=$(echo -e "$ANSWER" | grep "t=" | cut -f 2 -d "=")
# Isolate integer and fraction parts so we know where
# the decimal point should go
INTEGER=${TEMPERATURE:0:(-3)}
FRACTION=${TEMPERATURE:(-3)}

# Restore the leading 0 for positive and negative numbers
if [ -z $INTEGER ]
then
INTEGER="0"
fi
if [ "$INTEGER" == "-" ]
then
INTEGER="-0"
fi

# Write result of this sensor
echo "$DEVICE=$INTEGER.$FRACTION"
else
# A CRC was found, show error message instead
echo "$DEVICE=CRC error"
fi
fi
fi
done

Оставить ответ

Обязательные поля помечены*

Этот сайт использует Akismet для борьбы со спамом. Узнайте как обрабатываются ваши данные комментариев.