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
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.
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 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 ?
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;
}
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
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.