DS1302 RTC with arduino tutorial

DS1302 RTC with Arduino Tutorial

So why need an RTC? Is the Arduino has its own clock?

Yes, but that’s not that accurate plus when Arduino goes off it can’t always count the time.

So that’s why RTC comes to save us. RTC is just actually like a clock that always on. Never goes off except if it’s run out the battery which is should be anticipated.

Interfacingthe DS1302 with a microprocessor is simplified by using synchronous serialcommunication. Only three wires are required to communicate with the clock/RAM:CE, I/O (data line), and SCLK (serial clock)

DS1302 Wiring Diagram/Hookup Guide

Now let’s just get our hand dirty by start wiring this things with an Arduino. This is the diagram.

DS1302 with an arduino wiring diagram

DS1302 code with arduino

Now open the Arduino IDE and this is the sketch that we will use to read the RTC.

First, you need VirtuabotixRTC library. You can download the library here.

And we willprint the time in the serial monitor. But when this is your first time use theRTC, don’t forget to put the battery. This RTC uses CR2302 battery.

#include <virtuabotixRTC.h>
// Creation of the Real Time Clock Object
virtuabotixRTC myRTC(6, 7, 8);

void setup()  {
  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year
  myRTC.setDS1302Time(18, 10, 10, 7, 18, 9, 2020);

void loop()  {
  // This allows for the update of variables for time or accessing the individual elements.

  // Start printing elements as individuals
  Serial.print("Current Date / Time: ");
  Serial.print("  ");

  // Delay so the program doesn't print non-stop

Then in your first upload, you need to adjust the time in your RTC with time now. So the first upload you should change this line to time now.

myRTC.setDS1302Time(18, 10, 10, 7, 18, 9, 2020);

Now upload this sketch. We can check if the program works correctly by open the serial monitor. Once it’s correct. You should comment this line again and re-upload. If you don’t uncomment this line and reupload, every time the Arduino is turned ON it will change the time according to these lines. Which is not right.

//myRTC.setDS1302Time(18, 10, 10, 7, 18, 9, 2020);