programming arduino to wait for more than 50 days before action

Hi,

Looking to set up arduino to work inside an electronic safe,

I want to program it to close a switch and reconnect the batteries inside an electronic safe after a predetermined period of time (say 1year).

I.e. so the safe cannot be opened with the keypad until 1year has elapsed.

Is there any way to code a delay of more than 50 days? (50 days seems to be the limit of the delay() function.

Thanks,

Tom

For long time periods you will need a Real Time Clock (RTC) module as the Arduino's timekeeping is not good over long periods.

You could then program the system to do nothing until a certain date is reached.

I presume you will have some fall-back arrangement in case the Arduino does not work as intended?

...R

Use an RTC with an alarm output so you can put the Arduino to sleep for the entire time.

Make sure your batteries will still be alive in a year. :slight_smile:

twingall1:
Is there any way to code a delay of more than 50 days? (50 days seems to be the limit of the delay() function.

Thanks,

Tom

Of course, you simply delay() for a shorter time and do it multiple times by counting. For example, delay one hour, and count hours. But yes, delay() or any other functions will not be running if the batteries are not connected. :slight_smile:

The DS3231 real time clock has an open drain alarm output that could control a P-channel mosfet used to switch battery power on at the set alarm date/time. A DS3231 module has its own lithium coin cell which should last for years. The main battery would need to be 5.5V or less. If it's higher, a two-mosfet circuit would be needed. And of course the main battery should not self-discharge, so some kind of primary battery would be best, not a rechargeable.

But I don't think this is a good idea if there's no other way to open the safe. Things can go wrong that you don't anticipate. And you might change your mind about the delay.

thanks all for responses. Will get a rtc module then.. this project seems like the ticket: https://www.viralsciencecreativity.com/post/arduino-relay-trigger-at-specific-time-ds3231-rtc-module

Fall back arrangement would be the master key to manually open safe, but then this rather undercuts the point. perhaps a backup power supply cable running into the locked safe.

twingall1:
thanks all for responses. Will get a rtc module then.. this project seems like the ticket: https://www.viralsciencecreativity.com/post/arduino-relay-trigger-at-specific-time-ds3231-rtc-module

Fall back arrangement would be the master key to manually open safe, but then this rather undercuts the point. perhaps a backup power supply cable running into the locked safe.

That project leaves the Arduino powered up all the time. The battery might not last very long doing it that way. What I had in mind is more like the first response to this:

But I'm confused about what you really need. Is the only purpose of the Arduino to turn on the relay? If so, you could just have the DS3231 turn on a mosfet, which in turn would turn on a relay, and you wouldn't need an Arduino at all. The mosfet might even take the place of the relay. Or is the Arduino involved in the keypad stuff and unlocking calculations? What kind of battery does the safe itself have?

ShermanP:
That project leaves the Arduino powered up all the time. The battery might not last very long doing it that way. What I had in mind is more like the first response to this:

power - Turn Arduino on with timer every 24h - Arduino Stack Exchange

But I'm confused about what you really need. Is the only purpose of the Arduino to turn on the relay? If so, you could just have the DS3231 turn on a mosfet, which in turn would turn on a relay, and you wouldn't need an Arduino at all. The mosfet might even take the place of the relay. Or is the Arduino involved in the keypad stuff and unlocking calculations? What kind of battery does the safe itself have?

Thanks, i was just about to ask if i needed an arduino.. Yes, only want to turn on the relay. At a certain date. I have yet to buy the safe, but they seem to take 4*1.5v AA batteries, in series so 6v.. so i guess you would need mosfet then relay? thanks will check that out..

I don’t think you need the relay. You just need the DS3231, a small breadboard, headers, a diode, a resistor and a P-channel mosfet. I have to head out now, but will post a drawing later this afternoon.

Relays take lots of power to keep them on, and you have to be concerned about not only the one year wait, but what happens after that. The mosfet will handle amps of current using very little power to keep it on.

ShermanP:
I don't think you need the relay. You just need the DS3231, a small breadboard, headers, a diode, a resistor and a P-channel mosfet. I have to head out now, but will post a drawing later this afternoon.

Relays take lots of power to keep them on, and you have to be concerned about not only the one year wait, but what happens after that. The mosfet will handle amps of current using very little power to keep it on.

Awesome, thank you that would be a great help.

The DS3231 real time clock has an open drain alarm output that could control a P-channel mosfet used to switch battery power on at the set alarm date/time.

If so, you could just have the DS3231 turn on a mosfet, which in turn would turn on a relay, and you wouldn't need an Arduino at all.

The day/date alarm configuration on the DS3231 will not allow for a setting more than a month out.

I believe an Arduino will be required to count the alarm triggers, and to clear the alarm flag/interrupt so another alarm can occur.

cattledog:
The day/date alarm configuration on the DS3231 will not allow for a setting more than a month out.

I believe an Arduino will be required to count the alarm triggers, and to clear the alarm flag/interrupt so another alarm can occur.

I had forgotten that, but that's right. So my idea won't work unless there's another RTC that lets you set the alarm month.

Well, you know:

void delayDays(unsigned int n) {
   for (byte i=0; i <n; i+) {
       delay(86400UL * 1000);  // delay for 1 day.
   }
}

Or just monitor the day/date in the Arduino code and get your alarm that way.

Or, use an ordinary combination code safe.

Have a program running on some computer that waits until the target date, then displays or sends an email with the safe combination.

(deleted)

schedule email send.PNG

jremington:
Or, use an ordinary combination code safe.

Have a program running on some computer that waits until the target date, then displays or sends an email with the safe combination.

Thanks that's a thought, maybe the way to go. Though there is something appealing about it all being contained within a physical safe.
Just found this which explains time-lock encryption nicely:

..surprisingly not any solutions i could find for time-lock encryption without 3rd party.. guess its a more difficult problem than it first seems, thought there was something involving blockchain but cannot find.

So going back to Arduino with rtc in the safe, switch on the solenoid cable. Will post if i have success.

Many thanks for all the help

Well below is a circuit that might work. It switches the power to the safe electronics via a P-channel mosfet. The mosfet is turned on when the DS3231 alarm triggers and /INT goes low. That happens once a month, and the Pro Mini also turns on.

The Mini first checks its EEPROM to see if 12 months has elapsed. If not, it writes to the next EEPROM byte or word to record how much time has elapsed. Then it brings D2 and D3 high, which keeps the power on and provides Vcc power to the DS3231. Then it clears the alarm and sets up a new one for a month later, all using the I2C lines. When all of that is done and /INT has gone back high, it turns off D3 to return the RTC to coin cell power, then turns off D2 which turns off its own power and the safe electronics. This setup does enable the safe every month, for only for a second or two.

If the EEPROM data says 12 months have now elapsed, the Mini does nothing. It just goes into Power-Down sleep, which should reduce its current draw to the regulator's no-load idle current. The DS3231 alarm will not be cleared, and it will continue to run off the coin battery. Power will stay on indefinitely.

To start a new time lock, the EEPROM would all have to be re-initialized, and the DS3231 would have to be programmed with new alarm settings.

I should have included a manual switch that bypasses all this stuff and just turns on the power to the safe electronics. That's something you would want to have.

Safe Time Lock.jpg

I should say that there is another RTC chip, the M41T81, which does let you include the year in the alarm settings. But I've found only one module using it, and it's over $10 plus shipping. You could do your own, but you'd need a coin cell holder and a crystal and some board to put it on. If you went that way, the Pro Mini and NPN transistor wouldn't be needed, at least not inside the safe. You would still need a way to set up a new time lock.

Safe Time Lock.jpg

Thanks ShermanP, comprehensive! A bit daunting for me at my skill level, but have just ordered some of those NDP6020P P-channel mosfets and will have a play around with them first to see if i can replace my relay switch with one.
This looks like the proper solution, but if i struggle i may just settle with power cable for arduino coming out back of safe. Then plug in power as date of opening approaches, causing the uno to run:

/* getting relay module to work*/ 

#include <virtuabotixRTC.h> //Library used
virtuabotixRTC myRTC(6, 7, 8);

int relay = 4;

void setup() {
   Serial.begin(9600);
   pinMode(relay, OUTPUT);   
}

void loop() {
 myRTC.updateTime();
 Serial.print("Current Date / Time: ");
 Serial.print(myRTC.dayofmonth); 
 Serial.print("/");
 Serial.print(myRTC.month);
 Serial.print("/");
 Serial.print(myRTC.year);
 Serial.print(" ");
 Serial.print(myRTC.hours);
 Serial.print(":");
 Serial.print(myRTC.minutes);
 Serial.print(":");
 Serial.println(myRTC.seconds);
  delay(3000);          

if ((myRTC.month ==8) && (myRTC.hours ==22)&&((myRTC.minutes == 35 ) || (myRTC.minutes ==36))){   //set the specific month/day/hour/minute you want as trigger
  digitalWrite(relay, HIGH);
  Serial.println("Relay ON");
  }
  else {
  digitalWrite(relay, LOW);
  }
  }