3
\$\begingroup\$

I have done a lot of research on how to design a circuit to cut power from the pi after a safe shutdown and then turn it back on at a certain time. I am using a DS3231 RTC with an active low ALARM pin to operate the switching circuit.

I have found the following two diagrams from https://www.allaboutcircuits.com/projects/build-programmable-time-based-switches-using-a-real-time-clock/ for possible solutions. Disregard the PICAXE in the first picture. This I2C comm will be conducted by the pi when it is on.

Both of these solutions use batteries it looks like for the switching circuitry with the aid of the ALARM signal from the RTC, however, we don't need this. We will have a stable 5V/3A DC connection and simply want to reduce power consumption of the pi by cutting power based on the alarm signal. I would also like a tactile switch to re-apply power without having to wait for the RTC.

Here are my questions:

1) What is the best way to achieve a pass-thru 5V/3A depending on the output of the DS3231? The first alarm would be used to cut power, and the second alarm would be used to re-apply it. A relay would be preferable as the 5V supply is already regulated.

2) Is it valid to remove the cap/bat and connect our regulated supply instead?

3) From what I could gather in the datasheet, the alarm stays low until the flag is cleared... Will this create issues for the second alarm, or will the second alarm change the state of the output?

I don't need components, I can find those. I am looking to settle on a different schematic as I am pretty shaky with the analog portion of the design.

EDIT: It is also important to note that our RTC already has battery back up. Looking at the DS3231 datasheet, I see this,

When the RTC register values match alarm register settings, the corresponding Alarm Flag ‘A1F’ or ‘A2F’ bit is set to logic 1. If the corresponding Alarm Interrupt Enable ‘A1IE’ or ‘A2IE’ is also set to logic 1 and the INTCN bit is set to logic 1, the alarm condition will activate the INT/SQW signal. The match is tested on the once-persecond update of the time and date registers.

Option 1

Option 2

Final EDIT: With the accepted answer, I have come up with the following circuit to solve my problem.

Final Schematic

In essence, there are 3 aspects of the circuit: DS3231 RTC timer to enable power to the pi after the programmed time has been reached, a load switch to support 5V/3A with an enable pin with low quiescent current, and a 555 timer that will be used to retain power to the pi while it is shutting down.

DS3231:

This circuit is pretty straight forward. Basically, it has a charge circuit to charge a Panasonic 5mAh battery while the full system has power just in case power is ever lost. Our system is connected to a large battery, so we want to ensure that we can still keep time if power is completely lost (Our regulated 5V/3A is supplied by the USB output of the charge controller). The alarm pin after the inverter will remain high until the flag is serviced via I2C, so when the battery is charged again or if it is powering up the circuit, it will hold the load switch on.

Load Switch:

This takes an OR'd input from the 555 timer, the DS3231, and a GPIO pin from the processor. Once power is applied to the system, it will assert the GPIO pin to hold the switch on to maintain power after the DS3231 alarm flag is serviced. This switch will also activate from a 555 timer that is specific to our pi implementation.

LM555 timer:

Because the pi SD card can be corrupted if power is immediately removed during numerous conditions, we needed a way to delay the on-time of the load switch to ensure the pi can safely shut down. To operate in this mode for a safe shutdown, the pi de-asserts the pin for the 555 to begin "counting", the resistor and cap has been chosen for 33s of delay time to hold this high state. After this, a shutdown command is immediately commenced and after some time, the other GPIO enable pin will be automatically pulled low. Once the 555 is de-asserted, no other enables are high on the load switch causing output to be grounded. Finally, we OR'd the 555 input with an active low tactile switch and the pi shutdown status so that we can use this switch to induce a manual power on. We didn't want to use an on/off slide switch on the input of the load switch. If the user forgets to turn this off, the system will remain on, and it will be impossible for the pi to turn back on without manual re-application of the power cable. This input OR will work because the timer in monostable mode does not hold the input state, it waits for a low-going edge and and asserts the output. Once the discharge cap reaches 2/3Vcc, it will de-assert the output. With our active low switch, we want to see the low-going edge of the pi which is possible in this case. If the system is off and stable, the assertion of the button followed by a release will provide the trigger that the 555 needs to start.

