🛠 Horloge persistante : RTC à pile + resynchronisation NTP
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
- 1 Arduino UNO R4 WiFi + coin cell battery (CR1220 format) inserted into the board's RTC holder
- 2.4 GHz WiFi network for NTP synchronization.
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
- Battery missing or inserted backwards in the holder: the RTC resets to zero on every restart, and the code seems to "forget" the time.
- Writing to the RTC in every loop: synchronize once at startup (or once a day), not continuously—NTP is not a crutch.
- Forgetting that NTP goes through the ESP32-S3: without a successful connection,
getTimereturns an invalid value; check consistency (epoch > 10^9) before writing to the RTC. - Daylight saving time: NTP provides UTC; handle the offset and legal time in your code, the RTC does not do this.
- Confusing RTC (seconds, maintained by battery) and
millis(resets on every power-up): for persistence, use the RTC; for short durations, use millis.
Other tutorials for this board
- Premier Blink sur la UNO R4 WiFi : la première UNO 32 bits
- Apprivoiser la matrice LED 12x8 intégrée
- Toucher capacitif : touchRead sans composant
- Se connecter au WiFi et lire l'heure NTP (WiFiS3)
- Inclinomètre sur la matrice : l'IMU LSM6DSV16X en I²C
- Serveur web embarqué : piloter la matrice depuis le navigateur
Code examples for this board
clignotante.inoArduinobouton-poussoir.inoArduinodac-analogique.inoArduinoentree-analogique.inoArduinopwm-respiration.inoArduinoservo-balayage.inoArduino