Arduino Nano - Module RTC DS1307

Ce tutoriel vous explique comment utiliser un Arduino Nano pour lire la date et l'heure à partir du module RTC DS1307. En détail, nous apprendrons :

Préparation du matériel

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Real-Time Clock DS1307 Module
1×CR2032 battery
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Divulgation : Certains des liens fournis dans cette section sont des liens affiliés Amazon. Nous pouvons recevoir une commission pour tout achat effectué via ces liens, sans coût supplémentaire pour vous. Nous vous remercions de votre soutien.

À propos du module d'horloge temps réel DS1307

L'Arduino Nano inclut certaines fonctions liées au temps, telles que millis() et micros(). Cependant, celles-ci ne fournissent pas d'informations sur la date et l'heure (secondes, minutes, heures, jour, date, mois et année). Pour obtenir ces données, nous devons utiliser un module d'Horloge Temps Réel (RTC), tel que le DS3231 ou le DS1370. Le module DS3231 est plus précis que le DS1370. Pour plus de détails, consultez DS3231 vs DS1307.

Répartition des broches du module RTC DS1307

Le module d'horloge en temps réel DS1307 dispose de 12 broches, mais seulement 4 d'entre elles sont nécessaires pour son utilisation normale : SCL, SDA, VCC et GND.

  • Broche SCL : Il s'agit de la broche d'horloge série pour l'interface I2C.
  • Broche SDA : Il s'agit de la broche de données série pour l'interface I2C.
  • Broche VCC : Elle fournit de l'énergie au module et peut varier de 3,3V à 5,5V.
  • Broche GND : Il s'agit de la broche de masse.
Horloge temps réel module DS1307 brochage

Le module DS1307 possède un support de batterie pour une pile CR2032.

  • Si la batterie est insérée, l'heure sur le module sera conservée même lorsque l'alimentation principale est déconnectée.
  • Si la batterie n'est pas insérée, les informations horaires seront perdues si l'alimentation principale est déconnectée et elles devront être réinitialisées.

Diagramme de câblage

Schéma de câblage de l'horloge temps réel DS1307 pour Arduino Nano

This image is created using Fritzing. Click to enlarge image

Arduino Nano - Module RTC DS1307

DS1307 RTC Module Arduino Nano
Vin 3.3V
GND GND
SDA A4
SCL A5

Comment programmer le module RTC DS1307

  • Incluez la bibliothèque.
#include <RTClib.h>
  • Créez un objet RTC :
RTC_DS1307 rtc;
  • Démarrer le système d'horloge en temps réel (RTC).
