Saving data before shut down

I'm soon going to start a small project and I'm completely new to the Arduino, I didn't even buy one yet…

I will try to build something for my electric scooter. It will collect some data and display it on a lcd screen while driving. It needs to remember some data between every time I drive, so I need to save things on the built in EEPROM, as I understand it. I also understand that there is a limit how many times I can write to the EEPROM, so I thought that just saving all data every time I shut the scooter off would be a good idea. That would probably be twice a day or something like that.

The battery of my electric scooter (60 V, 40 Ah) is removable, so every day I take it out and recharge it inside my apartment, leaving the scooter and the Arduino without any kind of power. The scooter is equipped with a DC/DC converter for 12 V, which I will use to power the Arduino.

I think I will go for the Arduino One, I don't know if that's a good idea, but it seems to fulfil the other needs of my project.

So what I need to know now, is probably something like the following:

When the power is off, is the Arduino immediately dead or is there still some time to write to the EEPROM?

Is there a way for an Arduino program to see the voltage of the power connection?

What is the most common or easy approach for solving this problem?

Is it generally a bad idea to just cut the power off the Arduino without running some kind of shut down function first?

The idea that first comes to my mind is to just power the Arduino with a capacitor that is charged through a diode (and maybe a serial resistor) by the DC/DC converter and also connect the output of the DC/DC converter to an input of the Arduino. If the voltage from the DC/DC converter is low, the Arduino should save all the important data to the EEPROM. Since the Arduino should work fine from 7 VDC (right?), I don't think the capacitor needs to be very big to keep it running until all data (probably less than 20 bytes) is saved.

Any good advice about this? I want to keep the project as simple as possible, the fewer external components, the better.

When the power is off, is the Arduino immediately dead

Yes.

s there still some time to write to the EEPROM?

No.

I don't think the capacitor needs to be very big to keep it running until all data (probably less than 20 bytes) is saved.

Yes it works out to be big.

The best way to do this is to have some form of rechargeable battery that switches in when the power is removed and keeps it going. Then the switch also activates an interrupt and the interrupt service routine then saves the data in EEPROM.

A rechargeable battery is a safe bet, but a capacitor (as suggested) may be sufficient as well. In either case the interrupt routine should save the data, this ISR should not use functions that require further interrupts.

For testing purposes, monitoring the source and capacitor voltage is a good idea. All you need are voltage dividers to transform the voltages into acceptable ADC or digital (interrupt) values. Then you can write the capacitor voltage to the EEPROM, as the last value, and increase or decrease the capacity until the voltage stays high enough until everything has been stored.

DrDiettrich: A rechargeable battery is a safe bet,

Yes, I believe it is, but I think it will make the project bigger than necessary. Either I need a quite big battery, since the scooter could be switched off for a week or so, or I need to implement some kind of energy saving mode and turn things off that's not needed when the scooter is turned off, such as a GPS shield, which won't work well anyway since I store the scooter indoors.

DrDiettrich: but a capacitor (as suggested) may be sufficient as well. In either case the interrupt routine should save the data, this ISR should not use functions that require further interrupts.

For testing purposes, monitoring the source and capacitor voltage is a good idea. All you need are voltage dividers to transform the voltages into acceptable ADC or digital (interrupt) values. Then you can write the capacitor voltage to the EEPROM, as the last value, and increase or decrease the capacity until the voltage stays high enough until everything has been stored.

Sounds like a good idea.

Thanks for your inputs, everyone!

By the way, does INT0 and INT1 work like the other digital inputs? I think I read somewhere that voltages <3 V is considered LOW and >3 V is considered HIGH. Is there a hysteresis as well?

In other words, do I need to create some external switch using a comparator to get a steep flank or can I connect to INT1 directly from the voltage divider?

If not needed, my first thought would be a simple voltage divider using 27 and 10 kΩ respectively to divide 12 V down to 3.24 V, or maybe 27 and 12 kΩ dividing to 3,69 V. I’m not sure how close to 3 V I can get to be sure that no unnecessary writing to the EEPROM is done, maybe 3.24 V is too close…

I would think that you could have a capacitor see when the power has been lost, shut off all power use, write your data and then die.

not sure if anyone has come up with a 'dying gasp' circuit.

I would think that one AA battery would offer enough power, or one 9v rechargeable.

if you are going to make one, for your own use, and use a NANO or some such, then everything is external. if you are planning on making your own board, then you have more options for power saving and data saving.

as a note, I would suspect that you have to open a cover or unplug something to remove power. why not put a switch on that cover. every time it is opened, data is written ? you could put in a off-switch that will signal the write command, then shine an LED that it is safe to un-plug.

