Raspberry Pi Thermometer v1

A Raspberry Pi 3 with all cores running at 100% gets very hot, an example is when SETI is running. With a good heatsink such as a FLIRC case the core temperature is kept about 15-20 degrees Centigrade above ambient. This is fine until the ambient temperature starts rising beyond 30 Degrees Centigrade which happened during a recent heatwave.

I looked at using some PC heatsink style 5V fans powered by the raspberry Pi psu but they were noisy and on all the time - very distracting! It crossed my mind to use a Pi Zero and a temperature sensor to switch the fans on and off when my room temperature raised above 25 Degress C. A little research led me to the ModMyPi website which shows a simple example using a DS18B20 sensor. I used this as my starting point. I built a prototype on breadboard and started to write the code to do the switching. This quickly expanded to include a simple bargraph display for room temperature, a flashing LED for confidence that the Pi is running, logs on/off events and a shutdown button.


I bought a couple of cheap desktop USB fans which turned out to be considerably quieter and more efficient than the PC versions. My 13 SETI Pi's/Odroids are now kept nice and cool - and only when needed....

So the final build includes:

  • A Pi Zero W (but will run on any Pi)
  • 13 LED's to displays room temperature between 9 and 33 Degrees Centigrade in 2 Degree steps. Blue - Cold, Green - Comfortable, Yellow - switch the fans on!, Red - too hot!!
  • When the temperature goes out of range, below 8 and above 34 Degrees C, the 9 or 33 Degree LED will flash and log the event.
  • Switches via a relay, two 5V USB ports for fans - 500mA each.
  • Switch ON/OFF temperature - 25 Degrees
  • Logs ON/OFF events in a text file.
  • Logs if temperature falls below Zero Degrees.
  • Option with window displaying the current temperature - useful for a quick via VNC. See picture below.
  • All powered from the Raspberry Pi supply.
  • Uses the Waterproof DS18B20 Digital Temperature Sensor.
  • Shutdown button.
  • All the resistors are mounted on a Breakout Pi Zero from AB Electronics UK.

Script and hardware design:

RPi Thermometer Script (just copy & past into IDLE).

RPi Thermometer Script with temperature window(just copy & past into IDLE).

RPi Thermometer Schematic.

Pi Pi Pi Pi Pi Pi

Raspberry Pi Thermometer v2

I have been working to update the software to run on Python 3 and to change the log file format from .txt to the .xlsx spreadsheet format. I also added a small watchdog flashing red dot in the bottom right hand corner so I can tell it's still running when logged in remotely using VNC.

To do this I added a breadboard to the Pi400 so I could replicate part of the thermometer hardware to let me work on the software off line. I first updated the Tkinter GUI then changed the logging section to use openpyxl to log the data. I then added the flashing dot. The process to set up the final program on an SD card is as follows:

1) Download and copy the latest version of Pi OS to an SD card.

2) Enable the 1wire interface in the Raspberry Pi Configuration menu.

3) Check to see if the temperature sensor is enabled in the config.txt file by:
sudo nano /boot/config.txt
and look for the following line, if it's not there then add it:

4) To find the sensor ID:
sudo modprobe w1-gpio
sudo modeprobe w1-therm
then change directory:
cd /sys/bus/w1/devices/
which will show an ID that looks similar to this:
This is the number that has to replace the one that's in the script.

5) To install 'openpyxl' use the 'add and remove software' in the preferences menu to select the Python 3 module.

6) Copy the Thermometer script and .xlsx log file to /home/pi directory and to autostart at power on edit the following file by:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
and add the line:
@sudo /home/pi/
and to make the file executable:
sudo chmod +x

The script and example log file.

Problems with Python 3/1wire comms using the DS18B20 waterproof temperature sensor.
After running this script for a day it proved very unreliable, it would halt with an 'List index out of range' error at the line - while lines[0].strip()[-3:] != 'YES':

The definitions below are common place for reading and stripping the temperature data from the DS18B20 sensor. I've used them since June 2017 in Python 2 without so much as one missed beat and that's sampling once a second! but Python 3 has problems with it - it occasionally returns an empty string using f.readlines(). I proved this by inserting a print statement in the temp_raw() definition shown below and waited until the script halted, this displayed the empty string . The statement - while lines[0].strip()[-3:] != 'YES': does not like an empty string and throws up the error message.

Typical String returned from the sensor:
['b5 00 4b 46 7f 0c 10 f0 : crc=f0 YES\n', 'b5 00 4b 46 7f 0c 10 f0 t=11312\n']

Empty String:

To trap this error I inserted a while loop to look for the empty string then re-read the file if it is. Only when it returns with some data will it pass it on to while lines[0].strip()[-3:] != 'YES': to start the processes of checking and formatting the temperature data. Below are the two definitions I altered, the first one with the temporary 'print' statement and the second with a 'while' loop'. It's now running without issues so I have updated the zip file above with v2.3.

def temp_raw():

f = open(temp_sensor, 'r')

lines = f.readlines()


#print (lines)

return lines

def read_temp():

lines = temp_raw()

while len(lines)==0:

lines = temp_raw()

while lines[0].strip()[-3:] != 'YES':


lines = temp_raw()

temp_output = lines[1].find('t=')

if temp_output != -1:

temp_string = lines[1].strip()[temp_output+2:]

temp_c = float(temp_string) / 1000.0

return temp_c

February 2021 - After updating the script to Python 3 and using the latest RPi OS I found the Thermometer unreliable. It randomly stops after a few days - I can't see any problems with the script and no errors appear in the shell so I have reverted back to the Python 2 version. OS/Pi Zero Problem? This unit will be superseded with the new Pico ThermoSwitch project.