I made a simple HAT (Hardware Attached on Top) for the Raspberry Pi to read multiple DS18B20 temperature sensors — it was surprisingly easy. I used the measured temperatures to control a fan using Home Assistant.

Table of contents

First test

I started testing with a single DS18B20 break-out board.

First we need to do a couple of things on the Pi:

I’ve written a post on getting the Raspberry Pi ready for IoT
  • Enable 1-wire communication with raspi-config
  • Install python package sudo apt install python3-w1thermsensor

Now — let’s try to list the connected 1-wire devices:

$ ls /sys/bus/w1/devices/
28-0416a02b0eff  w1_bus_master1

Alright, we have one device! Now to read the temperature:

$ cat /sys/bus/w1/devices/28-0416a02b0eff/w1_slave
8e 01 4b 46 7f ff 0c 10 de : crc=de YES
8e 01 4b 46 7f ff 0c 10 de t=24875

24875 = 24.875°C

Sweet! 😄

Making the HAT

To make something more permanent — I used a 30×70 mm prototyping perfboard, schematic diagram and parts list below.

On the underside I attached a female pin-header for the Raspberry Pi GPIO ports.

And on the top; five 3-pin male pin-headers for DS18B20 temperature sensors, and a 10k pull-up resistor. All the 3-pin male pin-headers are connected in series.

Only three pins are needed to connect to the Raspberry Pi; VDD (5V), GND and GPIO4 (1-wire). I’m using a 20-pin header because it makes the HAT attach more firmly.

I connected a couple more DS18B20 temperature sensors and listed their IDs:

$ ls /sys/bus/w1/devices/
28-0416a02b0eff  28-0517021db9ff  28-051702869eff  w1_bus_master1

Reading the sensors

To read the sensor values I made a tiny python script:

https://github.com/thomasjsn/rpi-w1therm

It reads each sensor every 15 seconds and publishes the measurements over MQTT, it also calculates the average of all the connected sensors and publishes that.

See it in action here:

Home Assistant

My Home Assistant subscribes to the temperature measurements over MQTT, and I use the average temperature from all sensors to control the home office exhaust fan.

Home Assistant temperature group

Sensor configuration

sensor:
  - platform: mqtt
    state_topic: 'sensor/office/temp/above_rack'
    availability_topic: "sensor/office/temp/above_rack/status"
    name: 'Office ceiling above rack'
    unit_of_measurement: '°C'
  - platform: mqtt
    state_topic: 'sensor/office/temp/above_desk'
    availability_topic: "sensor/office/temp/above_desk/status"
    name: 'Office ceiling above desk'
    unit_of_measurement: '°C'
  - platform: mqtt
    state_topic: 'sensor/office/temp/room_center'
    availability_topic: "sensor/office/temp/room_center/status"
    name: 'Office ceiling center'
    unit_of_measurement: '°C'
  - platform: mqtt
    state_topic: 'sensor/office/temp/average'
    name: office_temp_average
    unit_of_measurement: '°C'

Fan automation

- alias: Fan on auto
  trigger:
    platform: numeric_state
    entity_id: sensor.office_temp_average
    above: 25
  action:
    service: switch.turn_on
    entity_id: switch.office_fan

- alias: Fan off auto
  trigger:
    platform: numeric_state
    entity_id: sensor.office_temp_average
    below: 24
    for:
      minutes: 5
  action:
    service: switch.turn_off
    entity_id: switch.office_fan

Schematic diagram

Raspberry Pi with 4 DS18B20 temperature sensors

Parts used

  • 1 × microSDHC card, Transcend, 32GB, class 10, 20MB/17MBs
  • 1 × PCB, perfboard prototyping, 30x70mm, 21cm2
  • 1 × Raspberry Pi 1 Model B+, 700MHz ARM CPU, 512MB RAM
  • 1 × Resistor, carbon film, 0.25W, 10 kΩ, 5%
  • 1 × Rubber foot, adhesive, SJ-5012, Ø 12.7x3.5 mm
  • 10 × Straight pin header, female, Dual row, 2.54mm
  • 12 × Straight pin header, female, Single row, 2.54mm
  • 15 × Straight pin header, male, Single row, 2.54mm
  • 4 × Temperature sensor DS18B20, probe, Water-proof, 1M cable
  • 1.5 m Wire, stranded, 0.22mm2, Black
  • 1.5 m Wire, stranded, 0.22mm2, Red
  • 1.5 m Wire, stranded, 0.22mm2, Yellow