|

Afficher l’heure sur LCD 20×4 i2c avec une horloge DS3231 et un arduino

Version IDE :

  • Arduino IDE 2.3.5

Bibliothèque :

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);
}

Publications similaires

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *