1-wire temperature sensor DS1820 at Raspberry Pi (GPIO directly)

To measure the indoor or outdoor temperature with the Raspberry Pi, there are several possibilities. This article describes the version with the minimal amount of external components. This is based on the DS18S20 temperature sensor and the software emulation of the 1-wire protocol.

The DS18S20 and related DS18B20 and DS1822 are integrated circuits in a TO-92 housing containing the temperature sensor, analog-to-digital converter and 1-wire interface. The types mentioned are pin and software compatible, they differ substantially in the measurement accuracy and price. The three connectors (see picture left) are ground (GND, pin 1), data (DQ pin 2) and operating voltage (VDD, pin3). You can connect VDD and GND and operate the sensor with a parasitic power supply of 3-5 volts. The connection of the circuit with the computer is then possible with a simple two-wire twisted pair cable.

At this cable – the 1-wire bus – you may connect several DS1820 parallel. Each sensor has a unique code assigned by the manufacturer to identify itself.

1-wire temperature sensor DS1820 at Raspberry Pi (GPIO directly)

Raspberry Pi and 1-wire

Temperature measurement with the Raspberry Pi and the 1-wire temperature sensor DS1820 contains a list of different ways to drive a 1-wire bus from the Raspberry Pi. This paper describes the solution (1) with the least amount of external circuitry. You need apart from the sensors only a single resistor, as this solution completely simulates the 1-wire protocol in software.

Only one resistor

The data port DQ of the DS1820 is connected directly to the port GPIO4 of the GPIO interface of the Raspberry Pi. GND and VDD are at ground terminal GND. The parasitic power supply accomplished a pull-up resistor of 4k7 between the 3.3 volt connection 3V3 and GPIO4.

The resistor, a female connector for attachment to the GPIO port and the 1-wire connection is soldered to a small breadboard, which sits directly on the GPIO port. The 1-wire connection can be realized with a space-saving angled female connector. All this fits into the TEK-BERRY housing of the Raspberry Pi.

1-wire software drivers

The kernel modules required for the activation of the 1-wire temperature sensor are part of the Linux distribution Raspbian “wheezy”. You should, however, explicitly load them:

sudo modprobe w1-gpio pullup=1
sudo modprobe w1-therm

Important is the parameter pullup = 1, which tells the module that a parasitic power supply via a pull-up resistor is present. The modules create a subdirectory for each sensor found just below /sys/bus/w1/devices. The directory name is composed of the Family Code of the sensor and its unique identification number. Sensors of the type DS1820 and DS18S20 have the Family Code 10, DS18B20 has Code 28 and DS1822 the 22. In each subdirectory there is the file w1_slave containing the sensor status and measured temperature value:

cd /sys/bus/w1/devices
cd 10-000801b5*
cat w1_slave

0f 00 4b 46 ff ff 06 10 0c : crc=0c YES
0f 00 4b 46 ff ff 06 10 0c t=7375 

The file consists of two lines, each containing the hexadecimal register-dump of the sensor IC. At the end of the first line is the checksum (CRC) and the information whether it is a valid reading (YES). The second line ends with the temperature reading in thousandths of a degree Celsius. In the example, the temperature is thus 7.375 °C. The accuracy to three places after the decimal point is of course only apparent; the datasheet of DS18S20 states, for example, that the measurement accuracy is only ± 0.5° C. The actual temperature is so anywhere from 6.8 to 7.9° C.

If everything works so far, you should enter the two required modules into the file /etc/modules to make them automatically loading at boot time:

# /etc/modules
w1-gpio pullup=1

1-wire temperature sensor DS1820 at Raspberry Pi (GPIO directly) Schematic

Round Robin Database RRDtool

For a long-term recording of temperature readings and comfortable generation of graphics, the use of RRDtool is recommended. The articles Weather data acquisition with the USB WDE1 and Temperature measurement with the Raspberry Pi (USB-serial) explain this tool in detail.

First you have to install RRDtool with the aid of the Package Manager at the Raspberry Pi:

sudo apt-get install rrdtool python-rrdtool

The second installed package python-rrdtool is a Python interface to RRDtool, which is used later. At the beginning of the work with RRDtool is the definition of the database. The following example creates a database for two temperature sensors temp0 und temp1. The aim is to store one value per quarter hour (900 seconds). After ten days (= 960 values), a reduction takes place to one average, minimum, and maximum value per day. The retention time of these daily values is ten years (= 3600 values):

rrdtool create temperature.rrd --step 900 \
DS:temp0:GAUGE:1200:-40:80 \
DS:temp1:GAUGE:1200:-40:80 \
RRA:AVERAGE:0.5:1:960 \
RRA:MIN:0.5:96:3600 \
RRA:MAX:0.5:96:3600 \

Data Acquisition with Python

A Python scripts reads the special files w1_slave and inserts the temperature values into the round-robin database:


For more detail: 1-wire temperature sensor DS1820 at Raspberry Pi (GPIO directly)

© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top