Desiging a Programmable RTC Power Cutoff Board - Page 1
Forum section: electronics / projects

author: waymond91
author_rank: Regular Contributor
timestamp: June 29, 2023, 08:27:08 am
content: Hello!Im going to be deploying some raspberry pi's and clones in a remote location and they will be battery powered. I dont need them running between sunset and sunrise.In effort to squeeze a few more days of service out of them, I want to make a power controller board that can be programmed to cut-power, and most importantly, re-apply power at a scheduled interval.To that I end, Im working on integrating a DS3231 into a board with a switching regulator. The DS3231 is already supported by the linux kernel on the pi, and its very easy to configure its alarm and interrupt pin it seems to me.The interrupt pin is active low, so it the interrupt pin will be idling high while the power is off. Then when its scheduled wake-up time is reached, the pin goes low.There are a few ways to handle this, the easiest would probably be a low-side n-channel mosfet to switch the device off at ground.Many switching regulators include an enable pin, which is active high (the opposite of what were outputting).There are also some high-side switch options (for example, the MIC2025-2YM seems to be a high side switch with active-low enable pin).Do you have some opinion about the best way to proceed? Or any reason to suspect this system cannot work as expected?Thank you so much for your time!!Schematic attached!

author: jwet
author_rank: Frequent Contributor
timestamp: June 29, 2023, 11:59:07 am
content: For lowest power, I would control the shutdown pin of a switching regulator. Assuming, that you are running on 5v and bucking down something like 12v, this would all be pretty straightforward. If you use the high side switches, you'd have the quiescent of the swticher in the circuit before the swtich. This is kind of redundant. It has the benefit though of not rolling your own switcher. Some switchers go into a very low power pulse skipping mode at very light loads and draw little power. One swtcher that I've found good in this respect is the MP1584- you will find it on many Chinese power modules. It has a shutdown pin that you can access by removing a pull up R, etc on these boards. I think it draws something like 30 uA with no load.Another gotcha is to watch out for "edge triggered" kind of schemes where you generate an alarm from the RTC for on and off. Its common for circuits like this to get into funny states where there not really on or off- they get stuck in some indeterminate state and since they're edge triggered, can't get out. Consider a watchdog or a secondary alarm that forces power on if it times out. The other issue that I can imagine is waiting out the boot time of the RPi.Over the last 20 or so years, I have a built a lot of audio players and video triggers for different kinds of birds, some are dawn activated (Purple Martins) and some are sunset activated (Chimney Swifts). These were all pretty low cost affairs- the Audubon society and Ornithology Departments aren't exactly gushing with cash.I've found photo cells, etc terrible for night and day determination. My boxes calculate Sunrise, etc with celestial calcs. Two good references are "Almanac for Computers- US Naval Observatory" and some very well explained code from edwilliams.org. Its about 30 lines of code with a fair amount of trig and jargon you won't understand probably but gives Sunsets and Sundowns accurate to less than a minute, given your lat and long and a date.Over the years, I have a built a lot of audio players for different kinds of birds, some are dawn activated (Purple Martins) and some are sunset activated (Chimney Swifts).You can PM me if you'd like. Good luck.

author: waymond91
author_rank: Regular Contributor
timestamp: June 29, 2023, 04:39:33 pm
content: I sent you a PM, let me know if you have any issues! Id love to talk further!

author: Peabody
author_rank: Super Contributor
timestamp: June 29, 2023, 09:13:29 pm
content: I can't tell what your input voltage is. If it's 5.5V or less, the simplest switch is just a P-channel mosfet with its gate connected to the INT/SQW pin of the DS3231. That pin will be tristate when off, or low when an alarm is triggered. So a pullup resistor on the gate will keep the mosfet turned off except during an alarm. When the alarm takes place, INT/SQW will stay low until your software clears the alarm flag in the DS3231 status register.So the alarm would take INT/SQW low, which turns on the mosfet, and your processor powers up and does its thing. Then it sets the next alarm time, and finally clears the alarm flag, and the power shuts down. The only thing drawing any current when the mosfet is off is the RTC powered by its coin cell.I'll post a schematic of how I do this. If the input voltage is above 5.5V, it gets a little more complicated. Also note that if you use the typical ZS-042 RTC module, there are some changes that need to be made to the module.

author: waymond91
author_rank: Regular Contributor
timestamp: June 30, 2023, 06:01:39 pm
content: Thank you so much @Peabody.Unfortunately, were looking at a 12v input.Does that change the calculus quite a bit?I think identifying that the pin is high-impedance and not low is pretty important for switching mosfets!!!

