Arduino Thermostat with Full Code

We are often requested to build simple thermostats – devices to turn something on or off depending on a measured temperature. Pictured below is an example of one such thermostat we made recently using an Arduino Pro Mini.

arduino thermostat

This particular thermostat was designed to keep a 12V 2A output turned on unless the temperature measured on a waterproof DS18B20 digital temperature sensor reaches above 80°C. The output must then remain off until the measured temperature has fallen to below 70°C.

The thermostat was designed to control a low voltage pump, turning it off if the water in a hot water tank fed by a solar water heating panel gets to be too hot. The 10°C temperature difference between the turn off and turn back on temperatures is hysteresis to prevent the pump being turned on and off rapidly multiple times.

In the image above a MOSFET is used to switch the small pump on and off, but typically a relay would be used in most pump controlling thermostats so that high currents and high voltages can be switched by the low voltage powered thermostat. Therefore, in the Arduino sketch, all references are to a relay. The relay will need to be connected to the relevant Arduino pin via an NPN transistor.

Here is a poorly drawn and badly photographed circuit diagram of the thermostatic relay controller. Click on the image to view it in larger size.

circuit diagram for arduino thermostat relay controller

We used an Arduino Pro Mini because of its small size and price, but any Arduino board could be used for this type of controller.

While the Arduino Pro Mini has an on board 5V regulator, we prefer to use an external low drop 5V regulator (lp2950cz-5.0) because they are much more robust and will cope with higher input voltages – for example 12V batteries while they are being heavily charged.

The red LED shown on the circuit board at the top of this page is not used in this project.

Here is the full sketch (source code) for this thermostat

 * - 2017
 * This is a thermostat which will keep a relay closed
 * until the temperature measured reaches 80 degrees C, and then will then
 * then re-close the relay only when the temperature falls to 70 degres or below.

// For the temperature sensors
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire plugged into pin 3 on the arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensor1(&oneWire);

const int relay = 5;

// Fixed temperature thresholds - turn off output at >80C, and turn back on again when <70C
const float onTemp = 70.0;
const float offTemp = 80.0;

// Keep track of whether the relay is energised/closed (1) or open (0). Start with closed (1).
int relayStatus = 1;

void setup(void)
 // Start up the library
 //set the resolution to 10 bit ADC
 // Set up the relay output for the arduino, and start with it high
 // since the thermostat only turns off at >offTemp degrees.
 pinMode(relay, OUTPUT);
 digitalWrite(relay, HIGH);

void loop(void)
 // Read in the temperature of the sensor
 sensor1.requestTemperatures(); // Read temperature of sensor1
 float sensorOneTemperature = sensor1.getTempCByIndex(0);

