RaspberryPi Thermostat Development Blog

Replacing a broken freezer thermostat with a RaspberryPi.


2020-06-22 -

RaspberryPi Diagram

Due to the corona virus pandemic, we have brought our basement freezer back into commission, to discover that its thermostat appeared to be broken. My brother in law addressed this by plugging the freezer into a homekit switch that turns on and off on a schedule to simulate the lost behavior, but i have long dreamed of using a Raspberry Pi for this job, with a temperature sensor and relay.

I started by ordering five TMP36 analog temperature sensors from Adafruit, only to later find that the Pi only has digital inputs. I think something went foul with the shipping, they never arrived, and after a few weeks i just canceled the order.

Then i ordered a five pack of DHT11 temperature and humidity sensors from Amazon, hooked one up to the Pi, stuck it in the freezer, and got surprising high readings. Then i double checked the sensors and learned that they measure 0 to 50 degrees celsius.

Feeling impressed with my own powers of obliviousness, i searched for a temperature sensor that was digital, and capable of measuring temperatures in a freezer, and found the DS18B20 waterproof sensor from Adafruit and Digi-Key. It arrived a couple days ago, and i connected it yesterday, found some instructions online on how to use it with the Pi, but the Pi is not appearing to detect it. Others have had this problem, i find, and i've tried a lot of troubleshooting already with no success.

My next idea is to try the DS18B20 with my Arduino Uno. If it works, i may content myself with a non-wifi connected thermostat.

Like this: DS18B20 (Digital Temperature Sensor) and Arduino

2020-06-23 - Arduino

I tried connecting to the Arduino; when trying to read the temperature, it got -127, which i find happens to many who have incorrect wiring, or are missing the resistor. I found some code to read the serial number of the sensor, but it only outputs "No more addresses"

PJRC - OneWire Library

Which is making me think that maybe it is dead.

2020-06-24 - It lives!

I chatted with Digi-key support, told them what i tried, they consulted a much more detailed data sheet for the ds18b20, recommended that i leave the resistor connected, but disconenct the sensor then run the temperature reading program. I did and got the expected -127. Then i tried connecting the sensor, hit the little red reset button on the Arduino, and when i walked back around to my computer, it was getting what appeared to be actual readings from the sensor!

Temperature: 24.06°C  ~  75.31°F
Temperature: 24.06°C  ~  75.31°F
Temperature: 24.06°C  ~  75.31°F
Temperature: 24.06°C  ~  75.31°F

I hooked it up the Raspberry Pi and it worked as well, wrote a little web server to show the temp, and moved the sensor into the freezer.

It'd be fun to add a graph / chart / visual thing, or d3 if i'm feeling ambitious.

FDA recommends keeping freezer at 0° F (-18° C)

Using AnyChart:

Freezer temp graph

Currently it oscillates between -11 and -13° C

D3 would be nice for the chart, like this:
How to Create Charts from External Data Sources with D3.js

And setting up a proper DB to store sensor data

i plugged the relay into GPIO17 (pin 11)

2020-07-01 - Measuring a working freezer

No news, except i do want to measure the working freezer in the kitchen, to see how it behaves. I just wondered though if it can control the cooler system speed, i.e. it's not off/on. And if so, if that'd be possible here. I imagine this motor is AC, and i doubt it was designed to be able to be run at different speeds, or if this is even a thing. Measuring the temperature in the kitchen freezer is easy. Measuring its motor voltage or whatever may be harder. Might have to just get it at the plug.

2020-07-08 - Measuring a working freezer, results

I measured the kitchen freezer over night, about 8 hours. It has a similar pattern, but longer cycles. They vary, but i'd say they're around 40 minutes, with cooling triggered between 8°F and 10°F, and stopped between 1°F and 2°F. I put the thermometer towards the top, i'm surprised it didn't reach the recommended 0°F. I wonder if it'd be worthwhile to measure again elsewhere.

I used rawgraphs to generate this svg from the timeseries data:

Kitchen freezer, 8hrs


Analyzing data from the kitchen freezer.

Datetime stamp / Event F C
Cooling starts
2020-07-07T06:28:05.841Z 10.9 -11.7
Cooling stops
2020-07-07T06:51:07.022Z 1.9 -16.7
average C/min during cooling phase: -12.9
Duration of cooling phase: 23:02
Warming stops, avg C/min during warming phase: 10.5
2020-07-07T07:14:18.302Z 9.3 -12.6
Duration of warming phase: 23:11

2020-07-23 - Bootstrap

Added bootstrap styling

Much has happened since my last update: the basic thermostat function is complete and working. My friend Josh and i worked on finishing the control logic. Then i did a bunch of crap: replaced the text file data store with a mysql mariadb, so the worker app takes temperature every minute, saves it to the database, then starts or stops the cooling based on the reading. The webserver reads the database and the GPIO pin that controls the freezer and makes all the info available in a webpage. I added bootstrap to make it prettier. But the front end is spaghetti, like everything else, and i'd like to make the following improvments: