🛠 Horloge persistante : RTC à pile + resynchronisation NTP

Arduino UNO R4 WiFi · avance · Schéma : IMG-uno-r4-wifi-avance-1-ur4wifi-horloge-rtc-ntp.svg — UNO R4 WiFi avec emplacement pile bouton du RTC surligné, symbole pile 3 V, serveur NTP à gauche, matrice LED affichant 12:34, horloge interne du RA4M1 annotée.

Objectives

Build a clock that survives power outages: the RA4M1 features an RTC (real-time clock) maintained by a coin cell battery inserted into the dedicated holder on the board. The professional strategy—unusually simple—is to synchronize the RTC with NTP when WiFi is available, then let it keep time on its own; the battery consumes so little power that the time persists for weeks without main power.

Hardware

Complete code

#include <WiFiS3.h>
#include "RTC.h"
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;
const char* SSID = "votre-reseau";
const char* PASS = "votre-cle";

// Trames 5x7 pour les chiffres 0-9 : voir la bibliothèque de trames
// du tuto matrice ; on n'affiche ici que l'heure hh:mm en deux blocs.
void afficherHeure(uint8_t h, uint8_t m);

void setup {
  Serial.begin(115200);
  matrix.begin;

  WiFi.begin(SSID, PASS);
  if (WiFi.waitForConnectResult == WL_CONNECTED) {
    unsigned long epoch = WiFi.getTime;     // NTP
    if (epoch > 1000000000UL) {
      RtcDateTime now = RtcDateTime::FromEpoch(epoch);
      RTC.SetDateTime(now);                   // on écrit dans le RTC matériel
      Serial.println(F("RTC synchronise via NTP"));
    }
  } else {
    Serial.println(F("Sans WiFi : heure du RTC conservée à la pile"));
  }
}

void loop {
  RtcDateTime t = RTC.GetDateTime;
  Serial.printf("%02u:%02u:%02u\n", t.Hour, t.Minute, t.Second);
  afficherHeure(t.Hour, t.Minute);
  delay(1000);
}

Expected verification

After synchronization, the time ticks second by second. Unplug everything (USB and jack) for ten minutes, then plug it back in: the displayed time is correct—the RTC kept running on its battery. Without a battery, the time resets to the compile-time value: this test proves the battery's usefulness.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials