Saturday, October 31, 2015

Electric Imp Thermostat

I decided to build a thermostat out of an electric imp, which will then be easily controlled remotely and with arbitrary complex programming based on any conceivable input / data.
Starting components, from upper left: electric imp, TMP36 temperature sensor, solid state relay, electric imp breakout board, perf board.  Not pictured:  op-amp

Background:  switch + temperature sensor = thermostat

The simplest thermostat controls when heat is supplied to an area of the house via two wires that run from the boiler up to the thermostat.  These wires carry ~30 V AC.  Part of the thermostat acts like a switch, connecting these two wires from the boiler:  when the switch is open, no heat is delivered; when the switch is closed, heat is delivered.  The thermostat measures the temperature in the room, and when it drops below the setpoint (e.g. 68 F), it closes the switch, turning on the heat, until the temperature rises above the setpoint, at which point it opens the switch.  It is effectively a negative feedback loop.  For this thermostat, the feedback loop is implemented in the electric imp using squirrel code, reading from a temperature sensor and controlling a switch based on the current measured temperature and the thermostat setpoint.

Temperature sensor

The temperature sensor used for the thermostat is exactly what I used to build some previous temperature sensors, as described in this previous blog post:

Briefly, a TMP36 temperature sensor chip from Analog Devices can be easily connected to the electric imp's analog-to-digital conversion pin and then read very quickly to measure the temperature.


A relay is a switch that can be controlled electronically.  Historically, relay's were mechanical - that is, they were controlled electronically but the way they worked was that the electronic activation caused a mechanical switch to physically close.  This mechanism is still widely used because it decouples the electronic control mechanism from the voltage / current being controlled, thus allowing very large voltages / and currents to be controlled by much, much smaller voltages / currents.  I first encountered them in the Ceyer Lab at MIT where mechanical relays like these allow 240 V and 10 A - power of 3600 watts - to flow through the switch, but only require 24 V and 0.75 A to be held open (power of 18 watts).  Also, if there is a spike in the voltage or current being carried by the switch - for example if the line voltage jumped, or if there was a short in the circuit causing high current - the relay may be destroyed, but the electronics that control it would be protected.  Finally, mechanical relays "fail safe" - if the control electronics fail for any reason (loss of power, component failure, etc.) it closes the switch.

For this application, we are nowhere near the power requirements of a mechanical relay, so we can use a "solid state relay", in which the switch is not mechanical, but instead built using solid state electronics (in other words, silicon).  I used the solid state relay described in the referenced blog post above (Sparkfun page here; Datasheet here).  The solid state relay still provides the benefit of decoupling (same as a mechanical relay), but the main benefit is that it requires much lower power to operate and comes in a single, simple package.  A possible downside is that it cannot handle anywhere near the amount of voltage / current of mechanical relay; however that is not an issue for this project.

I did some tests to learn about the solid state relay (SSR):  Used a DC power supply to control the voltage applied to the input of the SSR, measured the resistance across the output.  Minimum resistance of 1 k ohm when ~1 V applied.  Spec sheet for relay indicates max current should be 50 mA.  Measured the current when 1 V applied, appears to be ~70 mA, ~0.85 V applied has a current of 50 mA.

I did a test of using the state relay (SSR) to control the heating:  I wired the relay to the thermostat wires (that run to the boiler), then controlled the relay with a DC power supply set to ~0.85 V.  I turned on the DC power supply and boiler turned on.  I waited ~10 minutes, felt the baseboard heaters - warm!  Success!  I turned off the DC power supply, the boiler turned off.
SSR connected to the thermostat control wires and a DC power supply via a bread board
SSR connected to the thermostat control wires and a DC power supply via bread a board
SSR connected to the thermostat control wires and a DC power supply via a bread board
SSR connected to the thermostat control wires and a DC power supply via bread a board
I tested the electric imp Digital-to-Analog Converter (DAC):  I used pin 1 on the electric imp, used the electric imp web interface and the code below to set the voltage, measured the voltage using a digital multimeter - success:
local deviceId = hardware.getdeviceid();
server.log(format("Hello from device %s", deviceId))
local ssr_in = hardware.pin1;
The voltage output from the above is 0.24 multiplied by the power supply voltage for the electric imp, in this case 3.3 V, so the above output ~0.79 V.

