Let your Arduino know what Time, Day and Year it is!
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.
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.
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.
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.
Copy and paste the above code in the Arduino IDE to program your Arduino.
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.