if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); }
  • Réglez la RTC à la date et à l'heure sur le PC lors de la première compilation du sketch.
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  • Extrait les données de date et d'heure du module RTC.
DateTime now = rtc.now(); Serial.print("Date & Time: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(now.dayOfTheWeek()); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.println(now.second(), DEC);

Code Arduino Nano - Comment obtenir la date et l'heure

/* * Ce code Arduino Nano a été développé par newbiely.fr * Ce code Arduino Nano est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-nano/arduino-nano-ds1307-rtc-module */ #include <RTClib.h> RTC_DS1307 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (1); } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // manually sets the RTC with an explicit date & time, for example to set // January 21, 2021 at 3am you would call: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); Serial.print("Date & Time: "); 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.println(now.second(), DEC); delay(1000); // delay 1 seconds }

Étapes rapides

  • Cliquez sur l'icône Libraries dans la barre latérale gauche de l'IDE Arduino.
  • Recherchez “RTClib” et trouvez la bibliothèque RTC par Adafruit.
  • Appuyez sur le bouton Install pour installer la bibliothèque RTC.
Bibliothèque RTC Arduino Nano
  • Copiez le code et ouvrez-le avec l'IDE Arduino.
  • Cliquez sur le bouton Upload pour envoyer le code à l'Arduino Nano.
  • Ouvrez le moniteur série.
  • Consultez le résultat sur le moniteur série.
COM6
Send
Date & Time: 2021/10/6 (Wednesday) 9:9:35 Date & Time: 2021/10/6 (Wednesday) 9:9:36 Date & Time: 2021/10/6 (Wednesday) 9:9:37 Date & Time: 2021/10/6 (Wednesday) 9:9:38 Date & Time: 2021/10/6 (Wednesday) 9:9:39 Date & Time: 2021/10/6 (Wednesday) 9:9:40 Date & Time: 2021/10/6 (Wednesday) 9:9:41 Date & Time: 2021/10/6 (Wednesday) 9:9:42 Date & Time: 2021/10/6 (Wednesday) 9:9:43 Date & Time: 2021/10/6 (Wednesday) 9:9:44
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Arduino Nano - Comment créer un emploi du temps quotidien

/* * Ce code Arduino Nano a été développé par newbiely.fr * Ce code Arduino Nano est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-nano/arduino-nano-ds1307-rtc-module */ // Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <RTClib.h> // event from 13:50 to 14:10 uint8_t DAILY_EVENT_START_HH = 13; // event start time: hour uint8_t DAILY_EVENT_START_MM = 50; // event start time: minute uint8_t DAILY_EVENT_END_HH = 14; // event end time: hour uint8_t DAILY_EVENT_END_MM = 10; // event end time: minute RTC_DS1307 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } // sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // sets the RTC with an explicit date & time, for example to set // January 21, 2021 at 3am you would call: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); if (now.hour() >= DAILY_EVENT_START_HH && now.minute() >= DAILY_EVENT_START_MM && now.hour() < DAILY_EVENT_END_HH && now.minute() < DAILY_EVENT_END_MM) { Serial.println("It is on scheduled time"); // TODO: write your code" } else { Serial.println("It is NOT on scheduled time"); } printTime(now); } void printTime(DateTime time) { Serial.print("TIME: "); Serial.print(time.year(), DEC); Serial.print('/'); Serial.print(time.month(), DEC); Serial.print('/'); Serial.print(time.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[time.dayOfTheWeek()]); Serial.print(") "); Serial.print(time.hour(), DEC); Serial.print(':'); Serial.print(time.minute(), DEC); Serial.print(':'); Serial.println(time.second(), DEC); }

Code Arduino Nano – Comment créer un planning hebdomadaire

/* * Ce code Arduino Nano a été développé par newbiely.fr * Ce code Arduino Nano est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-nano/arduino-nano-ds1307-rtc-module */ // Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <RTClib.h> // UNCHANGABLE PARAMATERS #define SUNDAY 0 #define MONDAY 1 #define TUESDAY 2 #define WEDNESDAY 3 #define THURSDAY 4 #define FRIDAY 5 #define SATURDAY 6 // event on Monday, from 13:50 to 14:10 uint8_t WEEKLY_EVENT_DAY = MONDAY; uint8_t WEEKLY_EVENT_START_HH = 13; // event start time: hour uint8_t WEEKLY_EVENT_START_MM = 50; // event start time: minute uint8_t WEEKLY_EVENT_END_HH = 14; // event end time: hour uint8_t WEEKLY_EVENT_END_MM = 10; // event end time: minute RTC_DS1307 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } // sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // sets the RTC with an explicit date & time, for example to set // January 21, 2021 at 3am you would call: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); if (now.dayOfTheWeek() == WEEKLY_EVENT_DAY && now.hour() >= WEEKLY_EVENT_START_HH && now.minute() >= WEEKLY_EVENT_START_MM && now.hour() < WEEKLY_EVENT_END_HH && now.minute() < WEEKLY_EVENT_END_MM) { Serial.println("It is on scheduled time"); // TODO: write your code" } else { Serial.println("It is NOT on scheduled time"); } printTime(now); } void printTime(DateTime time) { Serial.print("TIME: "); Serial.print(time.year(), DEC); Serial.print('/'); Serial.print(time.month(), DEC); Serial.print('/'); Serial.print(time.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[time.dayOfTheWeek()]); Serial.print(") "); Serial.print(time.hour(), DEC); Serial.print(':'); Serial.print(time.minute(), DEC); Serial.print(':'); Serial.println(time.second(), DEC); }

Code Arduino Nano - Comment programmer à une date spécifique

/* * Ce code Arduino Nano a été développé par newbiely.fr * Ce code Arduino Nano est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-nano/arduino-nano-ds1307-rtc-module */ // Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <RTClib.h> // UNCHANGABLE PARAMATERS #define SUNDAY 0 #define MONDAY 1 #define TUESDAY 2 #define WEDNESDAY 3 #define THURSDAY 4 #define FRIDAY 5 #define SATURDAY 6 #define JANUARY 1 #define FEBRUARY 2 #define MARCH 3 #define APRIL 4 #define MAY 5 #define JUNE 6 #define JULY 7 #define AUGUST 8 #define SEPTEMBER 9 #define OCTOBER 10 #define NOVEMBER 11 #define DECEMBER 12 // event from 13:50 August 15, 2021 to 14:10 September 29, 2021 DateTime EVENT_START(2021, AUGUST, 15, 13, 50); DateTime EVENT_END(2021, SEPTEMBER, 29, 14, 10); RTC_DS1307 rtc; char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void setup () { Serial.begin(9600); // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (1); } // sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // sets the RTC with an explicit date & time, for example to set // January 21, 2021 at 3am you would call: // rtc.adjust(DateTime(2021, 1, 21, 3, 0, 0)); } void loop () { DateTime now = rtc.now(); if (now.secondstime() >= EVENT_START.secondstime() && now.secondstime() < EVENT_END.secondstime()) { Serial.println("It is on scheduled time"); // TODO: write your code" } else { Serial.println("It is NOT on scheduled time"); } printTime(now); } void printTime(DateTime time) { Serial.print("TIME: "); Serial.print(time.year(), DEC); Serial.print('/'); Serial.print(time.month(), DEC); Serial.print('/'); Serial.print(time.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[time.dayOfTheWeek()]); Serial.print(") "); Serial.print(time.hour(), DEC); Serial.print(':'); Serial.print(time.minute(), DEC); Serial.print(':'); Serial.println(time.second(), DEC); }

Vidéo

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!