Let your Arduino know what time and day it is!

//Let your Arduino know what time and day it is!
TUTORIAL


Let your Arduino know what Time, Day and Year it is!

OVERVIEW

Being able to know the time and date is something you’ll probably need sooner or later in some of your Arduino projects. Thankfully this can be easily achieved by using the DS1307 RTC module.

The DS1307 real time clock module uses the I2C bus so we will only need 2 pins to retrieve the date and time and display the results on the OLED display that we used in our last tutorial.

This module as a small battery to keep time clock ticking when you Arduino loses power or goes to sleep.

The type of battery that is required is a LIR2032, which is a rechargeable battery and can be hard to find sometimes, but the CR2032 is very common and easy to find, but is not rechargeable and cannot be used by default, so we will modify our module by removing the charging components on the module.

Of course if you have a LIR2032 battery you don’t need to do this.

We didn’t have one on hand when we started doing this tutorial, so we modified our little module so we could use a CR2032 instead.

DS1307 MODIFICATION

To use the CR2032 battery instead we will remove some components on the DS1307 module.

Being surface mount components, they are very small and using a magnifying glass with tweezers will almost be a requirement if you want to do this easily.

The way we do it is by getting a hold of the component we want to remove with tweezers, then heat one side of the component with the soldering iron then move to the other side until the component is removed.

As you can see in the before and after pictures, we removed : D1, R4, R5 and R6.

We then soldered a small wire to connect the pads of R6 together.

Now we can use a CR2032 battery with our module.

CONNECTIONS

Our OLED display uses the SPI Bus and the DS1307 uses the I2C Bus.

The OLED is connected to pins 9 through 13 and the DS1307 is connected to pin A4(SDA) and A5(SCL).

We use a breadboard to connect the VCC and Ground to both modules from our UNO.

THE CODE

We are using a couple of libraries in this tutorial:

The u8glib for the OLED display and the  DSRTC1307 Arduino Library for the DS1307.

We also include the Wire.h and Time.h library for the I2C Bus and Time manipulation.

First thing we need to do is initialize our DS1307 module with the current date and time.

We do this by running the “SetTime” sketch that comes with the DSRTC1307 library.

After this, we will upload the main code that will display the Time and Date on our OLED display.

As always please watch our Tutorial video for more information.

#include "U8glib.h"  // OLED
#include "Wire.h"  // I2C
#include "Time.h"  // Time Manipulation
#include "DS1307RTC.h"  // DS1307 RTC

char timebuf[10];  // Time
char datebuf[10];  // Date
int year2digit;  // 2 digit year
int year4digit;  // 4 digit year


U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8);  // D0=13, D1=11, CS=10, DC=9, Reset=8

const uint8_t brainy_bitmap[] PROGMEM = {
 0x00, 0x00, 0x03, 0xB0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x46,
0x00, 0x00, 0x00, 0x00, 0xFC, 0x47, 0xC0, 0x00, 0x00, 0x01, 0xCE, 0x4C, 0x60, 0x00, 0x00, 0x03,
0x02, 0x58, 0x30, 0x00, 0x00, 0x03, 0x02, 0x58, 0x10, 0x00, 0x00, 0x02, 0x02, 0x58, 0x18, 0x00,
0x00, 0x03, 0x06, 0x4C, 0x18, 0x00, 0x00, 0x07, 0x04, 0x44, 0x18, 0x00, 0x00, 0x0D, 0x80, 0x40,
0x3C, 0x00, 0x00, 0x09, 0xC0, 0x40, 0xE6, 0x00, 0x00, 0x18, 0x78, 0x47, 0xC2, 0x00, 0x00, 0x18,
0x0C, 0x4E, 0x02, 0x00, 0x00, 0x1F, 0x86, 0x4C, 0x7E, 0x00, 0x00, 0x0E, 0xC6, 0xE8, 0xEE, 0x00,
0x00, 0x18, 0x43, 0xF8, 0x82, 0x00, 0x00, 0x10, 0x06, 0x4C, 0x03, 0x00, 0x00, 0x30, 0x0C, 0x46,
0x01, 0x00, 0x00, 0x30, 0x18, 0x46, 0x01, 0x00, 0x00, 0x10, 0x18, 0x43, 0x03, 0x00, 0x00, 0x18,
0x10, 0x43, 0x03, 0x00, 0x00, 0x1C, 0x70, 0x41, 0x86, 0x00, 0x00, 0x0F, 0xE0, 0x40, 0xFE, 0x00,
0x00, 0x09, 0x1E, 0x4F, 0x06, 0x00, 0x00, 0x08, 0x30, 0x43, 0x86, 0x00, 0x00, 0x0C, 0x20, 0x41,
0x86, 0x00, 0x00, 0x06, 0x60, 0x40, 0x8C, 0x00, 0x00, 0x07, 0x60, 0x40, 0xB8, 0x00, 0x00, 0x01,
0xE0, 0x41, 0xF0, 0x00, 0x00, 0x00, 0x38, 0xE3, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xBE, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xCF, 0x82, 0x0C, 0x86, 0x46, 0x1F, 0xEF, 0xC3, 0x0C,
0xC6, 0xEE, 0x1C, 0xEC, 0xC7, 0x0C, 0xE6, 0x7C, 0x1C, 0xED, 0x8D, 0x8C, 0xFE, 0x38, 0x1C, 0xED,
0x8D, 0xCC, 0xDE, 0x38, 0x1D, 0xCD, 0xDF, 0xCC, 0xCE, 0x38, 0x1F, 0x8C, 0xF8, 0xEC, 0xC6, 0x38,
0x1F, 0xEC, 0x08, 0x0C, 0xC2, 0x18, 0x1C, 0xEC, 0x00, 0xC0, 0x00, 0x00, 0x1C, 0xFD, 0xFB, 0xC0,
0x00, 0x00, 0x1C, 0xFC, 0x63, 0x00, 0x00, 0x00, 0x1C, 0xEC, 0x63, 0xC0, 0x00, 0x00, 0x1F, 0xEC,
0x60, 0xC0, 0x00, 0x00, 0x1F, 0xCC, 0x63, 0xC0, 0x00, 0x00, 0x1F, 0x0C, 0x63, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x2B, 0x4F, 0x67,
0x42, 0x38, 0x7B, 0xEA, 0x86, 0xB2, 0x28, 0xC7, 

};

