April 13, 2013

Raspberry Pi Thermostat Hookups

I recently replaced my thermostat with my Raspberry Pi. In this post I explain how to make the hardware connections between the Raspberry Pi and the house wiring. Check out the Makeatronics Store if you want a PCB that does the connections for you.

In my house (and the vast majority of others) the thermostat wiring runs at 24V AC. There's a live wire (or two, depending on the setup) coming into the thermostat, and several others leaving to control the different components of the HVAC system. The thermostat's job is to close the circuit between the live wire and the appropriate control wire based on the temperature. A relay is an easy way to do it, but I find relay's cumbersome with their (usually) breadboard unfriendly pin layout and larger-than-can-be-supplied-by-gpio switching current requirements.

I have found, in my opinion, a much better way of switching AC current: the TRIAC. If you've ever used a MOSFET for DC current switching, a TRIAC is similar in application (but not physics), only for AC current. There are some important differences, however. Probably the biggest difference is that once a TRIAC is activated, it will not deactivate until the current through it drops below some small threshold current, and I'm not talking about the gate current. Because of this the TRIAC is pretty useless for DC since once there's current flowing it will never drop (unless you have another switch somewhere, which kinda defeats the purpose, or the battery dies...). In AC, the voltage (and hence current) will go to zero twice every cycle, so shutting the thing off is not an issue. An important side effect of this is that it's impossible to switch a TRIAC faster than the frequency of the AC line it's connected to, so you can forget about using it for PWM. But you CAN control when in the cycle it turns on to control the average current flow available to whatever it's connected to, and this is exactly what light dimmer switches do.

The 3 pin device is connected in line with the wire you want to switch, and a current is applied to the gate to turn the switch on. But TRIAC's are a little more complicated than MOSFET's. If you want to switch any appreciable amount of current (I was very surprised to learn that my HVAC control lines draw nearly 1.5A at 24V AC) you probably can't switch the TRIAC directly with a gpio. You need a second, smaller TRIAC as an intermediary. But don't fret, it's a very simple circuit.

I use an opto-coupled TRIAC as my small one for an added layer of isolation between the gpio and what I'm switching. Plus, it makes turning on and off the switch as easy as turning on and off an LED. All that needs to be done is to apply a voltage across the LED of the opto through the current limiting resistor R1.

The two TRIAC's are connected together as shown in the schematic, with the smaller one connecting the live wire to the gate of the larger TRIAC. Since the smaller one is actually quite small, R2 is necessary to limit the amount of current that would rush through it once it's activated. It is also important to get the order of the AC wires correct. Just because it's AC doesn't mean the wires are interchangeable!

Getting back to the thermostat, my house has 3 control wires that I'm interested in: the blower fan, heater, and air conditioner. That means I need 3 of these TRIAC circuits. The live 24V AC wire feeds into each of them, and the control wire goes on the load side. Connect the 3 opto's to 3 gpio's on the Raspberry Pi and voila! I have control.

In my setup I've used the following components:
R1 - 560 ohm
R2 - 100 ohm
Opto-TRIAC - MOC3063

The two TRIAC's are pretty cheap, $1.27 for the pair at the time of purchase from Digi-key. For all three channels I came in less than $5 for everything I needed to interface with the Raspberry Pi.

If you would like all 3 channels on a PCB instead of a breadboard, check out here.


  1. Not sure when this was posted but there's a typo. It should be voila.
    Anyway, great tutorial! I feel ready to do this project now!

  2. Greetings!
    I'm trying to make a similar circuit, which controls a water heater via 230VAC. Could you tell me, how you calculated R2? The heater uses 2kWatts, which means 8,7A. I use a 12A triac, but does it matter any other way?

    1. The value of R2 is not very critical. It's only purpose is to prevent so much current flowing through the opto-triac to burn it out. This can happen because the gate of a triac can sink (or source, depending on AC phase) a significant amount of current. Without R2 just as much current would try to flow through the gate as would through the triac, causing the opto to burn up. If you're doubling the voltage I would double R2.

    2. Thanks! Now it's working properly!

  3. I looked at the Eagle PCB design and I see that the BT134 uses the 'nich' library? Where did you find the component for Eagle? I can't seem to find it myself in the list.

    1. I've made my own library for parts I can't find in any of the existing Eagle libraries. I've updated the .zip file to include my library.