By the way, does INT0 and INT1 work like the other digital inputs?

Yes.

I think I read somewhere that voltages <3 V is considered LOW and >3 V is considered HIGH.

You might have read it but it is not true.

It is < 0.3Vcc so if Vcc is 5V it is 1.5V for a zero and > 0.7Vcc = 3.5V for a one.

However you could use the built in voltage comparator to trigger the interrupt.
http://gammon.com.au/interrupts

The interrrupt must be triggered when the external power drops to zero, that's not hard to achieve. You can use an (built-in) resistor to Vcc, and a diode to the external supply.

If possible, separate the controller supply from the other circuitry, so that the controller can be powered from the cap long enough. Send the controller to sleep when done, and wake it up (reset) when power is restored.

The limit on how many times you can write to the EEPROM on the Arduino UNO is 100000. Depending on your application you may not need to write the values at power off only. You can write the value to EEPROM anytime it changes or write it on a set interval. If the value may not have changed since the last write use one of the update EEPROM functions to prevent unnecessary writes. Although this may cause more wear on the EEPROM, it might be that the required frequency of the writes is low enough to make it not a problem over the lifetime of the device.

Pert has a point. if it takes you 6 seconds to stop, take off your helmet, turn you scooter over, then open the cover, and take out the battery.....

then you can just write to eeprom when the scooter has been idle for more than 5 seconds.

dave-in-nj: then you can just write to eeprom when the scooter has been idle for more than 5 seconds.

Yes, even if that causes some unnecessary writes if you do the math on how many possible writes this could cause per day you'll probably find that the EEPROM will outlast the scooter and the AVR. A little software work eliminates the hassle of the capacitor/backup battery and interrupt.

pert:
The limit on how many times you can write to the EEPROM on the Arduino UNO is 100000. Depending on your application you may not need to write the values at power off only. You can write the value to EEPROM anytime it changes or write it on a set interval. If the value may not have changed since the last write use one of the update EEPROM functions to prevent unnecessary writes. Although this may cause more wear on the EEPROM, it might be that the required frequency of the writes is low enough to make it not a problem over the lifetime of the device.

One of the values (Odometer) will change approximately every 20 ms, that’s why I ask the question in the first place.

dave-in-nj:
Pert has a point. if it takes you 6 seconds to stop, take off your helmet, turn you scooter over, then open the cover, and take out the battery…

then you can just write to eeprom when the scooter has been idle for more than 5 seconds.

The scooter has no idle mode - it’s an electrical scooter. When the key is turned to off mode, all power is gone.
One way would of course be to install another DC/DC converter (60-12 V) that is not connected to the key and run the Arduino with it.

Add a serial FRAM to your circuit, write to it as often as you like. SRAM access speed, EEPROM non-volatility (without 3.3mS write times). http://www.digikey.com/product-search/en?pv154=29&FV=fff40027%2Cfff80434&k=fram+memory&mnonly=0&newproducts=0&ColumnSort=0&page=1&stock=1&quantity=0&ptm=0&fid=0&pageSize=25 Way more bytes of storage available vs EEPROM also.

guraknugen: The scooter has no idle mode - it's an electrical scooter. When the key is turned to off mode, all power is gone. One way would of course be to install another DC/DC converter (60-12 V) that is not connected to the key and run the Arduino with it.

you just changed the rules. your first post did not say you lost all power with the key. and you do not really, the battery is still connected. you said you removed the battery. turning the key does not remove the battery. you can easily write a sketch that says, when the key is turned off. save the data. if you do this twice a day, your eeprom is fine. if you do that 10 times a day, the MTBF for the eeprom would be over 30 years. and in fact, this is the best signal to use to write the data. this makes my previous posts easier to implement. when you turn on the key, the Arudino will take a few seconds to come alive. if you power the ON circuit to start with the turn of the key, you can easily have the unit powered from the battery so that the key on turns on a relay that powers the arduino. then, either complete power loss, power failure or battery removal would be one way to have the relay fail to open. the arduino could write to eeprom, then open the relay and remove power from itself. this would be done before you removed the battery. once power is lost, either by battery removal or self shut-off, a new key turn would be required to re-start.

dave-in-nj: you just changed the rules. your first post did not say you lost all power with the key. and you do not really, the battery is still connected. you said you removed the battery. turning the key does not remove the battery.

