Logging a power fail

Thread Starter

PhilTilson

Joined Nov 29, 2009
131
Hi -

I have a device based around a RasPi Pico that runs on a powerbank for around 10 hours per charge. While running, it logs activity at various times to internal memory. It detects darkness, turns on some high-power LEDs, then detects dawn and turns off the LEDs. Usually this sequence occurs before the powerbank runs out, but sometimes the power runs out before the LEDs turn off.

I want to detect the fact that the power has disappeared and log the fact and time before the device finally dies! It seemed a fairly simple task: detect the disappearance of the 5V supply from the powerbank and run an ISR, keep the CPU going with a capacitor across the 3.3V, write the log entry then let the system die.

The problem is, I cannot get the system to run the ISR, even though I am using irq(trigger=Pin.IRQ_FALLING... in the code. Scoping shows an abrupt fall in the 5V level and the 3.3V slowly decaying over 150mS or more, which is quite long enough to write the log, so why isn't it working?

I have not posted diagrams, code etc at this stage as I didn't want to make too long a post, but I'm happy to do so on request. If anyone has a better way of achieving this, I'd be delighted to hear it!

Phil
 

Ya’akov

Joined Jan 27, 2019
9,169
I am not sure what your problem with the ISR is, but an alternative approach is to write out a timestamp each second (or whatever resolution you need), clobbering the file each time. It will contain the last time it could write and requires no power supply jiggering.
 

trebla

Joined Jun 29, 2019
545
Have you tested how long the RPi can run on the selected capacitor? And some voltage regulators have integrated reverse diode which allows drain the capacitor via outside circuit, so the capacitor does not help much, you must add some reverse current blocker to your system.
 

Thread Starter

PhilTilson

Joined Nov 29, 2009
131
Interesting thoughts, but...

Ya'akov - The log is designed to be read by the user from time to time. Logging every second (or even every minute) will make for a vast amount of 'dross' for them to get through in a ten-hour session!

trebla - As mentioned, I have looked at the voltages on a 'scope and the 3.3V output from the Pico stays well above the CPU minimum for over 60mS. The problem seems to be in getting the ISR to fire. Let me include the relevant bits of the code:

Python:
pFail = Pin(26, Pin.IN)
...
def haltTask():  #[11]
    writeLog("Power failure" + str(inp12V.value()), 1)  #[11]
    sys.exit()  #[11]
...
pFail.irq(trigger=Pin.IRQ_FALLING, handler=haltTask())  #[11]
...
and the Scope picture:
Scope1.jpg
 

ErnieM

Joined Apr 24, 2011
8,377
...While running, it logs activity at various times to internal memory...
I am not sure what your problem with the ISR is, but an alternative approach is to write out a timestamp each second (or whatever resolution you need), clobbering the file each time. It will contain the last time it could write and requires no power supply jiggering.
What Ya’akov is suggesting is to use a single internal memory location to save the last ping time.

When you recharge the battery and restart the battery that location contains a value as close as you wish to when the power went out.

You do need to inhibit the ping time storage until you read out the older value.

Also, there is a small possibility of saving a corrupted value if the save occurs simultaneously with the power outage.
 

Ya’akov

Joined Jan 27, 2019
9,169
Interesting thoughts, but...

Ya'akov - The log is designed to be read by the user from time to time. Logging every second (or even every minute) will make for a vast amount of 'dross' for them to get through in a ten-hour session!
That’s why I said to clobber (overwrite) the value, there will be only one. And if there is a concern about corruption, you can write to three places, and compare. If the power dies jut after or during the first write the other two will be the previous value.
 

Ya’akov

Joined Jan 27, 2019
9,169
Also, if the computer is always connected, you can use a heartbeat (could be Vbatt) sent to the computer and let it log. When the heartbeat stops, you know there is something wrong.
 

WBahn

Joined Mar 31, 2012
30,065
Also, there is a small possibility of saving a corrupted value if the save occurs simultaneously with the power outage.
This can be dealt with by writing to three memory locations in rotation. If nothing bad happens, all three locations will have valid timestamps so that, when organized earliest to latest, will have the correct time interval between them. So you use the latest one. If the last one to get written gets a corrupted value, then you will have two timestamps with the correct interval and one timestamp that doesn't. So you can take the latter of the two properly spaced timestamps and add one interval period to it. The only way this fails is if the corrupted value just happens to be the correct interval before the earlier of the two valid timestamps, in which case your accepted value will be early by one timestamp interval, so choose a interval small enough to make this an acceptable error.
 

Thread Starter

PhilTilson

Joined Nov 29, 2009
131
Now I understand! Some really good ideas there - thanks guys! These forums are so much gentler than the stack overflow ones, which I find a bit overbearing sometimes!
 
Top