Setting the DS1307 Real Time Clock Using the Serial Monitor

Introduction: Setting the DS1307 Real Time Clock Using the Serial Monitor

About: Why would i buy something ready made when i can make it myself with half the features for twice the money? DIY!

The hardware is the standard setup and is as follows.

SDA of the RTC to pin 4 of the Arduino.
SCL of the RTC to pin 5 of the Arduino

Then of course pos and neg.
Just ignore the gizDuino info in the setup picture. left side of the page.

Complete details of this RTC can be found here.  Just click on Technical Manual on the left side of the page.

Step 1: Code

Copy and past this into the Arduino IDE and burn to the board. For this i am using an ACEDuino that has a ATMega 328P.
To set the date and time is all done is the serial monitor, which is the easiest i have found. 

///////////////////////////////////////////
// RTC data and time setter              //
//                                       //
// This sample program allows the user   //
// to set the date and time of an RTC    //
// using I2C.                            //
//                                       //
// Codes by:                             //
// eGizmo Mechatronix Central            //
// Taft, Manila, Philippines             //
// http://www.egizmo.com                 //
// April 15, 2013                        //
///////////////////////////////////////////

#include <Wire.h>
const int DS1307 = 0x68; // Address of DS1307 see data sheets
const char* days[] =
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const char* months[] =
{"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"};
 
// Initializes all values: 
byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  delay(2000); // This delay allows the MCU to read the current date and time.
 
  Serial.print("The current date and time is: ");
  printTime();
  Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor");
  Serial.println("Would you like to set the date and time now? Y/N");
 
  while (!Serial.available()) delay(10);
  if (Serial.read() == 'y' || Serial.read() == 'Y')
 
  // This set of functions allows the user to change the date and time
  {
    Serial.read();
    setTime();
    Serial.print("The current date and time is now: ");
    printTime();
  }
 
 
  Serial.println("Thank you.");
}

// Continuous function for converting bytes to decimals and vice versa
void loop() {
}
byte decToBcd(byte val) {
  return ((val/10*16) + (val%10));
}
byte bcdToDec(byte val) {
  return ((val/16*10) + (val%16));
}


// This set of codes is allows input of data
void setTime() {
  Serial.print("Please enter the current year, 00-99. - ");
  year = readByte();
  Serial.println(year);
  Serial.print("Please enter the current month, 1-12. - ");
  month = readByte();
  Serial.println(months[month-1]);
  Serial.print("Please enter the current day of the month, 1-31. - ");
  monthday = readByte();
  Serial.println(monthday);
  Serial.println("Please enter the current day of the week, 1-7.");
  Serial.print("1 Sun | 2 Mon | 3 Tues | 4 Weds | 5 Thu | 6 Fri | 7 Sat - ");
  weekday = readByte();
  Serial.println(days[weekday-1]);
  Serial.print("Please enter the current hour in 24hr format, 0-23. - ");
  hour = readByte();
  Serial.println(hour);
  Serial.print("Please enter the current minute, 0-59. - ");
  minute = readByte();
  Serial.println(minute);
  second = 0;
  Serial.println("The data has been entered.");
 
  // The following codes transmits the data to the RTC
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekday));
  Wire.write(decToBcd(monthday));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(byte(0));
  Wire.endTransmission();
  // Ends transmission of data
}


byte readByte() {
  while (!Serial.available()) delay(10);
  byte reading = 0;
  byte incomingByte = Serial.read();
  while (incomingByte != '\n') {
    if (incomingByte >= '0' && incomingByte <= '9')
      reading = reading * 10 + (incomingByte - '0');
    else;
    incomingByte = Serial.read();
  }
  Serial.flush();
  return reading;
}


void printTime() {
  char buffer[3];
  const char* AMPM = 0;
  readTime();
  Serial.print(days[weekday-1]);
  Serial.print(" ");
  Serial.print(months[month-1]);
  Serial.print(" ");
  Serial.print(monthday);
  Serial.print(", 20");
  Serial.print(year);
  Serial.print(" ");
  if (hour > 12) {
    hour -= 12;
    AMPM = " PM";
  }
  else AMPM = " AM";
  Serial.print(hour);
  Serial.print(":");
  sprintf(buffer, "%02d", minute);
  Serial.print(buffer);
  Serial.println(AMPM);
}


void readTime() {
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.endTransmission();
  Wire.requestFrom(DS1307, 7);
  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read());
  weekday = bcdToDec(Wire.read());
  monthday = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

