Problem with ds1307 rtc

Hi...This is my first time on this platform so pls correct me if I make any mistake...
I want to make an alarm clock with an arduino uno and a ds1307 rtc module. I am using a piezzo buzzer for the sound. I don't have a display yet so i'm skipping that and just making it such that at a specific time, the buzzer will activate. I have just modified the example "read time" from the ds1307 library. In the serial monitor, when i activate it, it works for a few seconds and then stops working.
Here is an attachment of the serial monitor.


My code is such:-
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

void setup() {
pinMode(8,OUTPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
}

void loop() {
tmElements_t tm;

if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
digitalWrite(13,HIGH);
}
delay(9000);
}
delay(1000);
if (tm.Hour==14&&tm.Minute==53&&tm.Second==40){
digitalWrite(8,HIGH);
Serial.println("HIGH");
}
}

void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
My connections are the scl and sda pins of ds1307 connected to scl and sda pin of arduino.
Buzzer positive pin to pin 8 on arduino
a led on pin 13 to detect if there is problem with circuit

Please use the beginners guide on how to post

The most important pins are not SDA and SCL, but 5V and GND.
If your I2C bus is bad, then that could halt the sketch.

Can you pls elaborate cause I am new to this and I dont know about i2c buses

Whenever I close and reopen the serial monitor or restart the arduino it again works for a few seconds before not working...so I don't think that there is any problem with 5v and gnd

There are many ways for a bad I2C bus. For example SDA and SCL next to each other in a flat ribbon cable, no pullup resistors, wrong voltage levels, missing GND wire, and so on.

Could you give a link to the DS1307 and buzzer module that you have and tell us how it is wired ? A photo will help. Perhaps you have everything on a breadboard and the buzzer disturbs the I2C bus. Anything is possible.

The Arduino IDE has a "auto format" feature. Press Ctrl+T in the Arduino IDE.
This forum has code tags to show a sketch. You can put your sketch between lines with three backslash-single-quotes or use the </> button.

```
Your sketch
```

The Wire library for the Uno board has a timeout to prevent that the sketch halts. That is turned off by default at the moment. The timeout is not documented yet, it is in the source code, but first you have to fix the problem.

You might want to read the forum introduction: How to get the best out of this forum

ok thank you so much for your help...I will post the wire diagram and a photo of it shortly

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

void setup() {
pinMode(8,OUTPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
}

void loop() {
tmElements_t tm;

if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
digitalWrite(13,HIGH);
}
delay(9000);
}
delay(1000);
if (tm.Hour==14&&tm.Minute==53&&tm.Second==40){
digitalWrite(8,HIGH);
Serial.println("HIGH");
}
}

void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}

So this is how to paste a code in a reply?

the above is the circuit diagram

This is the circuit...I know it is a little messy so I have also sent the circuit diagram

This is the link for the ds1307 rtc module

and for the piezo buzzer, i ha bought it from a store so I don't have the exact link but this one is pretty much same

A led needs a resistor. Add a resistor in series of 220Ω to 1kΩ to the led at pin 13. Or remove that led and use the (very small) led on the Arduino board. It is the led with the label "L".

Can you check all the four wires between the Arduino board and the RTC module ? Do they make good contact ? Are they not broken ?

The schematic of the RTC module is available on its webpage: https://www.mytechnocare.com/product/ds1307-rtc-module/

The buzzer is confusing. In the photo it seems just a piezo element, but on the website it is a buzzer that makes sound when a DC voltage is applied. Can you remove the buzzer and first try to make it work without the buzzer ?

Have you pressed Ctrl+T in the Arduino IDE ?

first- Ok I will add the resistor
second- i will check the wires and let you know tomorrow...now it is night
third- Ok I will try without the buzzer once
fourth- I don't remember if I had done that so here it is once again...this time with auto format.

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse = false;
  bool config = false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

By the way thnks a lot for taking out your precious time to help me out in my problem.

Im so sorry...in posr no.13 I had given the wrong code...this is the correct auto formatted code

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

void setup() {
  pinMode(8, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop() {
  tmElements_t tm;

  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
      digitalWrite(13, HIGH);
    }
    delay(9000);
  }
  delay(1000);
  if (tm.Hour == 14 && tm.Minute == 53 && tm.Second == 40) {
    digitalWrite(8, HIGH);
    // Serial.println("HIGH");
  }
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

Is the USB cable very cheap ? Try another USB cable. Do you have a multimeter ? Then you can measure the voltage of the 5V pin.

Perhaps the Arduino board or the module is damaged.

Does the sketch always stop at the same moment ?

And if you remove buzzer & led, and try an example from the Arduino IDE,
does it work ?

Hi...I did everything u all suggested. First I removed the led and the buzzer. Then I checked all the 4 wires. One of them was acting strangely....sometimes it was showing "1" on my multimeter which it shows when there is no connectivity and sometimes my buzzer was beeping, indicating that there was connectivity.

Then I took a new wire and checked it... this one was fine.

So then I opened the serial monitor and again the same results.
Though the weird thing is that sometimes it stops after 1 second sometimes after 2 and sometimes after 10.

When I shake it, nothing happens in the serial monitor indicating that the wires all connected properly

image

I don't think that there is any problem with the wires or their connections as I removed the wire on purpose once and it showed that there was some error in the circuit unlike before.

Also I checked the ouptut 5 volt from the arduino using my multimeter and it was exact 5 v

Also I have question...I want to solder the resistor to the led so I dont have to add any breadboard...so can I solder it to the ground of my led?