Ardumotive Arduino Greek Playground
  • Home
    • About us
    • Contact
    • License
  • Arduino
    • Tutorials
    • Workshop
  • Raspberry Pi
  • DIY 3D Printer
  • News
    • Events >
      • Unboxing & Review
    • Blog
The 1st Arduino Playground in Greece - Open Source Hardware

Real Time Clock DS1307

Available Languages
Picture
Picture

Introduction 

Picture
A real-time clock (RTC) is a computer clock (most often in the form of an integrated circuit) that keeps track of the current time.

This module uses the RTC DS1307 IC and an extra EEPROM chip for farther use. 

In this tutorial you we learn how to use it to set the current time and date, and read it from the serial monitor of Arduino IDE.

As long as it has a coin cell to run it, the RTC will merrily tick along for a long time, even when the Arduino loses power, or is reprogrammed! You can use any CR1220 3V lithium metal coin cell battery.

What you will need - Hardware

For this tutorial you will need:
  • Arduino uno
  • Breadboard (optional)
  • DS1307 RTC Module (i2c)
  • CR1220 3V batterty (coin cell)

To use this module you also need to solder 4 cables.
Picture

The Circuit

The connections are pretty easy:
  • Vcc to Arduino 5V pin 
  • GND to Arduino GND pin 
  • SDA to Arduino  analog pin 4
  • ​SCL to Arduino analog pin 5

The code

Set current date and time with:
rtc.adjust(DateTime(2016, 11, 19, 19, 45, 0));
 // Format: YYYY,MM,DD,HH,MM,SS
​
  • Read day from RTC: now.day();
  • Read month from RTC: now.month();
  • Read year from RTC: now.year();
  • and etc...
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  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(2016, 11, 19, 19, 45, 0));   // <----------------------SET TIME AND DATE: YYYY,MM,DD,HH,MM,SS
  }
  delay(100);
}

void loop () {
    DateTime now = rtc.now();
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.year(), 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); //Print date and time every 3 sec
}
Download the code from here and open it with Arduino IDE. Inside you will also find and the RTC.h library. 
rtc_ds1307_tutorial.zip
File Size: 17 kb
File Type: zip
Download File


​Open the serial monitor from tools menu of Arduino IDE.

​The result:
Picture

Well done!

Picture
You have successfully completed one more Arduino "How to" tutorial and you learned how to use the DS1307 RTC module.
​
I hope you liked this, let me know in the comments.

Picture

Search Engine

Picture

Licence 

Picture

Help us to grow up!

Picture


Donate us
About us
License
Cookies policy

Visit the biggest Arduino Shop in Greece!

Picture
find us on dwrean.net
find us on Codebender
find us on Instructables
Developed and designed by Vasilakis Michalis Copyright © 2013 Ardumotive All Rights Reserved
All trademarks referenced herein are properties of their
Powered by Create your own unique website with customizable templates.
Design by DivTag Templates