# Arduino Alarm/Time Discrepancy

**URL:** <https://forum.arduino.cc/t/arduino-alarm-time-discrepancy/1360603>\
**Category:** UNO R4 WiFi\
**Created:** [March 5, 2025, 12:44pm UTC](https://forum.arduino.cc/t/arduino-alarm-time-discrepancy/1360603 "2025-03-05T12:44:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![m1cah2010](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/m1cah2010/32/1003603_2.png) [@m1cah2010](https://forum.arduino.cc/u/m1cah2010)\
**Post date:** [March 5, 2025, 12:44pm UTC](https://forum.arduino.cc/t/arduino-alarm-time-discrepancy/1360603/1 "2025-03-05T12:44:59Z")

</div>

Hi. I am using my Arduino to make an IoT project, and its connection to the internet is good. I programmed it to make an alarm go off at 8:00 and 20:00 (8:00 PM). Despite, it goes off at 7:33 and 19:33 (7:33 PM). If I make it print the time over serial, it is accurate, there is no time drift. If I reset the Arduino occasionally (press the reset button), then it works fine. Sometimes it even goes off at 7:33 and 8:00. Is anyone able to spot my oversight? Thanks for your time.  
Here is my main code (thingProperties.h doesn't matter):

```arduino
#include "thingProperties.h"
#include <DHT.h>

#define DHTPIN 2 // Pin where DHT11 is connected
#define DHTTYPE DHT11 // Sensor type
int BUZZER = 3; // Buzzer
int BTN = 4; // Interface button
bool isTime = false;
DHT dht(DHTPIN, DHTTYPE);

float tempAdjust = 5;
float humidAdjust = -5;
bool rang = false;

String MorningAlarmTime = "08:00";
String AfternoonAlarmTime = "20:00"; // Example for 3:00 PM

void checkAlarm() {
    time_t now = ArduinoCloud.getLocalTime();
    struct tm *timeinfo = localtime(&now);

    int currentHour = timeinfo->tm_hour;
    int currentMinute = timeinfo->tm_min;

    int morningHour = MorningAlarmTime.substring(0, 2).toInt();
    int morningMinute = MorningAlarmTime.substring(3, 5).toInt();

    int afternoonHour = AfternoonAlarmTime.substring(0, 2).toInt();
    int afternoonMinute = AfternoonAlarmTime.substring(3, 5).toInt();
  
     if (serialOutput) {
      Serial.print("Time is ");
      Serial.print(currentHour);
      Serial.print(":");
      Serial.println(currentMinute);
    }
  
    bool matchMorning = (
        (currentHour == morningHour || currentHour == morningHour + 12) &&
        currentMinute == morningMinute
    );

    bool matchAfternoon = (
        (currentHour == afternoonHour || currentHour == afternoonHour + 12) &&
        currentMinute == afternoonMinute
    );

    bool match = matchMorning || matchAfternoon;

    if (match && !rang) {
        isTime = true;
    } else if (!match) {
        isTime = false;
        rang = false;
    }
}

void setup() {
  pinMode(BTN, INPUT);
  pinMode(BUZZER, OUTPUT);
  // Initialize serial and wait for port to open
  Serial.begin(115200);
  delay(1500);

  dht.begin(); // Start DHT11 sensor

  // Initialize IoT Cloud properties
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  // Debugging level (0 = errors only, 4 = max details)
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update(); // Sync with IoT Cloud
  checkAlarm();
    if (isTime && !rang) {
        while (digitalRead(BTN) == LOW) { // HIGH means button is is pressed
            if (serialOutput) {
                Serial.println("BEEPING!");
            }
            digitalWrite(BUZZER, HIGH);
            delay(250);
            digitalWrite(BUZZER, LOW);
            delay(250);
            digitalWrite(BUZZER, HIGH);
            delay(250);
            digitalWrite(BUZZER, LOW);
            delay(1000);
        }
        rang = true; // Only mark it as "rang" after button press
    }

  humidAdjust = humidOffset;
  tempAdjust = tempOffset;
  // Read from DHT11 sensor
  float newTemp = dht.readTemperature();
  float newHum = dht.readHumidity() + humidAdjust;
  
  // if (newHum == humidity) {
  // humidity += 0.01; // Temporary change
  // }
  
  if (isnan(newTemp) || isnan(newHum)) {
    if (serialOutput) {
      Serial.println("❌ Failed to read from DHT11 sensor!");
    }
    return;
  }
  float temperatureF = ((newTemp * 9.0 / 5.0) + 32) + tempAdjust;

  // Print values to Serial Monitor
  if(serialOutput) {
    Serial.print("🌡 Temp: ");
    Serial.print(temperatureF);
    Serial.print("°F | 💧 Humidity: ");
    Serial.print(newHum);
    Serial.println("%");
  }

  // Update IoT Cloud variables
  temperature = temperatureF;
  humidity = newHum;

  delay(5000); // Wait 5 seconds before next read
}

```

---

<div class="post-metadata">

**Author:** ![kevinjwalters](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kevinjwalters/32/1126611_2.png) [@kevinjwalters](https://forum.arduino.cc/u/kevinjwalters)\
**Post date:** [March 18, 2025, 2:39pm UTC](https://forum.arduino.cc/t/arduino-alarm-time-discrepancy/1360603/2 "2025-03-18T14:39:15Z")

</div>

You code looks like it will work ok. I suspect the problem lies with `ArduinoCloud.getLocalTime()`. There are posts which suggest that goes _infrequently_ (by default) to a remote time service and relies on the local clock most of the time. The [UNO R4 boards do not have external quartz crystals](https://forum.arduino.cc/t/oscillator-on-r4-minima-appears-far-better-than-r4-wifi/1344218) and rely on the poor stability internal oscillators one of which drives the Renesas internal RTC ([ex1](https://forum.arduino.cc/t/realtimeclock-inaccurate/1342250) [ex2](https://forum.arduino.cc/t/uno-r4-wifi-rtc-runs-fast-by-about-1-second-per-minute/1145956)).

[Arduino RTC does not stay in sync despite use of ArduinoCloud.getLocalTime();](https://forum.arduino.cc/t/arduino-rtc-does-not-stay-in-sync-despite-use-of-arduinocloud-getlocaltime/1204968) has one solution as `TimeService.setSyncInterval(600)`. As with any remote service if the service is overloaded by clients making high frequency requests then they are likely to be ignored so I would advise caution on going for a lower value than the cited 600 seconds.
