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

/*
 * REUK.co.uk - 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
 sensor1.begin();
 //set the resolution to 10 bit ADC
 sensor1.setResolution(10);
 
 // 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.
 delay(100);
}

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 neil@reuk.co.uk with details of your specific requirements.