Hi all,
I'm working an an Arduino project that logs data from the fuel injection system in my car.
The data is stored in arrays, but I want it to persist when the car is turned off and back on. As such, I have it writing to an SD Card.
However, I only need to capture the last moment of data, so that when it's powered off and back on, it can start back up where it left off. Right now, the only way I can think of to do this is to have it writing every 2 seconds to the card so that it's sure to always have the last, most up-to-date data saved just in case the car is turned off. But that is a LOT of unnecessary writes to the SD card. Over a 2 hour drive, that's 3600 writes, when I only really need the last one.
The issue is that since the Arduino is powered by the car, turning the car off immediately kills power to the Arduino. There's (currently) no way for the Arduino to detect that the car is about to be shut off, and do a write only at that time.
So, I was thinking that there are two sources of power in a car. One is constant 12v (on all the time), and key-on (only powered when the vehicle is in use). I'd like to utilize this but don't know how.
My thought is that the Arduino is always powered, but can go into a holding/sleep state.
When the key is turned on, the Arduino detects this on one of the pins and powers up the system.
It then runs as normal when the car is on.
Then, when the key is turned off, the Arduino is still running. But, it detects the power "off". It then saves the data to the SD, and then goes to sleep until the car is turned back on.
Is there a way to do this?
Is there a sleep state in the Arduino that can still detect a wakeup call?
Or do I just need to put it into a small, infinite loop that waits for the pin to be activated again?
Have a small circuit detecting the power loss, add a capacitor large enough to power the arduino as long as necessary for saving the data (eeprom instead of SD ?)
What is RPM and fuel injection status when you turn the key off? How can that possible be the same as when you turn the key on? Something is missing in what you are writing.
@J-M-L has a good idea. To embellish on it why not use FRAM memory, it will not wear out, works at memory speeds, is not expensive, and remembers without power. I use it a lot and I cheat a bit as have the RTC so I remove the EEPROM and replace it with FRAM, addresses etc are the same. When saving there set up two groups, and when you write one set a flag and clear the other (you can use the same flag). You update it after the data is written so if you lose power during a write the other data is still valid.
You could use the neutral safety switch output to trigger the Arduino, may require a relay depending on the vehicle. That way when you move the shifter from drive to park it triggers the power supply turning the arduino on using the ignition power from the car. You can wire it using a capacitor to power for a short time while the car is still running and the capacitor could take over for a short time when you turn the ignition off.
The only caveat would be backing into a parking space but it will still turn on when you shift from drive to reverse.
I don't understand the data you're collecting, but if the Arduino is always powered, then you won't lose anything by going to sleep. So perhaps you can just let the data stay in ram in your variables/arrays. Then if you can detect when the ignition-switched power goes low, that can be a signal to go to sleep, and when it goes high it can wake up the Arduino through an interrupt, and just pick up where it left off.
Which Arduino will you be using? How are you handling powering it through all the noise and spikes that show up in the power lines?