Arduino RTC ds1307 not triggering

hey,
i have bought a RTC ds1307 module and have set the date and time using the code below:

#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);

void setup()
{
// Setup Serial connection
Serial.begin(115200);
// Uncomment the next line if you are using an Arduino Leonardo
//while (!Serial) {}

// Initialize the rtc object
rtc.begin();

// The following lines can be uncommented to set the date and time
rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014
}

void loop()
{
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");

// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");

// Send time
Serial.println(rtc.getTimeStr());

// Wait one second before repeating :slight_smile:
delay (1000);
}

and i was not able to trigger the 5v relay, it would be helpful if i get the perfect code to trigger the relay in a specific time that has to be set

What is the criteria for activating the relay?
Is it a relay or a relay module? Schematics can be useful.

1. If you are using the following DS1307 RTC Module (Fig-1), then upload the sketch of Step-2 to operate the clock.

ds1307RTC Module
Figure-1:

2. Test Sketch

#include "RTClib.h" 
byte prSec = 0;

RTC_DS1307 rtc;  //RTC_DS3231 rtc; //user data type RTC_DS3231    variable rtc or object
DateTime nowDT;
//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup ()
{
  Serial.begin(115200);
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //takes time and date from Computer
  //rtc.adjust(DateTime(2021, 12, 31, 11, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}

void loop ()
{
  
  showTime();
  timeDelay();//1-sec interval of time display; this time delay comes form DS1307 itself
}

//===================================================
void showTime()
{
  nowDT = rtc.now();  //nowDT hold Date and Time
  //---------------------------
  byte myHour = nowDT.hour();   //myHour holds Hour of time of daya
  Serial.print(myHour); //12:58:57     12:4:56
  Serial.print(':');
  //-------------------------------------------------
  byte myMin = nowDT.minute(); //to show leading zero of minute
  if (myMin < 10)
  {
    Serial.print('0');
  }
  Serial.print(nowDT.minute()); Serial.print(':');
  //--------------------------------------------------
  byte mySec = nowDT.second();
  if (mySec < 10)
  {
    Serial.print('0');
  }
  Serial.println(mySec);//(nowDT.second(), DEC);
}
//---------------------------------------------
void timeDelay()
{
  prSec = bcdSecond();   //current second of RTC
  while (bcdSecond()== prSec)// != 1 )
  {
    ;
  }
  prSec = bcdSecond(); //delay(1000);
}

byte bcdSecond()
{
  nowDT = rtc.now();
  if (nowDT.second() == 0 )
  {
    return 0;
  }
  else
  {
    return nowDT.second();
  }
}

3. At what time of the day, do you want to trigger the relay? At what DPin your realy is connected?

I want the trigger to activated( on at 6.30 morning ,and off at 10.30night) relay pin to digital 4

Does sketch of Post-3 work? If so, set the time in 12- hrs format. Study data sheets or example programs to know how to set alarm ON/OFF times.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.