cycle a relay based on temperature

I have a thermostat project that currently does the following… If the temperature is < 75 F, RELAY1 is active, if the temperature is > 75 F RELAY1 is inactive.

However, what I would like to do is if the temperature is < 75 F, RELAY1 cycles 4 minutes active/1 minute inactive continuously until the temperature is > 75 F at which point RELAY1 should cycle 1 minute active/2 minutes inactive continuously.

My current code, which has been pieced together from multiple sample codes, is below.

Thank you in advance for any guidance.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define RELAY1  4  // Relay heating

// Setup LCD
LiquidCrystal_I2C lcd(0x27,20,4);

// 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 sensors(&oneWire);
DeviceAddress addr;

void setup(void)
  pinMode(RELAY1, OUTPUT);

  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  lcd.init();                      // initialize the lcd 

void loop(void)
  lcd.setCursor(0, 0); 
  sensors.requestTemperatures(); // Send the command to get temperatures
  for (uint8_t s=0; s < sensors.getDeviceCount(); s++) {
    // get the unique address 
    sensors.getAddress(addr, s);
    // just look at bottom two bytes, which is pretty likely to be unique
    int smalladdr = (addr[6] << 8) | addr[7];
  lcd.setCursor(0, 0);
  lcd.print("Current Temperature");
  lcd.print("      ");

    if (sensors.getTempFByIndex(s) > 75){
        lcd.print("Low Heat            ");
    } else {

        lcd.print("           High Heat");

First, you should only get the temperature once, using a variable to store it:

float tempF = sensors.getTempFByIndex(s);

Then you just need to check the temperature vaue against your threshold and act accordingly. If your code does not need to do anything else, you can do it simply this way :

if (tempF < 75) {
  digitalWrite(RELAY1,1);  // is this active?
  delay (4*60*1000);
  delay (1*60*1000);
} else {
  delay (1*60*1000);
  delay (2*60*1000);

If you want to do some LCD display during these cycles, do it before the delay lines.

Please understand that I am a hardware guy attempting to become a coding guy.

So I get where you're going with the if/else statement, but I am puzzled on how to make the float work.

I am not sure where to add the following line?

float tempF = sensors.getTempFByIndex(s);

Just before this line   lcd.print(sensors.getTempFByIndex(s));which becomes


Oh, ok that makes sense. So now if I understand correctly, the sensor "load" tempF with the current temperature, find it's next step in the if/else statement, and will not update again until one complete relay cycle, (on/off run) is complete. Is this correct? If so, it is perfect, many thanks. It seems so simple when you actually see it work...

Hi, Yes, that is correct, it is most of the time best to read your inputs at the start of the loop, this effectively takes a snapshot of your system, then your code acts on it.

Tom.. :)