Having problems with DS1307 RTC

Thread Starter

abraar_sameer

Joined May 29, 2017
37
I brought a DS1307 chip incuding battery and crystal. I made the circuit in a breadboard and wired everything with arduino. It turns out that I can set the time, but the clock does not run at all. I have disconnected Vcc and the chip still holds time, but the clock does not run.

What is the problem?

I want to mention that I did not buy a module, beacuse I could build it myself.

image.jpeg
 

kubeek

Joined Sep 20, 2005
5,795
That´s weird, from reading the datasheet I could not determine whether the chip is supposed to keep counting when on battery power or just retain its memory.
 

Thread Starter

abraar_sameer

Joined May 29, 2017
37
That´s weird, from reading the datasheet I could not determine whether the chip is supposed to keep counting when on battery power or just retain its memory.
That's what I see. It retains its memory but does not count.:(
It doesn't even give out the 1 Hz signal it was supposed to
 

Picbuster

Joined Dec 2, 2013
1,047
I brought a DS1307 chip incuding battery and crystal. I made the circuit in a breadboard and wired everything with arduino. It turns out that I can set the time, but the clock does not run at all. I have disconnected Vcc and the chip still holds time, but the clock does not run.

What is the problem?

I want to mention that I did not buy a module, beacuse I could build it myself.

