RTC DS 1307

Hello Arduino Community,

First post!

I am trying to make a digital alarm clock for a school project however running into problems

I set up the RTC and used the below code to set the time etc in the serial monitor however after following all the prompts it gives me a date and time of 165, 20165 153:165PM ?? any idea what I’m doing wrong it might be really obvious but I"ve only been using Arduino for a couple of weeks any help would be amazing…

// 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() {
delay(2000); // This delay allows the MCU to read the current date and time.

Serial.print("The current date and time is: ");
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.print("The current date and time is now: ");

Serial.println(“Thank you.”);

// Continuous function for converting bytes to decimals and vice versa
void loop() {
byte decToBcd(byte val) {
return ((val/1016) + (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.print("Please enter the current month, 1-12. - ");
month = readByte();
Serial.print("Please enter the current day of the month, 1-31. - ");
monthday = readByte();
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.print("Please enter the current hour in 24hr format, 0-23. - ");
hour = readByte();
Serial.print("Please enter the current minute, 0-59. - ");
minute = readByte();
second = 0;
Serial.println(“The data has been entered.”);

// The following codes transmits the data to the RTC
// 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’);
incomingByte = Serial.read();
return reading;

void printTime() {
char buffer[3];
const char* AMPM = 0;
Serial.print(" “);
Serial.print(” “);
Serial.print(”, 20");
Serial.print(" “);
if (hour > 12) {
hour -= 12;
AMPM = " PM”;
else AMPM = " AM";
sprintf(buffer, “%02d”, minute);

void readTime() {
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());

moderator: removed your cross-post; please follow posting guidelines.

Welcome to the forum. You will get more help if you post your code using the code tags icon </> at the top left of the tool bar. Please read the following “How to use this forum” and especially point 7 on code tags.

The first troubleshooting to be done on your RTC problem is to confirm that the the device can be seen on the I2C bus. Please run the following code and see if it finds your device.

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

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

  // Leonardo: wait for serial port to connect
  while (!Serial) 

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  for (byte i = 8; i < 120; i++)
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

Is the device found, not found, or does the sketch hang and not report “Found x devices”?

What arduino are you using and what pins is the device connected to?

If the sketch hangs up, disconnect the device and run the sketch with nothing connect to the i2c pins of the arduino and see what is reported.

Your code itself has a few problems with the year, but that will be trivial to fix once you actually get the device connected.

if (Serial.read() == 'y' || Serial.read() == 'Y')

This is dangerous. Those are two separate Serial.reads. IF the user enters 'Y' then that 'Y' will be read by the first read and found not to match 'y' and then the second read will find whatever is next in the buffer or -1 if nothing is there and that won't match 'Y'.

Read the serial once and put it in a variable and use that in your comparison.

char c = Serial.read()
if(c == 'y' || c== 'Y')

That way both comparisons are looking at the same read from Serial.

What is Serial.flush doing in your readByte routine? I gather that you may be confused about what flush does.

There's another issue with readByte.

Let's say the user enters two digits. The first one arrives so Serial.available returns 1 and you leave that while loop and read the byte. But before the second one has a chance to arrive you read again. This read will return -1. That's not \n or a digit. What should happen then.

There's also the semicolon behind else in that function which effectively terminates that block, so for every read you are reading again at the end of the function and basically throwing that away.

It looks as though the OP is having a problem running a sketch s/he didn’t write. (Look at the comments at the top of the code in his/her first post.)

@RichT_Biscuits: Please run this and see what it shows you.
If it gives you a date and time (even a completely garbage date and time), the next step will be to set the date and time.
If it tells you that it couldn’t read the time, then you probably have the RTC miswired. Be careful how you wire things up: electronics are easy to fry, and you might have already fried it. Another possibility is that your RTC was defective to begin with (defective RTCs, and defective hardware in general, are by no means uncommon).

#include <Wire.h>

byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;
void setup()
  // clear /EOSC bit
  // Sometimes necessary to ensure that the clock
  // keeps running on just battery power. Once set,
  // it shouldn't need to be reset but it's a good
  // idea to make sure.
//  Wire.beginTransmission(0x68); // address DS3231
//  Wire.write(0x0E); // select register
//  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
//  Wire.endTransmission();
void loop()
  // ask RTC for the time
  // send request to receive data starting at register 0
  Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
  Wire.write((byte)0); // start at register 0
  Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
  // check for a reply from the RTC, and use it if we can
  if (Wire.available() >= 7) { 
    // if we're here, we got a reply and it is long enough
    // so now we read the time
    ss = bcd2bin(Wire.read()); // get seconds
    mi = bcd2bin(Wire.read()); // get minutes
    hh = bcd2bin(Wire.read()); // get hours
    wd = bcd2bin(Wire.read());
    dd = bcd2bin(Wire.read());
    mo = bcd2bin(Wire.read());
    yy = bcd2bin(Wire.read());
    // show that we successfully got the time
    Serial.print("Got the time: ");
  else {
    // if we're here, that means we were unable to read the time
    Serial.println("Unable to read time from RTC"); 

byte bcd2bin(byte x) {
  // converts from binary-coded decimal to a "regular" binary number
  return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;

void printTime() {
  // just like it says on the tin
  Serial.print ("\'");
  if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
  if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
  if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print("(");
  switch (wd) {
    case 1: Serial.print("Mon"); break;
    case 2: Serial.print("Tue"); break; 
    case 3: Serial.print("Wed"); break; 
    case 4: Serial.print("Thu"); break; 
    case 5: Serial.print("Fri"); break; 
    case 6: Serial.print("Sat"); break; 
    case 7: Serial.print("Sun"); break;
    default: Serial.print("Bad");  
  Serial.print(") ");
  if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
  if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
  if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");

Thank you all very much for your replies...

In the end I had to seek help from my teacher haha

I hope to post more in future to this great community

Thank you all again

Also apologies for not using the forum correctly


Moderator: no need to apologies, just do it right next time :slight_smile: