Issue activating a relay

I’m having a problem trying to activate a relay alongside an LCD display and an RTC unit and the relay doesnt click when the pin is set to high in the program, but if i replace the code with a simple on/off cycle it clicks fine. I’m not plugging or unplugging anything. The absolute only thing that changes is the program, and everything else in the program works flawlessly. I’ve tried both pin 9 and 10 for the relay with identical results. I’m using an UNO.

Program code:

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

RTC_DS3231 rtc;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  lcd.begin(16, 2);

  if (rtc.lostPower()) {
    lcd.setCursor(0, 0);
    lcd.print("RTC POWER LOSS");
    Serial.println("RTC POWER LOSS");
    while (true) {
      if (rtc.getTemperature() < 22) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature() > 23) {
        digitalWrite(10, LOW);
      }
      lcd.setCursor(0, 1);
      lcd.print("Temp: ");
      lcd.print(rtc.getTemperature());
      lcd.print(" C");
    }
  }
}

void loop() {
  DateTime dt = rtc.now();
  Serial.println(dt.dayOfTheWeek());
  //Sunday
  if (dt.dayOfTheWeek() == 0) {
    //5-8am
    if (dt.hour() >= 5 and dt.hour() < 8) {
      if (rtc.getTemperature < 23) {
        digitalWrite(10, HIGH);
      }
      else if (rtc.getTemperature() >= 24) {
        digitalWrite(10, LOW);
      }
    }
    //8am-2pm
    if (dt.hour() >= 8 and dt.hour() < 14) {
      if (rtc.getTemperature() < 21) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature() >= 22) {
        digitalWrite(10, LOW);
      }
    }
    //2pm-5am
    if (dt.hour() >= 14 or dt.hour() < 5) {
      if (rtc.getTemperature() < 22) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature >= 23) {
        digitalWrite(10, LOW);
      }
    }
  }

  //Monday
  if (dt.dayOfTheWeek() == 1) {
    //5-8am
    if (dt.hour() >= 5 and dt.hour() < 8) {
      if (rtc.getTemperature < 23) {
        digitalWrite(10, HIGH);
      }
      else if (rtc.getTemperature() >= 24) {
        digitalWrite(10, LOW);
      }
    }
    //8am-2pm
    if (dt.hour() >= 8 and dt.hour() < 14) {
      if (rtc.getTemperature() < 18) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature() >= 19) {
        digitalWrite(10, LOW);
      }
    }
    //2pm-5am
    if (dt.hour() >= 14 or dt.hour() < 5) {
      if (rtc.getTemperature() < 22) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature >= 23) {
        digitalWrite(10, LOW);
      }
    }
  }

  //Tuesday
  if (dt.dayOfTheWeek() == 2) {
    //5-8am
    if (dt.hour() >= 5 and dt.hour() < 8) {
      if (rtc.getTemperature < 23) {
        digitalWrite(10, HIGH);
      }
      else if (rtc.getTemperature() >= 24) {
        digitalWrite(10, LOW);
      }
    }
    //8am-2pm
    if (dt.hour() >= 8 and dt.hour() < 14) {
      if (rtc.getTemperature() < 18) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature() >= 19) {
        digitalWrite(10, LOW);
      }
    }
    //2pm-5am
    if (dt.hour() >= 14 or dt.hour() < 5) {
      if (rtc.getTemperature() < 23) {
        digitalWrite(10, HIGH);
        Serial.println("HIGH");
      }
      if (rtc.getTemperature >= 23) {
        digitalWrite(10, LOW);
      }
    }
  }

  //Wednesday
  if (dt.dayOfTheWeek() == 3) {
    //5-8am
    if (dt.hour() >= 5 and dt.hour() < 8) {
      if (rtc.getTemperature < 23) {
        digitalWrite(10, HIGH);
      }
      else if (rtc.getTemperature() >= 24) {
        digitalWrite(10, LOW);
      }
    }
    //8am-2pm
    if (dt.hour() >= 8 and dt.hour() < 14) {
      if (rtc.getTemperature() < 18) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature() >= 19) {
        digitalWrite(10, LOW);
      }
    }
    //2pm-5am
    if (dt.hour() >= 14 or dt.hour() < 5) {
      if (rtc.getTemperature() < 22) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature >= 23) {
        digitalWrite(10, LOW);
      }
    }
  }

  //Thursday
  if (dt.dayOfTheWeek() == 4) {
    //5-8am
    if (dt.hour() >= 5 and dt.hour() < 8) {
      if (rtc.getTemperature < 23) {
        digitalWrite(10, HIGH);
      }
      else if (rtc.getTemperature() >= 24) {
        digitalWrite(10, LOW);
      }
    }
    //8am-2pm
    if (dt.hour() >= 8 and dt.hour() < 14) {
      if (rtc.getTemperature() < 18) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature() >= 19) {
        digitalWrite(10, LOW);
      }
    }
    //2pm-5am
    if (dt.hour() >= 14 or dt.hour() < 5) {
      if (rtc.getTemperature() < 22) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature >= 23) {
        digitalWrite(10, LOW);
      }
    }
  }

  //Friday
  if (dt.dayOfTheWeek() == 5) {
    //5-8am
    if (dt.hour() >= 5 and dt.hour() < 8) {
      if (rtc.getTemperature < 23) {
        digitalWrite(10, HIGH);
      }
      else if (rtc.getTemperature() >= 24) {
        digitalWrite(10, LOW);
      }
    }
    //8am-2pm
    if (dt.hour() >= 8 and dt.hour() < 14) {
      if (rtc.getTemperature() < 18) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature() >= 19) {
        digitalWrite(10, LOW);
      }
    }
    //2pm-5am
    if (dt.hour() >= 14 or dt.hour() < 5) {
      if (rtc.getTemperature() < 22) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature >= 23) {
        digitalWrite(10, LOW);
      }
    }
  }

  //Saturday
  if (dt.dayOfTheWeek() == 6) {
    //5-8am
    if (dt.hour() >= 5 and dt.hour() < 8) {
      if (rtc.getTemperature < 23) {
        digitalWrite(10, HIGH);
      }
      else if (rtc.getTemperature() >= 24) {
        digitalWrite(10, LOW);
      }
    }

    //8am-2pm
    if (dt.hour() >= 8 and dt.hour() < 14) {
      if (rtc.getTemperature() < 21) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature() >= 22) {
        digitalWrite(10, LOW);
      }
    }
    //2pm-5am
    if (dt.hour() >= 14 or dt.hour() < 5) {
      if (rtc.getTemperature() < 22) {
        digitalWrite(10, HIGH);
      }
      if (rtc.getTemperature >= 23) {
        digitalWrite(10, LOW);
      }
    }
  }
  Serial.print("Time: ");   Serial.print(dt.hour());  Serial.print(":"); Serial.println(dt.minute());
  Serial.print("Temp: ");   Serial.print(rtc.getTemperature());   Serial.println(" C");
  Serial.println("");

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  lcd.print(dt.hour());
  lcd.print(":");
  lcd.print(dt.minute());
  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(rtc.getTemperature());
  lcd.print(" C");
  delay(1000);
}

Simple cycle code:

void setup() {
pinMode(9, OUTPUT);

}

void loop() {
digitalWrite(9, HIGH);

delay(1000);
digitalWrite(9, LOW);

delay(1000);
}

the pinMode(9, OUTPUT); is missing!

Good Luck & Have Fun!

Gil

With this code:

while (true) {
if (rtc.getTemperature() < 22) {
digitalWrite(10, HIGH);
}
if (rtc.getTemperature() > 23) {
digitalWrite(10, LOW);
}

Your relay pin will only go high or will go low and stay there for a very long time. Did you print the value returned by rtc.getTemperature to see what was returned? The would be the very first debugging step to take.

Paul

gilshultz:
the pinMode(9, OUTPUT); is missing!

Good Luck & Have Fun!

Gil

Aha, I knew it was something simple, thanks that fixed it.