Tested the electric imp to control the solid state relay (SSR):  I used a bread board to connect the SSR to the digital-to-analog converter (DAC) of the electric imp - pin 1 of the electric imp to the "+" lead of the SSR, ground (GND) of the electric imp to the "-" lead of the SSR.  Applied various voltages (0.85 V to 1.5 V, then to 3.3 V), and measured the resistance across the SSR switch with the digital multimeter - but did not at first observe the SSR switch closing - I expected that the resistance should go very low, e.g. 1-2, ohm when the switch is closed!  I measured the current being drawn by the SSR - it maxed out at 12 mA for the highest voltages I could apply via the electric imp's DAC.  Reading the spec sheet of the electric imp, it appears to say that the max current of the DAC is 4 mA.  I noticed that although the digital multimeter was displaying maximum resistance in the digits, the analog-like bar was flickering and in some cases appeared to drop quite low.  So I used the "MIN MAX" function on the digital multimeter, and then observed that the minimum resistance of the closed SSR was down to a few kiloohms.  When I measured it in "AVG" (average) mode I consistently observed an average resistance of a few kiloohms, for ~1.1 V applied by the DAC to the SSR.
To test the ability of the electric imp to control the solid state relay (SSR), the electric imp digita-to-analog conversion pin is attached directly to the input pins of the SSR via a bread board
Testing the electric imp controlling the solid state relay (SSR)

I tested the electric imp + SSR to control heating:  Set the electric imp to apply 0 V to the SSR, attached the SSR to the heating control wires for the boiler, then set the electric imp to apply 1.1 V to the SSR.  Boiler came on!  Success!

It was clear from the above that  I was both abusing the electric imp DAC by having it try to drive the SSR - providing 3x the recommended max current - and abusing the SSR by driving it with much less than the recommended voltage / current.  (From the tables and Figure 4 of the SSR datasheet, it appears the SSR should be driven with 1.2 V and 20 mA [at room temperature]).  To solve this problem I added an op-amp to the circuit, to act as a buffer between the DAC and the SSR.  Basically, the op-amp is powered by the DC supply voltage, and the DAC specifies the output voltage of the op-amp, which then applies that output voltage to the SSR, and is capable of doing so at the ~20 mA current required.  I bought op-amp LM385 from Sparkfun (item page here; datasheet here) to use.

Using the op-amp as a buffer, when I had the electric imp apply ~1.2 V to the op-amp, it then applied 1.2 V across the inputs of the SSR, and again I measured an average resistance on the order of a few kiloohms.  I then wired it up to the thermostat wires, and it worked to control the heating.  After talking to my good friend Phil, he suggested that the SSR would have to be under an actual current load to measure a "real" resistance, and that the multimeter's ohmeter function would not provide sufficient current to that.  Of course, he was right, and I measured a resistance of ~20 ohm when the SSR switch was closed and under load connected to the boiler's 29 V AC.

Full disclosure:  Phil was horrified at the use of the digital-to-analog conversion and the op-amp to effectively just switch between on and off, and he pointed out numerous reasonable, better designs.  But using the DAC and op-amp buffer allowed me to relive the glory days of grad school...and hopefully in the future I'll have a project that requires proper use of DAC e.g. using a continuous control feedback loop.

Putting it all together

Electric imp thermostat using  temperature sensor, op-amp and solid state relay
Fully built thermostat

Circuit diagram:
Circuit diagram of electric imp thermostat

Squirrel code / feedback loop:
The code is available in this github repository:


I started by reading this (which only gets about 1/3 of the way there):


  1. This comment has been removed by a blog administrator.

  2. This comment has been removed by a blog administrator.

  3. This comment has been removed by a blog administrator.

  4. Really impressive blog you got here. Thanks for posting.