Testing temperature sensor


This post is to share a few details about the temperature sensor and related software code to read the values of the sensor.

The temperature sensor is a digital thermometer DS18B20 that uses 1-Wire communications bus system.. The code is straightforward as it’s reusing all the work done by Timo Furrer. His w1thermsensor GitHub repository contains a Python package and CLI tool to work with w1 temperature sensors like DS1822, DS18S20 & DS18B20 on the Raspberry Pi, Beagle Bone and other devices.

So this is how the code looks like and continuously reads temperature every second until we terminate the program:

from w1thermsensor import W1ThermSensor
import time

while True:
   for sensor in W1ThermSensor.get_available_sensors([W1ThermSensor.THERM_SENSOR_DS18B20]):
      print("Sensor %s has temperature %.2f" % (sensor.id, sensor.get_temperature()))

There are few wiring diagrams for Raspberry Pi and DS18B20 sensor available. The one we used is from this blog post:
Raspberry Pi Temperature Sensor: Build a DS18B20 Circuit.

The Raspberry Pi Zero W costs £12.82 (+delivery, in my case £2.99). It’s from here:

Digital Temperature Temp Sensor Probe DS18B20 Thermometer Waterproof Sensor costs $1.18 (+delivery to the UK $0.14). It’s from here:

4.7K resistor costs $0.65 (100 units, free delivery to the UK). It’s from here:

The code is available here:

In our case there will be three temperature sensors required and in fact connecting them is rather easy. No need for additional resistors or GPIO pins on the Raspberry Pi.

As described in Connect Multiple Temperature Sensors with Raspberry Pi blog post “reading temperature readings from multiple sensors down one wire is possible because each ds18b20 sensor has a unique serial number coded into it at manufacture which the Raspberry Pi can be used to identify them by.”

The code mentioned above also works, and this is how the output looks like:

Sensor 0210917769e5 has temperature 25.62
Sensor 020b917739d3 has temperature 6.12
Sensor 0417a2f718ff has temperature 23.94