if(relayStatus == 1 and sensorOneTemperature >= offTemp-0.00001){
 // Relay is currently closed, but the temperature exceeds offTemp - therefore open the relay
 digitalWrite(relay, LOW);
 // Remember the new status
 relayStatus = 0;

if(relayStatus == 0 and sensorOneTemperature <= onTemp+0.00001){
 // Relay is currently open from a previous high temperature event, but now the temperature 
 // is below the threshold so close it again
 digitalWrite(relay, HIGH);
 // Remember the new status
 relayStatus = 1;

// Wait 1/10th of a second before we measure the temperature again.

Hen House Door Controller for Dawn/Dusk or Timer Operation

Pictured below is a controller we recently made to open and close the door of a hen house automatically.

hen house door controller with light detector for dawn/dusk operation and a programmable digital timerWe make a lot of door controllers for a range of different needs, and in general they either open and close the door depending on times programmed into a timer, or automatically detect dawn and dusk with a light detector and open or close the door accordingly.

With this particular controller, the user can select between two modes – dawn/dusk mode or timer mode. If timer mode is selected, the door will open when the timer turns ON and close when the timer turns OFF. In this way the door can be made to open and close at times convenient to the owner – for example opening the door later on weekend mornings so that the poultry do not disturb neighbours.

If instead the dawn/dusk mode is selected, the door will open at dawn and close at dusk, with the ambient light level for the day-dusk and night-dawn thresholds calibrated by the user when setting up the controller and light detector in its location.

If you need any kind of automatic door controller, email with details of your specific requirements.

Solar Swimming Pool Heating Controller with Datalogger and Display

Our 2016 Solar Water Heating Pump Controller is one of our most popular products. Pictured below is a derivative recently requested by one of our pump controller for swimming pool heating with datalogger and lcd displayThis controller will be used to control the operation of a pump circulating water through a solar thermal panel. Our standard controllers have at least two sensors – one for the pool/tank and one for the solar panel – but for this particular project only one sensor could be used: at the solar panel.

Therefore, instead of using the temperature differential between the solar panel and the pool to decide when the pump should be turned on or off, the user sets a high temperature threshold above which the pump will turn on, and a low temperature threshold below which it will subsequently turn off.

Sensible starter values would be 70 degrees C at the panel to turn on the pump, and around below 35-40 degrees at the panel to turn off the pump. With experimentation and analysis (this device has a built in datalogger) it will be possible to refine these thresholds to maximise efficiency. The pump should not be turning on and off too frequently and running for very short times, but the panel should also not be left at very high temperatures for a long time or it will radiate heat away before it can be transferred to the water.

If this device was to be used in a small pool or hot tub, and if the arriving hot water from the panel is pumped straight in without pre-mixing, a lower pump turn on temperature would be essential so that no-one gets burned on the incoming hot water.

If you need any kind of pump controller, email with details of your specific requirements.

Turning Shooting Target Controller

Pictured below is a controller we recently made to control a shooting target for competitive around an Arduino Pro Mini, this device is used to edge or face a shooting target according to preset and user-set timings.

Display for a shooting target controller

By default, the target is edge on to the shooter. When the start button is pressed, a relay closes which turns the target face on to the shooter. An accurate timer then starts and counts down the number of seconds the shooter has until the target is turned edge on again.

There are five preset modes – 165 seconds, 35s, 8s, 4s, and 2s. There is also a sixth mode which the user can programme to be any duration from 1 to 999 seconds.

Buttons are fitted to the circuit board, but there are also connectors to which external buttons can be connected so that the device can be fitted in a box with just the two external buttons and display visible.

If you need any kind of shooting target controller, please email with details of your requirements.

Apple Store Fan Thermostat

Pictured below is a device we recently made to act as a fan controller for an apple storage store fan controlling thermostatThis device has two waterproof DS18B20 temperature sensors connected to it and a relay to switch the power to a fan which drives air from outside the apple store to the inside.

Whenever the outside temperature is measured to be a couple of degrees cooler than the inside temperature, the fan is turned on to drive the cooler air into the store. When the temperature differential falls to zero – i.e. the inside and outside temperatures are equal or the inside is cooler, the fan is turned off again.

This device also has built in frost protection to prevent freezing air from being blown into the apple store. If the outside temperature falls below 2 degrees C, the fan is turned off if it is on, and remains off until the outside temperature has increased by a couple of degrees.

If you need any kind of thermostat, email with details of your requirements.

Programmable Automatic Plant Propagator Thermostat

In our March 2015 blog post Automatic Plant Propagator Thermostat, we showed a device we had made to automatically turn on/off 12V heat pads under young plants to prevent them from getting too cold (or hot).

This device connected the power to the heat pads when the measured temperature was below 17 degrees and off again when it had got back up to 23 degrees.

Different plants require different temperature ranges, so in order to meet those demands, we created the device pictured below.

programmable automatic plant propagator thermostatThis device has been enhanced with a user programming button enabling the user to set the low temperature threshold at or below which heating pads should be turned on, and also the number of degrees of temperature increase which must occur before the heating pads are turned off again. This gives a far more flexible thermostat for a wide range of plant propagation.

If you need a thermostat like this or similar for plant propagation thermostat, email with details of your specific requirements.

Irrigation Pump Timer with Low Voltage Disconnect

Pictured below is a device we made to control the pump of an automatic and often unattended irrigation system which is solar powered.

irrigation system pump control timer with low voltage disconnectprogrammable digital timer is set with the times that the pump is to be run – typically very early in the morning and in the evening. The pump for this particular irrigation system is relatively high powered, so could not be switched directly by the timer. Therefore a 10A rated relay is built into the controller.

As this system is solar powered and also often left unattended, it was essential to include a low voltage disconnect which will automatically prevent the pump from running whenever the measured battery voltage is found to be <11.9V. It then waits until the battery has been charged back up to over 12.5V before allowing the pump to run again.

LED indicators are included to show when the programmable timer is ON, the status of the low voltage disconnect, and also the status of the pump switching relay.

If you need any kind of irrigation pump timer or controller please email with details of your requirements.

Testing Arduino Low Power Library with Pro Mini

In general when using an Arduino Pro Mini in one of our projects or products, we use an external LP2940CZ-5.0 voltage regulator instead of the on board regulator. This is because most things we make are for 12V battery systems, and the voltage from a 12V battery can get to well over 12V which is the specified upper input voltage for a Pro Mini. We have measured that one of these regulators with a 10uF capacitor across its 5.0V output, draws a quiescent current of only 0.079mA.

We have found that an Arduino Pro Mini, whether powered as described above, or with the on board regulator draws around 20mA @ 12.0V. This is very high for an always on battery powered device – it will use 500mAh (0.5Ah) of battery charge per day. Therefore, we are always interested in testing ways to minimise power consumption.

breadboard test of low power library for arduino pro mini

We set up the above test circuit with a 12V input, and our usual LM2940CT-5.0 regulator connected to an Arduino Pro Mini (16MHz / 5V). With a sketch containing just delay(8000); in the loop() function – i.e. the Arduino will wait 8 seconds, then wait another 8 seconds, then wait another 8 seconds, etc – we measured a current draw of 19.793mA @ 12.0V input voltage.

We downloaded and installed the following Lightweight low power library for Arduino – LowPower.h, and modified our test sketch as shown below to power down the microcontroller for 8 seconds within the loop.


This time we measured the current draw to be just 6.265mA @ 12.0V input voltage – a huge reduction of around 70% power consumption obtainable just by replacing the delay function with the powerDown function from the LowPower library.

We make a lot of dataloggers and monitoring devices which spend most of their time doing nothing – just waiting to take the next measurement. Therefore this low power library is a quick and easy way to reduce power consumption.

(Note that 8 seconds is the maximum power down duration that can be set with this library, but by using loops of multiple 8 second intervals in your sketches, you can create a low power consumption delay of as long as you want.)

A standalone arduino in a low power consumption circuitIf you use a Standalone Arduino on a breadboard directly powered by a battery pack of the correct voltage (i.e. no voltage regulation required), it is possible to run your Arduino off less than 50uA @5V (<1000th the power consumption of our tests above) and therefore power something for years with a AA cells or smaller. See here for an excellent article How to Run An Arduino For Years on a Battery from the Open Home Automation website where they use the JeeLib low power library with a standalone Arduino.

Solar Water Heating Pump Controller with SD Card Datalogger

Pictured below is a controller we recently made for a solar water heating system including a full datalogger.

solar water heating pump controller with sd card datalogger

This controller is based closely around our 2016 Solar Water Heating Pump Controller which already has basic datalogging functionality – minimum, maximum, and average temperature sensor readings displayed on the LCD.

To this we have added a micro SD card reader and a high accuracy DS3231 Real Time Clock (RTC). Every 15 seconds, the temperature of each of the sensors, the status of the controller, and the date and time are appended to a logging text file on the micro SD card.

arduino data log file from sd cardThe collected data can then be copied over from the SD card to a computer for detailed analysis, graph plotting, and so on.

This controller is based around an Arduino Pro Mini coupled with an LCD module, DS3231 RTC module, micro SD card module, and DS18B20 temperature sensors – all of which are readily available and economically priced. The only difficulties with this project came from the limitations of having only 32KB of flash memory (program space) on the Arduino Pro Mini – not a lot when including so many code libraries for the various modules and sensors as well as 750 lines of of project specific code for this complex datalogging controller.

running out of sram arduino

If you need any kind of datalogger, please email with details of your exact requirements.

Hen House Door Controller with Voltage Indication

Pictured below is another of our hen house door controllers – this time using a light detector to automatically detect dawn and dusk (user light level calibration), but with the addition of three battery voltage indicator LEDs.

Hen house door controller with low voltage indicationThis device will open and close a hen house door at dawn and dusk respectively when the measured ambient light level dictates. As this device is to be used with a small 12V battery, there is a chance that the battery will go low on charge preventing reliable operation.

Therefore we have added three LEDs (LED-1, LED-2 and LED-3) connected to flying leads so that they can be located somewhere easily visible while the controller itself is in an enclosure to keep it clean and dry.

When the battery voltage is measured to be greater than 12V, all three LEDs are turned on. When the voltage is between 11.8V and 12V, two are on. When the voltage is between 11.5 and 11.8V, 1 LED is on, and when the voltage is below 11.5V, no LEDs are on.

Each day when the user goes into the hen house to collect eggs, they have a quick visual indication of the state of the battery voltage so that they know when they need to think about recharging it.

If you need a hen house door controller, please email with details of any specific requirements you may have.