Building a Raspberry Pi Home Thermostat

Posted on July 12, 2015

Here I describe how to build a Raspberry Pi based homewbrew Nest thermostat. It’s twice as good in all the right dimensions: 2x as complex, 2x more unreliable, 2x the fun.

The end result:

And a screengrab of the webapp that controls the AC system:

A thermostat is just a relay

Home thermostats are very simple. In my home’s pre-existing thermostat base, connecting T1 and T3 turns on the AC. Connecting T1 and T2 turns on the heat.

decorative thermostat faceplate removed

“decorative” thermostat faceplate removed

Thermostat wiring

Thermostat wiring

Here in Houston the winters are warm enough that I’ve never turned on the heater. My system only switches the AC and needs a single relay to connect T1 and T3.

The relay requires 5V to switch, but the Raspberry Pi GPIO pins only output 3.3V. Hence the need for a simple NPN transistor based relay driver. A flyback diode is placed in parallel with the relay to dissipate the relay coil’s inductive current.

Relay schematic for controlling AC

Relay schematic for controlling AC

Relay PCB

Relay PCB

Cool! Now I can SSH into the raspberry pi and turn on the AC by running a Python script.

import RPi.GPIO as GPIO

############################################################################
# Initialize Raspberry PI GPIO

GPIO.setmode(GPIO.BOARD)
GPIO18_PIN = 12 # GPIO18 corresponds to pin 12 on the board.
GPIO.setup(GPIO18_PIN, GPIO.OUT)


############################################################################
# Set GPIO18 to output 3.3V, closing the relay latch and turning on the AC.

GPIO.output(GPIO18_PIN, True)

And to turn off the AC:

############################################################################
# Set GPIO18 to 0V, opening the relay latch and turning off the AC.

GPIO.output(GPIO18_PIN, False)

PiNet takes over

Running python through SSH to control the AC system is kind of annoying. Let’s add a temperature sensor to detect when a bang-bang controller should switch the AC on and off. As a bonus, the DHT22 temperature sensor also records the humidity.

temp and humidity sensor

temp and humidity sensor

The temperature sensor is read by a second Raspberry that lives in my bedroom. Now I have a network of two RPis that talk to each other via WiFi.

Temperature Pi

Temperature Pi

Energy savings

One nice thing about locating the temperature sensing Pi in the bedroom is that the AC turns off as soon as my room gets cold enough. No more wasting energy to cool the living room and kitchen while I’m sleeping.

Next up: tracking energy usage and savings with SmartMeter energy consumption data.

Parts list

part quantity cost
Raspberry Pi Model A or B 2 $70
DHT22 Temp+Humidity Sensor 1 $20
SD card 2 17
USB WiFi 2 $18.5
prototype boards 1 7
SPST 5v relay 1 1.25
2N3904 NPN Transistor 1 -
1N4001 Diode 1 -
various resistors - -
sparkfun beginner parts kit 1 $20
total ~$130 - $150

Resources