Step 2: Open the Serial Monitor

Open up the serial monitor and this what you will see. The date and time will not be the same but that's OK we are here to correct this.
Please follow along with the pictures for the input sequence.
If you mess up just start over until you enter everything correctly.  

2 People Made This Project!

Recommendations

  • Creative Misuse Contest

    Creative Misuse Contest
  • Metalworking Contest

    Metalworking Contest
  • Fix It! Contest

    Fix It! Contest

62 Discussions

its work,very simple thanks brother........

0
None
LoicK1

2 years ago

Hi,

i have a little problem, i've this message:

The current date and time is: ¾û�ÍÇÿ^¿þºþ^ß±¿yw}¿•�ß©Óïp}ŸöÿÿÙ?î݇¿wÿ—ûÜ<ß÷÷Û{÷Ïï,ûóÞ[ïW¼;}Û]ÝßÍÿûþíséÿWû÷éÿÿùùËÅþïmÖk}ãî~ÞïxŸû_ÿ¶Ù¿o®Íý¿Wÿï}Þÿ,Ý 165, 20165 153:165 PM
Please change to newline ending the settings on the lower right of the Serial Monitor
Would you like to set the date and time now? Y/N

The date's format is'nt ok.

The probleme already exist after settings:

The data has been entered.
The current date and time is now: ¾û�ÍÇÿ^¿þºþ^ß±¿yw}¿•�ß©Óïp}ŸöÿÿÙ?î݇¿wÿ—ûÜ<ß÷÷Û{÷Ïï,ûóÞ[ïW¼;}Û]ÝßÍÿûþíséÿWû÷éÿÿùùËÅþïmÖk}ãî~ÞïxŸû_ÿ¶Ù¿o®Íý¿Wÿï}Þÿ,Ý 165, 20165 153:165 PM
Thank you.

If someone can help me...

Thanks!

1 reply

The code worked fine for me. I also have the code how to display the time and date on the serial monitor. If anyone has any doubt regarding the code can contact me.

5 replies

Brother when I have run the code I got the output as kkkkkkkkkkkkkkkkkkk..........
So please help me



I am getting the time format in 165, ..153 something like this. What is the wrong? can you suggest me..

Hello Hemanthgoalla,

I am not able to set the time in required format. Suggest me on that.

Thank you..

Hey, I am having trouble in displaying time and date when i upload the sketch and open serial monitor to see it. My problem is when i give 'y' character in serial monitor the setTime() function is executed which is fine because i want to set time and other parameters through serial monitor but WHAT I WANT IS WHEN I DONT PRESS 'Y' I SHOULD GET EARLIER VALUES OF TIME AND DATE FROM RTC AS A NORMAL FUNCTIONING AS SOON AS I OPEN SERIAL MONITOR AFTER UPLOADING THE CODE AND IN BETWEEN IF I GIVE 'Y' KEY IT SHOULD ONLY EXECUTE setTime(). so what should i do?

0
None
mmgm

8 months ago

Best Tuto

Merci

if you get funny settings like "(undescifrable random stuff) 165, 165, 165", there are some reasons:

- SCL and SDA inverted;

- wrong pins connection;

- you try to use another pin for DS's Vcc than 5V;

- your DS chip is fried,

- your 'duino is fried, partly or at all.

my Nano seems to be fried on his I2C port, but other pins are working, on my Mega i get normal responses on I2C port, so...
in a pinch, you can try a "breadboarduino", and see what you get with that...

thanks a lot very good example very useful.........

great . then ,How can I made it display on TM1637 ?

thank you soo much code is very good.

Thank you this is amazing! Very sophisticated code!

Thank you very much. You just made my work easy. Keep uploading such instructables.

Thank you for this code.

I am trying to set a simple RTC DS1307 but when I follow the prompts to set the time etc in the serial port it comes up with the below message...

the data has been entered.

The current date and time is now: . -....B. ... 165, 20165 153:165 PM

Thank you.

Any ideas how to get the actual time or what I am doing wrong? I am very new to arduino.

Cheers

R

1 reply

Works well.

Could a DS3231 be substituted in place of the DS1307?

hi, thanks for the code, but i have one problem with it.

After i type in the y for yes, the code fill automatically the year with a 0

Please enter the current year, 00-99. - 0 (this could not be set)

Please enter the current month, 1-12. -

Any ideas what the problem is ?

Thanks and greats

André