Time and calendar functions with a real time clock module based on the DS1307 and an Arduino

Since Arduinos have not directly the possibility to save time, we expand our Arduino with a small DS1307 Real-Time-Clock (RTC) boardWhich can, battery powered, “remember and count” the time and the date.

Tiny RTC (DS1307) Real Time Clock module and an Arduino Uno - blog.simtronyx.de

Tiny RTC (DS1307) Real Time Clock module and an Arduino Uno

We connect with a few cables our Arduino with the real-time clock, as in the following wiring circuit:

Zoom level:

ATMega328P1afbgchdie1j55101015152020252530afbgchdie30j
Move over elements (parts, jumper cable etc.) for more information (or tap in touch mode)...

Arduino
(NanoUno, Pro Mini)
Arduino
(Mega, Mega 2560)
Tiny RTC-Modul
DS1307
5V (VCC) 5V (VCC) VCC
GND GND GND
A4 (SDA) D20 (SDA) SDA
A5 (SCL) D21 (SCL) SCL
Tiny-RTC-DS1307-Echtzeituhr-Pinout-Rueckseite-blog.simtronyx.de

Tiny RTC Modul – Pinout

Sourcecode (Sketch):

In addition, we need to install the RTClib by jcw and then upload following code to the Arduino:

// Tiny RTC (DS1307)

#include <Wire.h>    // I2C-Bibliothek einbinden
#include "RTClib.h"  // RTC-Bibliothek einbinden

RTC_DS1307 RTC;      // RTC Modul

void setup(void) {

  // Initialisiere I2C  
  Wire.begin();
  
  // Initialisiere RTC
  RTC.begin();

  // Serielle Ausgabe starten 
  Serial.begin(9600);

  // Begrüßungstext auf seriellem Monitor ausgeben
  Serial.println("Starte Datum und Zeit - blog.simtronyx.de");

  // Prüfen ob RTC läuft  
  if (! RTC.isrunning()) {
    
    // Aktuelles Datum und Zeit setzen, falls die Uhr noch nicht läuft
    RTC.adjust(DateTime(__DATE__, __TIME__));
    
    Serial.println("Echtzeituhr wurde gestartet und auf Systemzeit gesetzt.");
  }
  else Serial.println("Echtzeituhr laeuft bereits.");

}

void loop(){
  
  DateTime now=RTC.now(); // aktuelle Zeit abrufen
  
  show_time_and_date(now);  // Datum und Uhrzeit ausgeben
  
  delay(30000); // 30 Sekunden warten bis zur nächsten Ausgabe
}

// Wochentag ermitteln
String get_day_of_week(uint8_t dow){ 
  
  String dows="  ";
  switch(dow){
   case 0: dows="So"; break;
   case 1: dows="Mo"; break;
   case 2: dows="Di"; break;
   case 3: dows="Mi"; break;
   case 4: dows="Do"; break;
   case 5: dows="Fr"; break;
   case 6: dows="Sa"; break;
  }
  
  return dows;
}

// Datum und Uhrzeit ausgeben
void show_time_and_date(DateTime datetime){
  
  // Wochentag, Tag.Monat.Jahr
  Serial.print(get_day_of_week(datetime.dayOfWeek()));
  Serial.print(", ");
  if(datetime.day()<10)Serial.print(0);
  Serial.print(datetime.day(),DEC);
  Serial.print(".");
  if(datetime.month()<10)Serial.print(0);
  Serial.print(datetime.month(),DEC);
  Serial.print(".");
  Serial.println(datetime.year(),DEC);
  
  // Stunde:Minute:Sekunde
  if(datetime.hour()<10)Serial.print(0);
  Serial.print(datetime.hour(),DEC);
  Serial.print(":");
  if(datetime.minute()<10)Serial.print(0);
  Serial.print(datetime.minute(),DEC);
  Serial.print(":");
  if(datetime.second()<10)Serial.print(0);
  Serial.println(datetime.second(),DEC);
}

Download Source Code

Then we start the serial monitor (with 9600 baud rate) and should get the following output:

DS1307 real-time clock - Serial Monitor - blog.simtronyx.de

DS1307 real-time clock – Serial Monitor

Now it is possible for us, even after a power failure (or after disconnecting the Arduino from the power supply and reconnecting it again later) display and use the exact time and date. If the clock (which may even happen) is going incorrect after some time, we have to remove the battery for a short time, reinsert it and then upload the sketch again.

Postgraduate projects with the DS1307 RTC module:

Components:

eBay: Breadboard jumper wires
RTC Module
Amazon: Breadboard jumper wires
RTC Module

Good?

FacebooktwitterredditpinterestlinkedinmailFacebooktwitterredditpinterestlinkedinmail