Adding the storage of a state when powered down

Hi all
I have some made some basic DCC decoders using Nanos where the incoming data stream is resolved to produce a 2 state output (high or low).
However they do not currently store the state when power is off.
Is it possible to store the state in memory so that on power up it is as when powered off?

Cheers
Keith

You could consider using EEPROM. However, whether that is a viable proposition depends on how frequently you need to save the state. This is because EEPROM has a limit as to how many times you can safely write to each EEPROM location

you could use EEPROM
or if you change data often an FRAM

Thanks for the replies
The EEPROM is probably out of the question as the state could be different everytime the Nano is powered off

Just had a look at the FRAM module, that could be the way to go, nice and neat.

You could power the Nano from a capacitor charged through a diode from the main supply.

Use an digital input to detect when the main supply goes off, then immediately save the required state to EEPROM.

The capacitor only needs to maintain the Nano's power supply for a few milliseconds, whilst the EEPROM is being written to.

You don't have to wait till the power goes off. Using EEPROM.update or EEPROM.put will only write to the EEPROM cell if the new content differs from the previous one.

So this is 100% safe / will not wear out the eeprom cell.

byte value;

...
...

void loop()
{
  EEPROM.update(someAddress, value);
}

So the question is how often your data will change to determine if it's feasible to use EEPROM or not. Each EEPROM cell is guaranteed for 100,000 writes.

And lastly you can use wear levelling which causes the data not to be written to the same cell every time that it needs to be modified.

If you implemented circuitry to "know" when power was about to be cut, and took the few dozens of milliseconds before it went to zero, the question would be not

it how often will you be turning the device on and off.

I'm not sure too many things around here have been turned off 100000 times.

If you think that might be sufficient, then the question becomes which kind of fun would you rather have

  • switch to FRAM or
  • use wear leveling or
  • add a capacitor, a diode and use one more input pin

a7

OK, thanks for the suggestions so far
A bit more info to clarify.
The device is operating point motors on a DCC controlled model railway.
Whilst the railway is being used the motor and hence the state could be changed quite a number of times.
When it switches on after a power up it goes to the default state (0), I would like it to store the last state set before power off and restore it when powered up again.

EDIT
each module will be operating 4 or more motors individually, so more than one piece of info to store, I assume that's not a problem, same when restoring, the data would be retrieved and set outside the loop.

@melmerby I propose the same. Just costs you one diode and one capacitor and you can do the rest in software. Some time ago I have used an analog input and played around with several capacitors. 1000µF will be a good value to start with.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.