Note: This circuit has not yet been tested, but after simulating the 555 and the load switch, I am confident enough to purchase parts and start testing it. For anyone referencing this, note that my final schematic level shifts the button from 5V->3.3V and the only other level shifting present are the I2C lines. I checked the datasheets for the load switch and 555, and 3.3V logic from the pi is "good enough" for a 5V input. My only concern is the enable on the load switch going from 5V->3.3V after the alarm has been serviced, but I'll just have to test and find out if this has any implications. A lot of the decisions here are geared towards a Pi, however, if a uC needs to instead be supported, this final circuit will become much much simpler!

\$\endgroup\$
  • \$\begingroup\$ Something like the Vishay Load switch might suit: vishay.com/docs/66597/sip32431.pdf You would need a 2 input gate to ensure that the PI could force the supply on when it was running. \$\endgroup\$ – Jack Creasey Sep 18 '18 at 22:48
  • \$\begingroup\$ Neither circuit will work. A switch for a device's own power requires at least two active semiconductors, otherwise the circuit ends up at least partially turning itself back on through the protection diodes. \$\endgroup\$ – Chris Stratton Sep 20 '18 at 1:46
  • \$\begingroup\$ RE: Your schematic (marked final EDIT). Haven't looked carefully at all of the circuit, but from the data sheet and as mentioned in the article, INTSQQ is open-drain. You have that signal going directly to the inverter input. How will it drive the inverter high or low? I think you might want to use a pull up resistor such that the input to the inverter will be high normally and low when an alarm has occurred. \$\endgroup\$ – DrG Sep 23 '18 at 2:13
  • \$\begingroup\$ Thanks for pointing that out, I didn't catch that! I have parts coming in to prototype a few different ideas regarding the switch as the current schematic I posted will not quite work as expected. Once I see how this thing works, I'll possibly be moving to a uC for the enable pin control instead of the 555 timer, as I now feel it'll not only simplify the circuit but also make it cheaper and more versatile! \$\endgroup\$ – nichollsg Sep 23 '18 at 22:42
1
\$\begingroup\$

Are you trying to run the RPi on a battery pack?

There are two ways of handling this:

  1. Load switch

  2. Controlling the power supply feeding the RPi

In the first schematic, it looks like a PFET is being used as a high side switch to control power flow to whatever is downstream. You can connect a OR'ing Schottky diode to the Alarm of the RTC + a GPIO pin from the RPI. Reset the RTC and be good for the next run. Once the RPi shuts down, you lose the latch and everything should power off.

The circuit would look something like this:

Load switch configuration

If you search any electronics distributor's website for load switch, you'll find what you need there.

Alternatively, you can do the same OR'ing procedure but connect it to the enable pin of a switching power supply (or whatever is feeding the RPi). I've done this with fantastic results in most cases. Make sure that if you use a switching power supply the output gets disconnected from the input when disabled.

The important thing is to setup the RTC to wake up the circuit at some point in the future when you're in the process of shutting down. (Single shot would be ideal instead of repeated)

The neat thing is you can power the RTC off a coin cell CR2032 (or similar) and it will last a looooong time as long as you're careful about any static current to ground (pull-downs pull-ups)

Good luck!

