Monday, January 30, 2017

Temperature Controlled Relay

Like many people who enjoy seeing birds, we have feeders and a bird bath in our yard. In the winter the bath requires a heater to keep it from freezing. However, it only gets below freezing here a few dozen days a year, so the heater is usually just wasting electricity. Also, it makes the water evaporate much faster when it's on. What if we could automatically turn it on only when needed?

All it takes to do this is a temperature sensor, a relay, and of course, an Arduino. An enclosure holds these along with an old cell phone charger for power. An extension cord runs through the enclosure and is spliced into the relay. This device could also be used to control heating tape for protecting exposed pipes.

Enclosure with sensor visible through small hole

ds18b20 sensor is soldered
and insulated

One channel relay module

The final setup with the bird bath

You must provide good insulation for all the AC lines in the enclosure. I used wire nuts for all of the connections except the relay. The relay had screw terminals and the bottom of the PCB was exposed, so all of that was wrapped heavily with electrical tape. The keep the enclosure waterproof, I sealed the sensor and the extension cord holes with epoxy.

Circuit Diagram for temperature Controlled Relay

And, finally, here is the code I used.

 *  Temperature Controlled Switch
 *  by
 *  29-Jan-2017
 *  read a ds18b20 temperature sensor and turn a relay on 
 *  when temperature is below freezing


#define RELAY 0                     // relay control pin
#define ONE_WIRE_BUS 2              // one-wire bus pin
OneWire oneWire(ONE_WIRE_BUS);      
DallasTemperature sensors(&oneWire);
DeviceAddress thermo;               // address of ds18b20

// One time setup
void setup() 
  pinMode(RELAY,OUTPUT);            // relay pin is OUTPUT
  digitalWrite(RELAY,0);            // start with relay OFF

  sensors.begin();                  // initialize one-wire system
  sensors.getAddress(thermo, 0);    // get device address of sensor

// Main loop
void loop() 
  sensors.requestTemperatures();    // get the current temperature
  float tempC = sensors.getTempC(thermo);

  if (tempC<1.0)                    // if it's near or below freezing
    digitalWrite(RELAY,1);          // then turn relay on
    digitalWrite(RELAY,0);          // else turn relay off
  delay(2000);                      // wait 2 seconds


No comments:

Post a Comment