Well, I'd rather say that I added a rule that I just forgot to mention it in the first post, sorry for that. As you say, turning the key does not remove the battery but there are currently no cables available directly from the battery. However, which I didn't think of when I wrote my first post, there will be, since I recently connected a shunt to the battery for measuring the current. I didn't finish that yet though, but I will as soon as I get some time to do it. So yes, battery voltage will be available even when the scooter is turned off, as long as I don't disconnect the battery, which is located under the seat and connected with a red Anderson connector. I'm sorry that I didn't think of this when writing this thread. And I remove the battery almost every day to recharge it in my apartment, that's still true, but maybe not that important to know about… :P

dave-in-nj: you can easily write a sketch that says, when the key is turned off. save the data. if you do this twice a day, your eeprom is fine. if you do that 10 times a day, the MTBF for the eeprom would be over 30 years. and in fact, this is the best signal to use to write the data. this makes my previous posts easier to implement. when you turn on the key, the Arudino will take a few seconds to come alive. if you power the ON circuit to start with the turn of the key, you can easily have the unit powered from the battery so that the key on turns on a relay that powers the arduino. then, either complete power loss, power failure or battery removal would be one way to have the relay fail to open. the arduino could write to eeprom, then open the relay and remove power from itself. this would be done before you removed the battery. once power is lost, either by battery removal or self shut-off, a new key turn would be required to re-start.

My native language is not English, which some times leads to misunderstandings, but it seems like your suggestion involves another DC/DC converter, right? The scooter has one already to make it possible to use standard equipment, such as 12 V light bulbs and so on, but it's turned off with the key. So what I need is another DC/DC converter that is powered directly from the 60 V battery, since the Arduino doesn't like to be powered with 60 VDC (in reality about 63-70 VDC), and since I need 12 V for a little while after the scooter had been turned off with key. Or did I totally misunderstand?

guraknugen:
So yes, battery voltage will be available even when the scooter is turned off

OK, so here’s a suggestion: When the odometer value hasn’t changed for x seconds do the EEPROM write. You just need to figure out the minimum number of seconds from stopping the scooter to removing the battery and set x to a bit less than that value. This will cause some unnecessary writes when you have stopped but didn’t remove the battery so you would have to analyze the EEPROM life for your usage. Another possibility for extending the EEPROM life if you have unused EEPROM is to save the number of writes you have done and then when that number reaches 100000 change the EEPROM addresses to the next unused cells.

pert: OK, so here's a suggestion: When the odometer value hasn't changed for x seconds do the EEPROM write. You just need to figure out the minimum number of seconds from stopping the scooter to removing the battery and set x to a bit less than that value. This will cause some unnecessary writes when you have stopped but didn't remove the battery so you would have to analyze the EEPROM life for your usage. Another possibility for extending the EEPROM life if you have unused EEPROM is to save the number of writes you have done and then when that number reaches 100000 change the EEPROM addresses to the next unused cells.

Yes, I see what you mean. That would probably also work. I'll probably go back and forth in my mind between the different ideas that has been mentioned in this thread before I decide what the final design is going to look like and why… I guess I have some time to think; I didn't even buy the Arduino yet and a few more details are not solved yet, but I'll get there!

Thanks to everyone! I must say that this forum seems to be very active and alive. I have also visited the Swedish Arduino forum, and it was… what could I say… dead… :grin:

For my own amusement I put together a model circuit to do the capacitor hold up long enough to write EEPROM and ran it through the simulator. This is using the SIMetrix tool.

Unless I’ve gotten something terribly wrong or misunderstood the problem, the capacitor value isn’t unreasonable, so I think that approach could work. With a 2200 uF capacitor, the hold up time is on the order of 0.15 seconds where about 3 ms is required per write to the EEPROM.

The circuit is as shown in the attachment with the following explanation:

  • V1 - simulates the 12 volts from your 60 to 12 volt DC converter.
  • S1 - simulates the key switch on the scooter and V2 is a pulse to turn it on then off for the simulation
  • R2, R3, R6 - a voltage divider to get a “Sense_Pin” that is monitored by the Arduino to determine that primary voltage has been removed and the shutdown process should start. There is an implicit assumption that the 12 volt input is reasonably well constrained, otherwise more protection of the sense pin input may be required. There is also an implicit assumption that the voltage drops quickly which may not be the case if the DC-DC converter is on the other side of the switch and has significant capacitance on it’s output.
  • D1 & C1 are the hold up circuit, C1 holds voltage to the Arduino long enough for the shutdown recording, D1 prevents the charge from getting back to the sense pin
  • X1, R1, R4, R5 emulate the Arduino regulated power supply and load with ~50 mA nominal current drain

You also may consider using a cheap ds1307, offering 56 bytes of non volatile RAM (battery backed) with unlimited writes...