Introduction
While the Arduino has a built-in timekeeper called millis(), it only tracks time since the Arduino was last powered. What if you needed timekeeping for even when the Arduino is powered off? The Arduino is not able to tell whether it is a 'Thursday' or a 'Friday', either. For those purposes, a real-time clock module can be used instead.Â
In this guide, learn to connect a DS1307 Real Time Clock (RTC) Module with the Little Bird - Uno R3 and program it for time-keeping. Then, learn to use the RTC module and interrupts to put the Uno R3 in and out of power-saving mode.
Complete this guide to get started with using the DS1307 RTC Module with Arduino.
In this guide, learn to connect a DS1307 Real Time Clock (RTC) Module with the Little Bird - Uno R3 and program it for time-keeping. Then, learn to use the RTC module and interrupts to put the Uno R3 in and out of power-saving mode.
Complete this guide to get started with using the DS1307 RTC Module with Arduino.
Tools
-
-
-
-
-
-
-
-
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <Wire.h> #include "RTClib.h" #if defined(ARDUINO_ARCH_SAMD) // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero! #define Serial SerialUSB #endif RTC_DS1307 rtc; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; void setup () { #ifndef ESP8266 while (!Serial); // for Leonardo/Micro/Zero #endif Serial.begin(9600); if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if (! rtc.isrunning()) { Serial.println("RTC is NOT running!"); // following line sets the RTC to the date & time this sketch was compiled //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: rtc.adjust(DateTime(2019, 06, 27, 16, 43, 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(); delay(3000); }
-
-
-
-
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <Wire.h> #include "RTClib.h" #include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes #define interruptPin 2 //Pin we are going to use to wake up the Arduino RTC_DS1307 rtc; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; void setup () { pinMode(LED_BUILTIN, OUTPUT); //We use the led on pin 13 to indecate when Arduino is asleep pinMode(interruptPin, INPUT_PULLUP); //Set pin d2 to input using the built-in pullup resistor digitalWrite(LED_BUILTIN, HIGH); //turn built-in LED on Serial.begin(9600); if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if (! rtc.isrunning()) { Serial.println("RTC is NOT running!"); // following line sets the RTC to the date & time this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: //rtc.adjust(DateTime(2019, 06, 27, 20, 30, 0)); } } void loop () { DateTime now = rtc.now(); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); if (now.hour() == 20) { delay(5000); //wait 5 seconds before going to sleep Going_To_Sleep(); } } void Going_To_Sleep() { sleep_enable();//Enabling sleep mode attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2 set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep digitalWrite(LED_BUILTIN, LOW); //turn LED off delay(1000); //wait a second to allow the led to be turned off before going to sleep sleep_cpu();//activating sleep mode Serial.println("just woke up!");//next line of code executed after the interrupt digitalWrite(LED_BUILTIN, HIGH); //turn LED on } void wakeUp() { Serial.println("Interrrupt Fired");//Print message to serial monitor sleep_disable();//Disable sleep mode detachInterrupt(0); //Removes the interrupt from pin 2; }