\$\endgroup\$
  • \$\begingroup\$ No, we aren't planning to use a battery to power the pi. We have a large battery that is charged from the solar panel and this 12V output supplies a buck converter to get our 5V/3A for the pi. For all intents and purposes, it is safe for us to assume this will always supply our required voltage/amperage. However, while a "shutdown -h now" turns "off" the pi, I have measured that there is still ~0.5W being drawn, thus, we want to completely cut this power once it has safely shutdown and use the RTC to re-apply power at a specific time. \$\endgroup\$ – nichollsg Sep 20 '18 at 22:54
  • \$\begingroup\$ Ahh got it. So technically, you are still running on a battery. ;) Controlling the enable of that buck seems like the best option then! (as long as when it's off the output is off) \$\endgroup\$ – jaredwolff Sep 21 '18 at 16:40
  • \$\begingroup\$ haha yes, technically we are running from a battery. Our requirements have changed a bit and so I have 2 circuits so far to achieve this. Our battery controller has a USB 5V/3A so we are going to use this to save cost on the PCB. I haven't finalized the schematic, but I am in essence incorporating what you have suggested. It involves the DS3231 with a charging circuit for the RTC backup battery, a 555 timer to allow the pi to safely shutdown, and a TPS load switch from TI. I'll mark this as solved and upload my schematics here shortly. Thanks for the help! \$\endgroup\$ – nichollsg Sep 22 '18 at 0:43
  • \$\begingroup\$ Nice! Good luck and if you have any more questions feel free to post em here! \$\endgroup\$ – jaredwolff Sep 23 '18 at 1:03
5
\$\begingroup\$

The schematics come from an article written several years ago (which I was paid for) that you can see here https://www.allaboutcircuits.com/projects/build-programmable-time-based-switches-using-a-real-time-clock/ I think it is always a good idea to give the citation with the schematics, both to acknowledge the source and so people can see the context. Edited to add: Thank you for editing the initial post to include the citation. Also note that neither schematic posted includes the DS3231 schematic (see original article).

I can't answer each of your questions thoroughly, but there are a couple of points that I would make that I think are important. Both circuits are dedicated for particular uses. The top one is to operate a small relay briefly and the bottom one is to operate a data collector (like an ESP8266 with sensors). Both were tested extensively and work well (in my opinion and experience).

What you have to understand is that there is a limit to how much the LP0701 alone can drive and it is going to be well below 5V/3A. So, neither of these circuits will do what you want.

While the DS3231 has two alarms, it has only one alarm signal. When either alarm (triggered by a time comparison) is hit, that single alarm signal is active.

The signal is active low with an open drain output. It is meant to have a resistor tied to a voltage source not greater than 5.5V. When the alarm is active, the signal in these two circuits goes directly to the gate of the LP0701.

One way or the other, you have to send the command to reset the interrupt flag on the DS3231 or the alarm will remain active. Whether this is done by the RPi or by something else, that command has to be sent over the I2C interface to the DS3231.

This means that when an alarm happens, it could, potentially, power up the RPi which would shut itself off after doing whatever it does, by clearing the DS3231 flag - and would then be powered up again as a result of the next alarm. Although I don't use the RPi, this seems plausible to me IF you had the switching circuitry to handle that power level and be initiated by the alarm signal.

Off the top of my head, I can't doodle out that circuit - I'm not that good, but I'm sure there are others who could. It may very well be the case that you could operate a relay that would switch power to the RPi (not unlike how the first circuit is designed, but with a larger capacity relay). That seems clunky and noisy and I don't know how well it would work - but it might. In that case, you only need the DS3231 circuitry to switch a relay.

As to your question #3... if you don't shut off the alarm, when a second one occurs (from either of the alarms) it will never be noticed because the first alarm is still active. A second alarm occurring while the first one is still active will not toggle the alarm signal as far as I know (why would it?). So, yes, if you don't clear the flags with intention, you will likely not have the operation that you want.

Hope this helps.

Edited: I can’t add comments (as a new user) but am editing my answer to respond to a user comment that “Neither circuit will work. A switch for a device's own power requires at least two active semiconductors, otherwise the circuit ends up at least partially turning itself back on through the protection diodes.”

Again, both circuits work just fine in my experience. One has been working continually for about three years (with battery changes) dutifully resetting a router nightly. There is no “partially turning itself back on through the protection diodes”.

It may be that the user who commented simply does not understand that each of the posted schematics requires the DS3231 circuit which is shown in the article but not in the original question. Additionally, these are straightforward circuits.

For the top circuit: The gate to the LP0701 is held at ~5v with the 10K resistor pulled up to ~5v (from the battery) and is also connected to the DS3231’s alarm output. The DS3231 is powered always (by the batteries when the attached device is on (Vswitched), or by the RTC battery backup. When the alarm occurs, the voltage at the gate of the LP0701 drops to ~0v and Vswitched activates the load (the relay, the DS3231 and the Picaxe, in this case). When the alarm signal is reset by the I2C commands (from the Picaxe), voltage at the gate of the LP0701 is back to 5v and Vswitched is deactivated.

I have no problem admitting a mistake if I have made one, but given extensive experience with both circuits and in the absence of evidence or even a compelling explanation to the contrary, I am going to respond that the comment is simply wrong.

\$\endgroup\$
  • \$\begingroup\$ Thank you for the thorough answer, I appreciate the time you took into analyzing my question. I apologize for not citing where I got the info, I have now updated that above. Anyways, I think I have come to a pretty reasonable solution, so I will be uploading the schematic once it is final. Some aspects are unique for the pi since there are known SD issues by just pulling the power, so we have had to incorporate some safety with a 555 timer. Anyways, thank you again! \$\endgroup\$ – nichollsg Sep 22 '18 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.