DS1307 returns all 0s

I am running Matt's code but the time on the RTC is not changing. I am using the module from SparkFun with the resistors.

I am seeing all 0s returned when I get the time. I think this implies that I can successfully write data to and read data from the clock but it's jut not ticking.

In other threads, I see references to the CH (ClockHalt) bit which Matt seems to be managing correctly. I also see suggestions that the oscillator is incorrect. Since I bought a ready to use module from SparkFun, that shouldn't be the problem.

Please be gentle. I have been programming for 20 years but this is my first foray into this sort of stuff and I am definitely not an electronics guy.

I am confused. Can somebody help me?

thanks,
Roger

Can we see pictures of your set up and also the code you are using please?

Hi Mike,

I couldn't figure out how to load a picture like this to the forum so I followed your www link. A photo is on the way.

5v comes from the Arduino to the breadboard and is jumpered across the board to the other side.
Ground is handled the same

Analog pin 4 is the green wire. It goes to the board and connects to the yellow wire which goes to SCA on the clock
Analog pin 5 is the blue wire. It goes to the board and connectes to the blue wire which goes to the SCL on the clock.
+5v on the clock module goes to +5 on the breadboard
GND on the clock module goes to GND on the breadboard

I found the code below on the Arduino playground. It came from Combustory.com. I ran it exactly as it came from there and the clock returned all zeros. In the course of understanding it a bit better, I cleaned it up some so the data returned is a bit tighter.

I also ran Matt's code. All Zeroes
I ran several other examples. Zeroes

The Combustory code was the only one that had the routine for setting some memory to 0xff to (as I read it) get the clock out of backup mode. It's completely command driven. The first command is to get the time. Second command writes 0xff to registers 8-35. Third command dumps all registers.

thanks,
Roger

1: 0:0:0 0/0/0

Command: 49
0:0:0 0/0/0: RTC1307 Initialized Memory

Command: 50
0:0:0 0/0/0: RTC 1307 Dump Begin
0: 0 1: 0 2: 0 3: 0 4: 0 5: 0 6: 0 7: 0
8: 0 9: 0 10: 0 11: 0 12: 0 13: 0 14: 0 15: 0
16: 0 17: 0 18: 0 19: 0 20: 0 21: 0 22: 0 23: 0
24: 0 25: 0 26: 0 27: 0 28: 0 29: 0 30: 0 31: 0
32: 0 33: 0 34: 0 35: 0 36: 0 37: 0 38: 0 39: 0
40: 0 41: 0 42: 0 43: 0 44: 0 45: 0 46: 0 47: 0
48: 0 49: 0 50: 0 51: 0 52: 0 53: 0 54: 0 55: 0
56: 0 57: 0 58: 0 59: 0 60: 0 61: 0 62: 0 63: 0
RTC1307 Dump end

/*

  • RTC Control v.01
  • by http://www.combustory.com John Vaughters
  • Credit to:
  • Maurice Ribble - http://www.glacialwanderer.com/hobbyrobotics for RTC DS1307 code
  • With this code you can set the date/time, retreive the date/time and use the extra memory of an RTC DS1307 chip.
  • The program also sets all the extra memory space to 0xff.
  • Serial Communication method with the Arduino that utilizes a leading CHAR for each command described below.
  • Commands:
  • T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) - T Sets the date of the RTC DS1307 Chip.
  • Example to set the time for 02-Feb-09 @ 19:57:11 for the 3 day of the week, use this command - T1157193040209
  • Q(1-2) - (Q1) Memory initialization (Q2) RTC - Memory Dump
    */

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // This is the I2C address

// Global Variables

int command = 0; // This is the command char, in ascii form, sent from the serial port
int i;
long previousMillis = 0; // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
long nIteration = 0;

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

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

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.

void setDateDs1307()
{

second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.
minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
dayOfWeek = (byte) (Serial.read() - 48);
dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());

Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);

}
void vInitializeMemory()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS); // 255 will be the init value and 0 will be cosidered an error that occurs when the RTC is in Battery mode.
Wire.send(0x08); // Set the register pointer to be just past the date/time registers.
for (i = 1; i <= 27; i++)
{
Wire.send(0xff);
delay(100);
}
Wire.endTransmission();
getDateDs1307();
Serial.println(": RTC1307 Initialized Memory");
}
void vDumpMemory()
{
int nRow;
int nCol;
int nByte;

getDateDs1307();
Serial.println(": RTC 1307 Dump Begin");
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 64);

