Skip to content Skip to sidebar Skip to footer

Widget HTML #1

How to use Module RTC DS1307 Arduino Clock and AM/PM Code

RTC DS1307 Arduino 

How to use Module RTC DS1307 Arduino Clock and AM/PM Code - Real Time Clock (RTC) is a timer IC where it can precisely calculate seconds, minutes, hours, date, month and year.

There are many vendors of this RTC IC such as Microchip, STMicroelectronics, Micro Crystal, Epson, Maxim Integrated, NXP, Renesas Electronics. In this tutorial we will learn about Maxim Integrated's RTC, the DS1307.

 

A. DS1307 RTC Specifications

The DS1307 is the RTC from Maxim Integrated which is able to calculate the time until 2100. The details of the DS1307 RTC specifications are as follows:

  • Time calculation is valid until 2100
  • AM / PM format, 24 hours or 12 hours
  • I2C communication
  • Supports Battery Backup
  • Current is less than 500nA when using backup battery
  • IC 8-Pin DIP and 8-pin SO packages
  • Uses 32.768kHz digital
  • VCC 4.5 - 5.5 V
  • Vbat 2.0 - 3.5 V 

 

B. How RTC DS1307 Works?

The DS1307 will keep counting the time as long as the voltage source is supplied. There are 2 voltage sources that can turn on this IC, VCC and Vbat. 

VCC is the voltage that comes from the main circuit and Vbat is the voltage that comes from the  battery called the backup battery.

When the main circuit is on, the DS1307 IC will select the VCC voltage to operate, but when main circuit goes down, the DS1307 IC will automatically switch to the backup battery voltage. This  battery is usually a flat battery like the CR2032 type which has a 3V rating.

As long as the power supply voltage is available, this IC will continue to issue time data which is sent via I2C pins (SDA and SCL). 

A microcontroller, such as the Arduino Uno board, will retrieve this data via I2C communication as well.

In this article we are not using the DS1307 IC which is directly connected to the Arduino Uno, but we are using the DS1307 module.

What does Module mean? When we say the DS1307 IC, then we will say only one component, namely IC or chip. This RTC IC will be able to work if there is a minimum system. One of the minimum system components of the RTC is Crystal as a clock.

In this DS307 RTC module, there are several additional components such as crystals, backup batteries, EEPROM ICs and several resistors, capacitors and diodes. 

 

C. The DS1307 Arduino RTC Wiring

To be able to communicate between DS1307 Arduino , we need to connect them with I2C communication, SDA to SDA and SCL to SCL. The schematic are:

In the RTC DS1307 Arduino module above, there are two pin sides (VCC, GND, SDA and SCl) on the left and right. You can use one of them, left or right can also because it is connected.

Basically, to be able to communicate with I2C only uses 2 connections, namely SDA and SCL. You can determine which IC to choose, we only need to know the I2C address of the IC.

 

D. RTC DS1307 Arduino Library

Communicating using I2C mode using a separate protocol in sorting the data received. To make it easier for us to retrieve and map this data, we need a library. 

There are many libraries available on the internet to be able to access this RTC, such as from DinkyDink, Adafruit, Michael Margolis, Manjunath CV and so on. At least in operating the RTC module I have tried two libraries from DinkyDink and Adafruit.

The library from RinkyDink is a great library. From the experiments I experienced when using the library for DS3231, there was one problem when I used it for the advanced level, namely the day of the week couldn't sync.

So "I think" that is better library by Adafruit, RTClib. This RTClib library supports the DS3231, DS1307 and PCF8523 RTC chips.

For boards, this RTClib library DS1307 Arduino supports:

  • ATmega328 @ 16MHz: Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini
  • ATmega328 @ 12MHz: Adafruit Pro Trinket 3V
  • ATmega32u4 @ 16MHz: Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0
  • ATmega32u4 @ 8MHz: Adafruit Flora, Bluefruit Micro
  • ESP8266: Adafruit Huzzah
  • ATmega2560 @ 16MHz: Arduino Mega
  • ATSAM3X8E: Arduino Due
  • ATSAM21D: Arduino Zero, M0 Pro
  • ATtiny85 @ 16MHz: Adafruit Trinket 5V
  • ATtiny85 @ 8MHz: Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V 

 

To operate this RTC Arduino, please download the RTClib library at the following link:

Download the DS1307 Libary RTClib

Include that  RTClib library to the Arduino IDE by:

  • Open the Arduino IDE
  • Click Sketch> Include Library> Add .ZIP Library
  • Look for the RTClib-master.zip Library that friends have downloaded
  • Click Ok

 

In addition, you can also add libraries available in the arduino IDE library manager by: 

  • Click the Sketch menu> Include Library> Manage Libraries
  • Search for RTClib
  • Then click install.

 

E. Arduino DS1307 RTC program

1. Read RTC DS1307 Arduino

Now, we will try to read data from the RTC. The data includes the day, date, month, year, hour, minute and second.

#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char Month [13][10] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

void setup () {
  Serial.begin(9600);


  if (! rtc.begin()) {
    Serial.println("No RTC detected");
    Serial.flush();
    abort();
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC not running");
  }
}

void loop () {
  DateTime now = rtc.now();

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); 
  Serial.print(", ");
  Serial.print(now.day(), DEC);     
  Serial.print(' ');
  Serial.print(Month[now.month()]); 
  Serial.print(' ');
  Serial.print(now.year(), DEC);    
  Serial.print(", ");


  Serial.print(now.hour(), DEC);   
  Serial.print(':');
  Serial.print(now.minute(), DEC);  
  Serial.print(':');
  Serial.print(now.second(), DEC); 
  Serial.println('\n');

  delay(1000);
}