author: Peabody
author_rank: Super Contributor
timestamp: July 01, 2023, 02:41:00 pm
content: Below is a circuit for higher voltage. It just adds a logic level N-channel mosfet in the middle to protect the DS3231 from the 12V. Normally that would invert the logic, but we can't do that because the INT/SQW pin goes low when the alarm triggers, and that can't be changed. So the N-channel is oriented to preserve the original logic. Its gate is connected to the RTC's 3V coin cell, and its source to the INT/SQW pin. So when INT/SQW goes low, Vgs is increased and the mosfet turns on. Otherwise it stays off. No gate/source resistor is needed since INT/SQW is tristate unless it is low, so there's no current drain on the coin cell when INT/SQW is low.You have to be sure there is no pullup resistor on INT/SQW on whatever module you are using (if you're using an RTC module).

author: TheMG
author_rank: Frequent Contributor
timestamp: July 01, 2023, 02:55:40 pm
content: Have you thought of simply installing a solar panel to keep the batteries charged up? Then there will be no need to shut off the Pi to conserve power as the batteries should receive a fresh charge every day.

author: waymond91
author_rank: Regular Contributor
timestamp: September 09, 2023, 07:45:14 am
content: Ok I have some time to work on this some more - there are a few issues.1) When the board is powered on for the first time or if the rtc loses power, it defaults to a 1hz square wave. Not a big deal, but it means the board _could_ get into a wierd state if we are not careful.2) When programming the next timer interrupt for the ds3231, it is necessary to clear the alarm flag in the ds3231. This also changes state in the interrupt, which effectively turns off the main controller before the new wake time can be set.My new proposed circuit is attached, but Im worrying its getting a bit too complicated. But it does a few things that I think are good:1) Output of the DS32341 interrupt pin has a pull-up and then passes through a not gate, so when the alarm fires, the line goes high.2) From here, the interrupt goes into a makeshift SR latch. This way, when we first power on the device or if we lose power our switching regulator wont be driven on and off at 1 hz. The "R" pin on the SR is connected to the raspberry pi so it can cleared..3) Lastly, the signal passes through an "OR" gate, so that the raspberry pi can hold the enable while while clearing the interrupt flag in the microntroller without accidentally shutting itself off.I dont do a lot of digital logic, so this is a little risky for me. I worry that the SR Latch wont work as expected since its just two "NOR" gates, and may get into an indeterminant state. Furthermore, it woud be nice to add a delay to the Pi's "power hold" line so that the pi can power off cleanly.What do you guys think?SCHEMATIC ATTACHED

author: Peabody
author_rank: Super Contributor
timestamp: September 09, 2023, 02:49:44 pm
content: Quote from: waymond91 on September 09, 2023, 07:45:14 amYes. The only time the RTC should reset to factory default settings is when the backup coin cell dies, or is replaced when there is no power on Vcc. If that happens, you have to set it back to YOUR defaults. I don't see any way around that, but a CR2032 backup coin cell should last a long time.Edit: But if the INT/SQW pin is controlling power, then when you are setting this up you have to set the RTC so that it is triggering an alarm at some point to get everything started. Or if it's generating 1Hz squarewave on that pin, that would work too. Or you could have a push button that grounds the mosfet gate to temporarily turn on the power, and then your processor could set everything up for the next alarm.QuoteNo, that's not true. It is not necessary to clear the alarm flag to set a new alarm time. The library you use may choose to do that automatically, but it doesn't have to. When the alarm triggers, INT/SQW goes low and the alarm flag is set. The processor then does whatever it has to do, then programs the new alarm time, and then as its final act it clears the alarm flag, which shuts off power. So I really don't think you need the circuit complications. But if your library or your code automatically clears the alarm flag before setting the new alarm time, you will have to modify it so it doesn't do that.

author: waymond91
author_rank: Regular Contributor
timestamp: September 09, 2023, 04:24:18 pm
content: QuoteOk well that would be wonderful - I was using Adafruits RTCLib when doing this test, found here:https://github.com/adafruit/RTClibTo be clear, it wasnt that I physically could not execute the commands to set the next timer without clearing the alarm.It was just that if I did not clear the alarm flag first, the next alarms behavior was inconcistent.I had a simple sketch going that would trigger an alarm every 10 seconds. After the alarm was triggered, it would wait 5 seconds, then it would clear the alarm and set the next one for 10 seconds later.EDIT: After re-trying the code, it seems that it _does_ in fact work:

author: waymond91
author_rank: Regular Contributor
timestamp: September 09, 2023, 11:03:29 pm
content: For posterity, this code did behave for me.Code:#include <Arduino.h>#include <Wire.h>#include <SPI.h>#include <RTClib.h>RTC_DS3231 rtc;void setup() {Wire.begin();Serial.begin(9600);if (! rtc.begin()) {Serial.println("Couldn't find RTC");while (1);}if (rtc.lostPower()) {Serial.println("RTC lost power, lets set the time!");rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));}DateTime now = rtc.now();DateTime alarmTime = now + TimeSpan(10); // 10 seconds from nowrtc.setAlarm1(alarmTime,DS3231_A1_Second);}void printTime(){DateTime now = rtc.now();Serial.print(now.year(), DEC);Serial.print('/');Serial.print(now.month(), DEC);Serial.print('/');Serial.print(now.day(), DEC);Serial.print(" (");Serial.print(now.dayOfTheWeek());Serial.print(") ");Serial.print(now.hour(), DEC);Serial.print(':');Serial.print(now.minute(), DEC);Serial.print(':');Serial.print(now.second(), DEC);Serial.println();}void loop(){if (rtc.alarmFired(1)) {Serial.println("Power on.");for(int i = 0; i<5; i++){printTime();delay(1000);}DateTime now = rtc.now();DateTime alarmTime = now + TimeSpan(10); // 10 seconds from nowrtc.setAlarm1(alarmTime,DS3231_A1_Second);rtc.clearAlarm(1);Serial.println("Power off.");}printTime();delay(1000);}The only problem will be getting a delay in there so the pi can properly shutdown after clearing the flag.

author: langwadt
author_rank: Super Contributor
timestamp: September 09, 2023, 11:30:45 pm
content: I've done something similar but using an MCU with a buildin RTC instead, gives more options like handling gpio-poweroff and gpio-shutdownand safely powering down if the battery gets low etc.to simply make it work with linux I just made the MCU an I2C slave emulating few things needed of one of the standard RTCs linux already have drivers for

author: Peabody
author_rank: Super Contributor
timestamp: September 10, 2023, 02:49:32 am
content: The Adafruit library appears to be very good about leaving the alarm flag alone unless you specifically tell it to do otherwise. I've looked at other libraries, and some automatically clear the flag before setting the new alarm time, which is not good, and not necessary.As for shutdown, you need to do all your printing and anything else in the way of shutdown, make sure all of that has completed, and then clear the alarm flag:Code:rtc.clearAlarm(1);delay (5000); // power will shut down before this completes