View attachment 133938
did you start the clock?
from the manual "'
Bit 7 of Register 0 is the clock halt
(CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled. On first
application of power to the device the time and date registers are typically reset to 01/01/00 01 00:00:00
(MM/DD/YY DOW HH:MM:SS). The CH bit in the seconds register will be set to a 1.
The clock can be halted
whenever the timekeeping functions are not required, which minimizes current (I
BATDR
).
'' end manual
picbuster
 

Thread Starter

abraar_sameer

Joined May 29, 2017
37
Post your sketch fie.
I have never used Wire library before so I am using a sketch taken from the internet.

Code:
/*
  RealTimeClockDS1307 - library to control a DS1307 RTC module
  Copyright (c) 2011 David H. Brown. All rights reserved
 
  Much thanks to John Waters and Maurice Ribble for their
  earlier and very helpful work (even if I didn't wind up
  using any of their code):
   - http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock
   - http://www.glacialwanderer.com/hobbyrobotics/?p=12

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include <Wire.h>
#include <RealTimeClockDS1307.h>

//RealTimeClock RTC;//=new RealTimeClock();

#define Display_Clock_Every_N_Seconds 1
#define Display_ShortHelp_Every_N_Seconds 25
//#define TEST_Squarewave
//#define TEST_StopStart
//#define TEST_1224Switch

int count=0;
char formatted[] = "00-00-00 00:00:00x";

void setup() {
//  Wire.begin();
  Serial.begin(9600);
  tone(10, 38768);
}
void loop() {
  if(Serial.available())
  {
    processCommand();
  }
  delay(1000);
  RTC.readClock();
  count++;
  if(count % Display_Clock_Every_N_Seconds == 0){
    Serial.print(count);
    Serial.print(": ");
    RTC.getFormatted(formatted);
    Serial.print(formatted);
    Serial.println();
  }
 
  if(count % Display_ShortHelp_Every_N_Seconds == 0) {
    Serial.println("Send ? for a list of commands.");
  }
#ifdef TEST_Squarewave
if(count%10 == 0)
{
  switch(count/10 % 6)
  {
    case 0:
    Serial.print("Squarewave disabled (low impedance): ");
    RTC.sqwDisable(0);
    Serial.println((int) RTC.readData(7));
    break;
    case 1:
    Serial.print("Squarewave disabled (high impedance): ");
    RTC.sqwDisable(1);
    Serial.println((int) RTC.readData(7));
    break;
    case 2:
    Serial.println("Squarewave enabled at 1 Hz");
    RTC.sqwEnable(RTC.SQW_1Hz);
    break;
    case 3:
    Serial.println("Squarewave enabled at 4.096 kHz");
    RTC.sqwEnable(RTC.SQW_4kHz);
    break;
    case 4:
    Serial.println("Squarewave enabled at 8.192 kHz");
    RTC.sqwEnable(RTC.SQW_8kHz);
    break;
    case 5:
    Serial.println("Squarewave enabled at 32.768 kHz");
    RTC.sqwEnable(RTC.SQW_32kHz);
    break;
    default:
    Serial.println("Squarewave test not defined");
  }//switch
}
#endif

#ifdef TEST_StopStart
if(count%10 == 0)
{
  if(!RTC.isStopped())
  {
    if(RTC.getSeconds() < 45)
    {
      Serial.println("Stopping clock for 10 seconds");
      RTC.stop();
    }//if we have enough time
  } else {
    RTC.setSeconds(RTC.getSeconds()+11);
    RTC.start();
    Serial.println("Adding 11 seconds and restarting clock");
  }
}//if on a multiple of 10 counts
#endif

#ifdef TEST_1224Switch 
  if(count%10 == 0)
  {
    if(count %20 == 0)
    {
      Serial.println("switching to 12-hour time");
      RTC.switchTo12h();
      RTC.setClock();
    }
    else
    {
      Serial.println("switching to 24-hour time");
      RTC.switchTo24h();
      RTC.setClock();
    }
  }
#endif
}

void processCommand() {
  if(!Serial.available()) { return; }
  char command = Serial.read();
  int in,in2;
  switch(command)
  {
    case 'H':
    case 'h':
    in=SerialReadPosInt();
    RTC.setHours(in);
    RTC.setClock();
    Serial.print("Setting hours to ");
    Serial.println(in);
    break;
    case 'I':
    case 'i':
    in=SerialReadPosInt();
    RTC.setMinutes(in);
    RTC.setClock();
    Serial.print("Setting minutes to ");
    Serial.println(in);
    break;
    case 'S':
    case 's':
    in=SerialReadPosInt();
    RTC.setSeconds(in);
    RTC.setClock();
    Serial.print("Setting seconds to ");
    Serial.println(in);
    break;
    case 'Y':
    case 'y':
    in=SerialReadPosInt();
    RTC.setYear(in);
    RTC.setClock();
    Serial.print("Setting year to ");
    Serial.println(in);
    break;
    case 'M':
    case 'm':
    in=SerialReadPosInt();
    RTC.setMonth(in);
    RTC.setClock();
    Serial.print("Setting month to ");
    Serial.println(in);
    break;
    case 'D':
    case 'd':
    in=SerialReadPosInt();
    RTC.setDate(in);
    RTC.setClock();
    Serial.print("Setting date to ");
    Serial.println(in);
    break;
    case 'W':
    Serial.print("Day of week is ");
    Serial.println((int) RTC.getDayOfWeek());
    break;
    case 'w':
    in=SerialReadPosInt();
    RTC.setDayOfWeek(in);
    RTC.setClock();
    Serial.print("Setting day of week to ");
    Serial.println(in);
    break;
   
    case 't':
    case 'T':
    if(RTC.is12hour()) {
      RTC.switchTo24h();
      Serial.println("Switching to 24-hour clock.");
    } else {
      RTC.switchTo12h();
      Serial.println("Switching to 12-hour clock.");
    }
    RTC.setClock();
    break;
   
    case 'A':
    case 'a':
    if(RTC.is12hour()) {
      RTC.setAM();
      RTC.setClock();
      Serial.println("Set AM.");
    } else {
      Serial.println("(Set hours only in 24-hour mode.)");
    }
    break;
   
    case 'P':
    case 'p':
    if(RTC.is12hour()) {
      RTC.setPM();
      RTC.setClock();
      Serial.println("Set PM.");
    } else {
      Serial.println("(Set hours only in 24-hour mode.)");
    }
    break;

    case 'q':
    RTC.sqwEnable(RTC.SQW_1Hz);
    Serial.println("Square wave output set to 1Hz");
    break;
    case 'Q':
    RTC.sqwDisable(0);
    Serial.println("Square wave output disabled (low)");
    break;
   
    case 'z':
    RTC.start();
    Serial.println("Clock oscillator started.");
    break;
    case 'Z':
    RTC.stop();
    Serial.println("Clock oscillator stopped.");
    break;
   
    case '>':
    in=SerialReadPosInt();
    in2=SerialReadPosInt();
    RTC.writeData(in, in2);
    Serial.print("Write to register ");
    Serial.print(in);
    Serial.print(" the value ");
    Serial.println(in2);
    break;   
    case '<':
    in=SerialReadPosInt();
    in2=RTC.readData(in);
    Serial.print("Read from register ");
    Serial.print(in);
    Serial.print(" the value ");
    Serial.println(in2);
    break;

    default:
    Serial.println("Unknown command. Try these:");
    Serial.println(" h## - set Hours       d## - set Date");
    Serial.println(" i## - set mInutes     m## - set Month");
    Serial.println(" s## - set Seconds     y## - set Year");
    Serial.println(" w## - set arbitrary day of Week");
    Serial.println(" t   - toggle 24-hour mode");
    Serial.println(" a   - set AM          p   - set PM");
    Serial.println();
    Serial.println(" z   - start clock     Z   - stop clock");
    Serial.println(" q   - SQW/OUT = 1Hz   Q   - stop SQW/OUT");
    Serial.println();
    Serial.println(" >##,###  - write to register ## the value ###");
    Serial.println(" <##      - read the value in register ##");
   
  }//switch on command
 
}

//read in numeric characters until something else
//or no more data is available on serial.
int SerialReadPosInt() {
  int i = 0;
  boolean done=false;
  while(Serial.available() && !done)
  {
    char c = Serial.read();
    if (c >= '0' && c <='9')
    {
      i = i * 10 + (c-'0');
    }
    else
    {
      done = true;
    }
  }
  return i;
}
 

AlbertHall

Joined Jun 4, 2014
12,346
It seems that the 32kHz oscillator is not running.
First check that it is enabled. From the datasheet:
Please note that the initial power-on state of all registers is not defined. Therefore, it is important
to enable the oscillator (CH bit = 0) during initial configuration.

If that is not the problem make sure you use very short connections between the crystal and the chip.
 
This is not an uncommon problem.

You are using an old sketch/library. Do you understand that when you run that sketch, you need to issue a 'z' command to start the clock (which calls RTC.start() )? It is not clear from your posts that you have explicitly tried that. Lowercase 'z' to start the clock and uppercase 'Z' to stop the clock. I see that you wrote that "I re-enabled the clock.. but no results.", but you did not write how you did that.

I would suggest that you use a simple sketch to test, but before that:
Can you post a clear picture of your circuit with all of the connections, including to A4/A5 on the Arduino?
What crystal did you use?
What is the voltage on your battery?
 

Thread Starter

abraar_sameer

Joined May 29, 2017
37
This is not an uncommon problem.

You are using an old sketch/library. Do you understand that when you run that sketch, you need to issue a 'z' command to start the clock (which calls RTC.start() )? It is not clear from your posts that you have explicitly tried that. Lowercase 'z' to start the clock and uppercase 'Z' to stop the clock. I see that you wrote that "I re-enabled the clock.. but no results.", but you did not write how you did that.
Yes, I have issued the "z" command, but no results.

The battery is of 3V
Crystal: 32.768 KHz, I will test it with a microcontroller in a while.

Posting the pictures in a while..
 

MrAl

Joined Jun 17, 2014
11,466
Hi,

If you cant get it to work somehow then i have to recommend getting the next up generation chip or module.

The better chip/module has a temperature compensated crystal built right in so there is no external crystal. The internal crystal is accurate and temperature compensated so you get very accurate long term timing such as you would want for a wall clock or something like that. The error over one full year is not very big.

All of the external crystal type chips/modules have much bigger errors over the long term like a year. The new type got around that so i have to recommend getting a new type unit.
I am sure someone else here knows the part number for the new type RTC.
 
Nice pics, they help a lot.

Unlike your initial schematic, you have, what looks to be a 220 ohm resistor to an LED to ground with the other side being attached to pin 7.
That is not right. Pin 7 does not work that way. From the data sheet:

/---/
Square Wave/Output Driver. When enabled, the SQWE bit set to 1, the SQW/OUT
pinoutputs one of four square-wave frequencies (1Hz, 4kHz, 8kHz, 32kHz). The SQW/OUT
pin is open drain and requires an external pullup resistor. SQW/OUT operates with either
VCC or V BAT applied. The pullup voltage can be up to 5.5V regardless of the volt age on
VCC. If not used, this pin can be left floating.
/----/

It is open drain and you have it driving. Just remove the LED and resistor and try again and see if doesn't work.

Meanwhile l will look again, but I think you have it otherwise wired ok.
 
That would stop the LED working but it won't bother the chip at all.
The LED and resistor should connect to +5V
Why make the assumption that it would not "bother the chip at all"?

Pin 7 is an oscillator signal and it only makes good sense to not be hanging a pull-down resistor with a diode on that signal - regardless of its appropriate use of sinking current. We are trying to debug an RTC that is not updating RAM. Rather than assuming that's not it, how about if it is removed and then, IF it still does not work, go on to the next step.

In my view the next step,btw, would be to remove the battery and the battery connections and connect Vbat to ground. If the RTC still does not run, it becomes more likely that the chip is blown (although I'd like to know that the crystal works and is appropriate for the RTC).

Let's wait and hear from the OP, eh?
 
Last edited:
Top