After the program is uploaded, open the Arduino IDE Monitor serial and you will see data that appears :

 

 

 

2. Set Time Manually As You Wish (Arduino RTC DS1307 Set Time)

Now we are going to create a program in which we can set the RTC manually. 

The basic commands are:

rtc.adjust (DateTime (year, month, date, hour, minute, second)); 

For example, we only want to change the date or hour, how do it?

Here I include the complete program. This program will display the RTC data on the Serial Monitor.

 To change data, send the following characters to the monitor serial:

   - DA for change the date

   - MO for change the month

   - YE for changing the year

   - HO for change the hour

   - MI for change minutes

   - SE for change seconds

 

/*
   www.arduino32.blogspot.com

   This program will display the RTC data on the Serial Monitor

   To change data, send the following characters to the monitor serial:
    - DA for change the date
    - MO for change the month
    - YE for changing the year
    - HO for change the hour
    - MI for change minutes
    - SE for change seconds
*/

#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char Month [13][10] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

String read_string = " ";
int read_int = 0;

void setup () {
  Serial.begin(9600);


  if (! rtc.begin()) {
    Serial.println("No RTC detected");
    Serial.flush();
    abort();
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC not running");
  }
}

void loop () {
  readRTC();
  setRTC();
  delay(1000);
}

void readRTC()
{
  DateTime now = rtc.now();

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); 
  Serial.print(", ");
  Serial.print(now.day(), DEC);    
  Serial.print(' ');
  Serial.print(Month[now.month()]); 
  Serial.print(' ');
  Serial.print(now.year(), DEC);   
  Serial.print(", ");
  Serial.print(now.hour(), DEC);   
  Serial.print(':');
  Serial.print(now.minute(), DEC); 
  Serial.print(':');
  Serial.print(now.second(), DEC);  
  Serial.println('\n');
}
void setRTC() {
  DateTime now = rtc.now();

  while (Serial.available() > 0) {

    read_string = Serial.readStringUntil('\n');
    read_string.trim();
    delay(100);

    // The day cannot be changed because it will change according to the date itself

   
    if (read_string == "DA") {
      Serial.print("Enter the date: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), read_int, now.hour(), now.minute(), now.second()));
    }

    
    if (read_string == "MO") {
      Serial.print("Enter the month: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), read_int, now.day(), now.hour(), now.minute(), now.second()));
    }

   if (read_string == "YE") {
      Serial.print("Enter the year: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(read_int, now.month(), now.day(), now.hour(), now.minute(), now.second()));
    }

   if (read_string == "HO") {
      Serial.print("Enter the hour: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), read_int, now.minute(), now.second()));
    }

    if (read_string == "MI") {
      Serial.print("Enter the minute: ");

      while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), read_int, now.second()));
    }

    if (read_string == "SE") {
      Serial.print("Enter the seconds: ");

     while (!Serial.available()) {}

      read_int = Serial.parseInt();
      Serial.println(read_int);

      rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), now.minute(), read_int));
    }
  }
}

 

Then the results are as follows:

 

3. How to set DS1307 RTC to match the timing of your computer or laptop

After we know the basic program and program set time, then how can we set the time according to the time on our computer / laptop when it is uploaded? 

The basic commands are:

rtc.adjust (DateTime (F (__ DATE__), F (__ TIME__)));

That command is placed in setup (); Here is the program.

#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char Month [13][10] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

void setup () {
  Serial.begin(9600);


  if (! rtc.begin()) {
    Serial.println("No RTC detected");
    Serial.flush();
    abort();
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC not running");
  }

  // the following line sets the RTC to the date & time from the computer when this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
  DateTime now = rtc.now();

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(", ");
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(Month[now.month()]);
  Serial.print(' ');
  Serial.print(now.year(), DEC);
  Serial.print(", ");


  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println('\n');

  delay(1000);
}

 

Then the results are as follows:

 

4. How to Change and Display AM / PM Format

Now that we can make a program for this RTC, let's learn how to display the time format in 12 hours, namely AM and PM.

By default, the above program will display the time in 24 hour format such as: 

  • 9.14
  • 14.20
  • 18.30
  • etc.

 

With a 12 hour format, the time displayed is:

  • 9.14 AM
  • 2.20 PM
  • 6.30 PM
  • etc.

 

 

The basic commands are:

(h> 12)? h - 12: ((h == 0)? 12: h), DEC

and

(h <12)? "AM": "PM"

 

Here is the program:

#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char Month [13][10] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int h = 0;

void setup () {
  Serial.begin(9600);


  if (! rtc.begin()) {
    Serial.println("No RTC detected");
    Serial.flush();
    abort();
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC not running");
  }

  // the following line sets the RTC to the date & time from the computer when this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
  DateTime now = rtc.now();

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(", ");
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(Month[now.month()]);
  Serial.print(' ');
  Serial.print(now.year(), DEC);
  Serial.print(", ");


  Serial.print((h > 12) ? h - 12 : ((h == 0) ? 12 : h), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print((h < 12) ? " AM" : " PM");
  Serial.println('\n');

  delay(1000);
}

 

The results displayed are as follows:

  

F. RTC 1307 Datasheet

If you need a datasheet to see a complete description with this DS1307 dan parameters, I attach the datasheeet as follows:

 

If you want to know how to display pdf files on a blog like the pdf above, you can read the article How to display a pdf on a blog or website.

I hope this article is useful. Thank you for visiting. 

Source: arduino32

Post a Comment for "How to use Module RTC DS1307 Arduino Clock and AM/PM Code"