How do I set a 48 hour relay trigger?

The aim for this project is to get an Ardiuno Uno to a trigger a relay at a specific time once every 48 hours.
I am also using an RTC DS3231 clock to keep the timing accurate.

I have borrowed some code and libraries that are working well…
However the relay is currently set to turn on once at 8.30am and loops every 24 hours.

See the attached code below.

Any ideas how I can make it turn on once at 8.30am every 48 hours instead?

Or if need be: trigger the relay once on a Monday, Wednesday & Friday.

Here is the time trigger relay code as it is currently:

#include <DS3231.h>

int Relay = 4;

DS3231 rtc(SDA, SCL);
Time t;

const int OnHour = 22; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 33;
const int OffHour = 22; //SET TIME TO OFF RELAY
const int OffMin = 34;

void setup() {
Serial.begin(115200);
rtc.begin();
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}

void loop() {
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s) , “);
Serial.print(t.min);
Serial.print(” minute(s)");
Serial.println(" ");
delay (1000);

if(t.hour == OnHour && t.min ==OnMin){
digitalWrite(Relay,LOW);
Serial.println(“Light OFF”);
}
}

Any advice would be greatly appreciated.

Kind Regards

Rick

Look in the \Examples folder of your DS3231 library for examples of how to set the DS3231 Alarm feature.

Then the DS3231 SQW\INT pin will go low at the specified Alarm time.

If you want to trigger every 48 hrs , “ count” how many 24hr events occur. Set a variable =1 when 24 hrs is up. Check ( IF) the status of that variable when the 24 hrs occurs ; if it is already 1, then fire the relay and set the variable back to 0. If it’s zero then set it to one ( and don’t fire the relay )

For Mon/Wed/Fri alarm: set it to every 24 hrs, check whether it's the correct dow, if so continue.

You can make pretty complex alarms this way. Store them in EEPROM if you want to be able to survive your settings through a power outage.

#include <DS3231.h>

const byte RelayPin = 4;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 22; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 33;
// const int OffHour = 22; //SET TIME TO OFF RELAY
// const int OffMin = 34;

void setup()
{
  Serial.begin(115200);
  rtc.begin();
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, LOW);
}

void loop()
{
  static int lastMinute = 0;
  static int dayCounter = 0;

  t = rtc.getTime();

  // Once per minute...
  if (t.min != lastMinute)
  {
    lastMinute = t.min;
    
    // ...Display the time...
    Serial.print(t.hour);
    Serial.print(" hour(s) , ");
    Serial.print(t.min);
    Serial.println(" minute(s) ");

    // ...And check for ON time...
    if (t.hour == OnHour && t.min == OnMin)
    {
      // ...But only act every 2nd day
      dayCounter++;
      if (dayCounter == 2)
      {
        dayCounter = 0;
        digitalWrite(RelayPin, LOW);
        Serial.println("Light OFF");
        delay(60000UL); // Keep the light OFF (?) for one minute
        digitalWrite(RelayPin, HIGH);
        Serial.println("Light ON");
      }
    }
  }
}

Hey everyone. Sorry for the delay in responding.
A huge thank you for all your responses.

My 48hr trigger is now up and running.

Your sketch is appreciated johnwasser. Its been a great help.

Looking forward to my next Arduino project.

Kind regards
Thanks again

Rick

Hi Everyone

I know this is an old thread so bit of a long shot but anyone know what library this is using for the DS3231?
I have been tring to do something similar but have been strugeling with libraries.
tbh even a pointer to a different DS3231 library that would be good for this would be helpful.

I am trying to trigger a relay once a day for 2 mins and a different relay every other day for 2 mins, I have been trying to use the alarm examples in a different library but keep getting stuck and the example above makes alot more sense to me.

Im quite new to this and have only done a few simpler things in the past with arduino so any help would be greatly apreiciated.

Thanks

Kai

kaiken:
anyone know what library this is using for the DS3231?

It looks like it's using the one named "DS3231" from Library Manager.

That was my first thought but it doesnt seem to work with that or im completly missing somrthing, its my first time using any libraries so its probably me.

kaiken:
That was my first thought but it doesnt seem to work with that or im completly missing somrthing, its my first time using any libraries so its probably me.

Perhaps if you could be a bit more specific than "doesn't seem to work" we could offer suggestions.

I have attached an image of what comes up, I done a few small projects before but nothing with a library so this completly new to me.

I am trying to trigger a relay once a day for 2 mins and a different relay every other day for 2 mins, I have been trying to use the alarm examples in a different library but keep getting stuck and the example above makes alot more sense to me.

Unless your heart is set on making your own, I've been working with a relay I inquired about in this forum that may do what you're trying to do depending on voltage and there are others that will work A/C or DC depending on how you wire them.

They can be had for the best price on Aliexpress if you don't mind the wait, and they do standalone.

Not all are programmable to with in a tenth of a second depending on your application some start with a seconds.

That sounds very interesting and like it could do what I need. could you send me a link please?

I found it under a couple of model numbers. The one I have is this one.

https://www.google.com/search?client=firefox-b-1-d&q=hcw-m135+wholesale

The other is this one I just ordered some of.

Visually, there only seems to be 4 resistors near the led on one and 2 on the other.

My experience is it would not drive a load under 10ish volts....I'm thinking the Mosfet's need at least so much before they work.

This last one comes in AC or DC...and a case to boot! so you don't electrify yo self! ;D

BTW, because the instructions can be a bit confusing, how I would use it is depending on if you need to trigger the relay, or just have have it close on its own, use the infinite cycle mode to come on 2 minutes every 48 hours infinitely. I'm not sure if they make them with multiple channels or you may just use one for every action with independent times.

Wanna get fancy?, try this one (4 channels) and let us know if it has the timer feature listed in the title, but WI-fi connection you can control from your phone is pretty cool for the price. Just didn't see the timer feature listed anywhere other than the title.

https://www.aliexpress.com/af/wifi-relay-timer-ac-dc.html?d=y&origin=n&SearchText=wifi+relay+timer+ac+dc&catId=0&initiative_id=SB_20200430103944