for (nRow = 0; nRow < 8; nRow++)
{
for(nCol = 0; nCol < 8; nCol ++)
{
nByte = nRow * 8 + nCol;
test = Wire.receive();
if(nByte < 10)
Serial.print(" ");

Serial.print(nByte, DEC);
Serial.print(":");
if(test < 10)
Serial.print(" ");
if(test < 100)
Serial.print(" ");
Serial.print(test, DEC);
Serial.print(" ");
}
Serial.println();
}
Serial.println(" RTC1307 Dump end");
}
void nGetDate()
{
nIteration++;
Serial.print(nIteration);
Serial.print(": ");
getDateDs1307();
Serial.println();
}
void vHandleCommand()
{ // Look for char in serial que and process if found

command = Serial.read();
switch(command)
{
case 32: // space = get Date
nGetDate();
break;
case 84: // T = Set date
setDateDs1307();
getDateDs1307();
Serial.println(" ");
break;
case 81: // Q RTC1307 Memory Functions
delay(100);
if (Serial.available())
{
command = Serial.read();
Serial.print("Command: ");
Serial.println(command); // Echo command CHAR in ascii that was sent
switch(command)
{
case 49: //If command = "1" RTC1307 Initialize Memory - All Data will be set to 255 (0xff). Therefore 255 or 0 will be an invalid value.
vInitializeMemory();
break;
case 50: //If command = "2" RTC1307 Memory Dump
vDumpMemory();
break;
}
}
}
Serial.println("------------------------------------");
}

void setup() {
Wire.begin();
Serial.begin(9600);

}

void loop()
{
if (Serial.available())
vHandleCommand();

command = 0; // reset command
delay(1000);
}
//******************************The End

If you haven't connected a backup battery to the DS1307, have you connected it's Vbat (pin 3) to ground ?

Ver

Thanks for the response,

I bought the SparkFun module that has a battery.

Roger

Is the battery good? dead battery behavior == no battery behavior (found this out last week).

-j

Good idea, kg4wsv but no joy

Battery reads 3.16 volts on the multimeter. I don't have a way to test it under load but surely this module doesn't draw much amperage.

Just for fun, I left the battery out and you are right. No battery gives me exactly the same behavior.

So we can say that

Dead battery = no battery = battery

Should I be wondering about the RTC module? Could I have fried it somehow when I was initially hooking it up? At some point, I probably wired it backwards....

Roger

A voltage check is good enough for the battery; current draw from the battery is so small you may actually have trouble measuring it with typical hobbyist equipment (e.g. cheap radio shack meter).

As for damage wiring it backwards, it depends on what got wired backwards. If SDA/SCL were swapped, I seriously doubt it would damage anything. Likewise, Vcc on SDA or SCL shouldn't damage anything, as these are both inputs and connecting Vcc would simply force them high.

If you switched Vcc and GND, though, that's frequently a chip-killer.

-j

This could be good news for me. The money for the new module is not as big a deal to me as finishing the project. I just hate being stuck.

If I did that, would it manifest itself like this? It seems that the chip is communicating with me. It's just not ticking.

Time is truly the final frontier.

Roger

If you're getting all 0s, I wouldn't be too sure the chip is talking to you - sounds more like it isn't talking at all, and the resulting bits are empty. I'll bet if you disconnect the device completely the program will behave the same way.

-j

hmmmm

Remember, I am using the SparkFun module:

I disconnected +5v and got no response at all (program hung)
I disconnected scl and got no response at all (program hung)
I disconnected SDA and the program didn't hang but I got no response

Roger

Remember, I am using the SparkFun module:

Doesn't matter, it's the same device.

I may be wrong about the "all 0s response". Come to think of it, the one I flew on a balloon responded with all zeroes when it got too cold, but started talking sense again once it thawed out.

I'm stumped.

-j

As I was writing my last reply, a wire came off of the module. I soldered it back on and now the thing won't reply at all. I ordered two more from Sparkfun. Grrrr

I've been programming for over 20 years and this sort of thing is always frustrating. I always describe it as , "Doesn't work until it does". there's no halfway here. This thing is not going to work until I find that magic key and then it will work.

For now, I am going with your suggestion that I somehow made the chip crazy by miswiring it.

Roger

I've had the same problem and it turns out I had swapped the SDA Y SCL wires to the Arduino. Strangely enough, if any of these wires is disconnected arduino blocks, but swapping them returns all zeros.

Have you double checked these connections?

Alberto

Thank you for your reply. It turned out that I had crossed the power leads at some point and fried the module. I had to order another module from SparkFun and it all worked just fine.

I have a sample DS1307 that I ordered from Maxim. I built a board with a crystal and battery. Until I got mine connected correctly, it "responded" with all 0s as well. I'm using the same code. Make sure you have your pins connected correctly. You must also have 5V and ground connected to the clock module. Instead of having to use the memory dump, I added the following to my program. This just retrieves the time and date and prints it through the serial monitor. I actually had progressed to saving the time and date for 7 different events into the clock SRAM. I used the following as a learning step to get there. Good luck!

else if(command == 67){ //If command = "C" RTC1307 Get Time and Date
getDateDs1307();
Serial.println(" ");