RTC Displaying Unknown Values via Serial Monitor

Can you please help me? This RTC Module of mine is displaying unknown values though I believe I have uploaded the correct program to sync the time from the time in the code.


I'm using this code.

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527


void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      45; //0-59
  byte minute =      40; //0-59
  byte hour =        0; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    1; //1-31
  byte month =       3; //1-12
  byte year  =       11; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  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(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}

which is I also got over the internet.

Nick Gammon has an I2C scanner. Have you run that sketch to confirm that your device's address IS 0x68?

The various Wire methods return values. Printing them might be a clue.

I believe those results suggest inadequate power. If you are running off the USB cable, you might try another computer or a 9v wall wart.

Hi!

You chould check the following:

  1. Do you have pullup resistors on the i2c bus?
  2. Bad battery also cause this error.
  3. And also check all wires :slight_smile:

@PaulS

I went to his website before posting this and unfortunately, I find it very complicated and just went away from it.

@Nick_Pyner

I tried using my friend's RTC Module. Wired with the same power and same everything. Their component is working properly, time ticks continuously.

@olof_n

I have this.

Do I really need to place pull-up resistors? Isn't it provided already in the module? Just asking. :slight_smile:

@Topic

I've searched and found one probable reason why the time isn't ticking. They say that the problem is in internal oscillator of the circuit. Is it true?

Also, sorry for the very late reply.

The occurrence of 165 in the output means that the software always reads all-ones from the RTC - i.e. 0xFF. If another RTC works, then the software is OK so you need to check the wiring but also check that the header has been soldered onto the RTC board properly. There might be a bad joint.

Pete