Afficher l’heure sur LCD 20×4 i2c avec une horloge DS3231 et un arduino
Description :
Projet affichant l’heure en temps réel sur un LCD 20×4 I2C à l’aide d’une horloge RTC DS3231 et d’un Arduino.
Le DS3231 fournit une mesure précise de l’heure, que l’Arduino lit périodiquement pour mettre à jour l’affichage sur le LCD. Ce montage est idéal pour créer une horloge numérique, une station horaire ou tout projet nécessitant un suivi temporel fiable, tout en offrant un affichage clair et lisible grâce à l’écran I2C.
Prérequis :
- 1 x Carte Arduino Uno
- 1 x LCD 20×4 2004 avec adaptateur d interface série IIC I2C
- 1 x RTC DS3231 I2C
- 1 x Breadboard
- Fils de connexion
Version IDE :
- Arduino IDE 2.3.5
Bibliothèque :
- LiquidCrystal_I2C.h (version: 1.1.4 par
johnrickman) - RTClib.h (version: 2.1.4 par Adafruit)
- Adafruit BusIO (version version: 1.17.2 par Adafruit)
Vidéo de démonstration :
Schéma de câblage :


Code :
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include "RTClib.h"
char msg[20];
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
// Cette ligne définit l'horloge temps réel avec une date et une heure explicites.
//Par exemple, pour définir le 21 janvier 2014 à 3 heures du matin, vous appelleriez :
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
sprintf(msg, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.setCursor(6, 1);
lcd.print(msg);
sprintf(msg, "%02d/%02d/%02d", now.day(), now.month(), now.year());
lcd.setCursor(5, 2);
lcd.print(msg);
delay(1000);
}
