Is there a safe way to power off an arduino and not risk SD files?
I’ve had a few instances where data logging files just don’t exist. I assume that I must have powered off whilst the SD card is being written to. Restarting a new file each hour helps with this.
However, I’ve had a couple of instances where the SD card becomes unreadable.
I’ve got a couple of ideas of how I may reduce this risk but I wondered if there is a standard way that people prevent this.
make sure all the files are fully closed before shutdown. if shutdown can happen at any point in time, then provide some sort of extra power buffer (a capacitor) that will let the Arduino perform the emergency flush before dying and trigger that safe shutdown when you detect the power loss.
➜ if you are unlucky, the power loss will happen at a "safe time" (when no data loss would have occurred) but the extra power you got will keep the arduino running long enough for a file to be opened or some sort of system operation happening on the SD Card.
You need to add power loss detection and trigger the emergency flush / close the files process.
the question is can you loose power randomly or is it the user pressing a software switch to turn things off what you want to address?
if the off button is "virtual" (it's a button managed by code), your arduino catches the press of that button and there is no energy challenge, you clean things up and perform a clean shutdown.
if the off button is a switch on the power line, then that's a different issue as your arduino just does not get a chance to do anything, it won't even know power went off.
It is just me disconnecting the power so that i can remove the SD card to download the data to my PC.
It's pure bad luck really. Every 30 seconds, the files are opened, data is written and then the file is closed. The odds of me disconnecting whilst the SD is active are really quite low, but I have still managed it.
Monitoring the power so that it never opens the files on a power drop would be my best long term solution but that may take a bit of time and research for me to achieve.
In the short term, i think I will add a momentary switch with delay to give me a safe time limit to power off.
you could have a switch that suspends the data logging. you just activate it when you want to remove the SD card and then switch it again to be back in business.
JML, thanks for your help.
I've sorted it by using a touch pin. If the touch pin is touched, a led goes high followed by a10 second delay and then then the led goes low.
This is checked whilst the SD files are closed.
As long as I power down whilst the led is on, I know the SD is not being accessed.
Not very elegant but it does the job.