void draw(void) {
  u8g.drawBitmapP( 78, 5, 6, 50, brainy_bitmap);  // put bitmap
  u8g.setFont(u8g_font_fur11);  // select font
  u8g.setPrintPos(0, 30);  // set position
  u8g.print(timebuf);  // display time
  u8g.setPrintPos(0, 55);  // set position
  u8g.print(datebuf);  // display date
  
}

void setup(void) {
}

void loop(void) {
  

  tmElements_t tm;
    if (RTC.read(tm)) {
        year2digit = tm.Year - 30;  // 2 digit year variable
   //   year4digit = tm.Year + 1970;  // 4 digit year variable
      
      sprintf(timebuf, "%02d:%02d:%02d",tm.Hour, tm.Minute, tm.Second); // format time
      sprintf(datebuf, "%02d/%02d/%02d",tm.Day, tm.Month, year2digit);  // format date
  
      u8g.firstPage();  // Put information on OLED
      do {
        draw();
      } while( u8g.nextPage() );
  
  }
  
  delay(1000);  // Delay of 1sec

}
TUTORIAL VIDEO
DOWNLOAD

Copy and paste the above code in the Arduino IDE to program your Arduino.

Used Libraries:

Download the RTC1307 library here:  DS1307RTC.zip

Download the U8GLib library here:  https://bintray.com/olikraus/u8glib/Arduino/1.17

Download the Time library here:  Time.zip

Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder.

By | 2018-02-23T17:23:56-05:00 February 19th, 2015|Tutorials|

2 Comments

  1. Scott August 10, 2019 at 3:44 pm - Reply

    Hi and thanks for all of these great tutorials. Just recently I took a liking to micro controllers and programming.

    I am having some issues with my DS1307RTC and found your YouTube tutorial to be very informative and helpful. Unfortunately, I’m still having some issues… I’ve tried the Arduino community forums and really can’t get any clear insight or a straight answer there.

    So I followed your tutorial and I am able to set the time with the SetTime example and I can read the time with the ReadTest example; however, the time doesn’t change… It just repeats the time that was uploaded during SetTime execution…

    15:38:28.751 -> Ok, Time = 15:28:10, Date (D/M/Y) = 10/8/2019
    15:38:29.761 -> Ok, Time = 15:28:10, Date (D/M/Y) = 10/8/2019
    15:38:30.775 -> Ok, Time = 15:28:10, Date (D/M/Y) = 10/8/2019
    15:38:31.773 -> Ok, Time = 15:28:10, Date (D/M/Y) = 10/8/2019
    15:38:32.786 -> Ok, Time = 15:28:10, Date (D/M/Y) = 10/8/2019
    15:38:33.779 -> Ok, Time = 15:28:10, Date (D/M/Y) = 10/8/2019

    I’m plugged into wall power for power and the USB for serial monitor. Is the crystal maybe not oscillating? Have you ever heard of this before?

    I truly appreciate any insight you are able to provide. If you are too busy to bother with a cheap RTC, no worries!

    I included a link to my YouTube so you can verify that I am a real dood that’s interested in furthering my knowledge and not just a kid looking for an easy answer.

    THANKS!
    Scott

    • Amir October 28, 2019 at 5:50 pm - Reply

      Dear friend ;

      You must set run bit on ds1307 control register ( if i true remember it was register 0x07 ) after set time or date for first time.

      After one time this bit set system counter will begin to work until stop bit set on this register or main and backup power removed simultaneously.

      For more detail refer to ds1307 datasheet

Leave A Comment