detecting a powercut (battery powered device)

i have a project which runs on a battery which is in turn connected to a powersupply...

the problem is i want the arduino to know when there is a powercut and when the power is back on...

so i was thinking
____________________________________________(to arduino interrupt)
| |
Power source -------------- |>|---------------- |=====| ------------- Arduino
(diode) (battery)

i was thinking when the power is not there the battery voltage does not flow back because of the reverse bias so the interrupt pin would go low so the arduino could detect a powercut...

what diode should i be using here? battery will be 12 volts...

simply direct a feed off the wall power supply to a voltage divider (to get less than 5v)

if (digitalRead(pin)==LOW)
{
//Power Out
}

using the diode method along with the battery is fine, but I prefer to float charge the 12v to 14.5v then to a 5v regulator (or 7v) or dc barrel jack.

The Arduino has a built in power switch.
If the i/p is > 6.2 volts USB power is turned off and the Arduino is powered by the i/p 12V (in this case).
If you were to connect a 4.8V P.S. (3 X 1.6 = 4.8V Alkaline D cells) to the USB power pins, no power would be drawn normally.
When the power fails the switch would turn on and the Arduino would be powered by the batteries (while power is off).
Then use cjdelphi idea of the voltage divider to detect the power loss.

while i was trying to implement it, i realized it was just one value which i had to store incase of powercut, so i thought i will store it into eeprom

but i read eeprom had only 100,000 life cycles...

so i want to know how do i switch between multiple locations so that lifecycle is not completed, and remember which memory location even when power goes out???

i mean to say, i write into 1st location and then when power comes back on i read it from 1st location.... but in case somehow in my code i implement it to write into second location how do i get arduino to check in the second location for the data..??

i suppose one way would be to use start off by wiping them all to #0 / null chars (all the eeprom memory) and then start at byte 0, and have a marker eg x,[inservalue] and then simply scan the eeprom for the 'x,' part and pull out your data next time there's a power out, it wipes them 3 chars out and you shift right 1 byte start again...

never written cells in the eeprom contains "FF" , then it's easy to read the last written position :wink:
(if you avoid writing FF in a cell ^^ )

how many power failures do you expect ?
100000 cycles, even if used once a day, gives you several years

well the data has to be retained after the powercut, but the data is written everytime a sensor is triggered, so at max 50 times a day... that gives me about 6 years!!

i know that is a lot but just want it to run without glitches :smiley:

the unwritten eeprom contains FF, that is interesting... will give it a try :slight_smile:

but how do i write to a next cell??? how will the arduino get to know that it has written enough in this location and it is time to switch on to the next memory location ??

alnath im from india, so most probably thrice or 4 times a day a powercut

oh, i never knew the arduino had such a switch... helps me cut out on expensive 12v battery!!

thanks cjdelphi and larryD :slight_smile:

kiriti:
the unwritten eeprom contains FF, that is interesting... will give it a try :slight_smile:

but how do i write to a next cell??? how will the arduino get to know that it has written enough in this location and it is time to switch on to the next memory location ??

have a look here
http://arduino.cc/en/Reference/EEPROM :slight_smile:

edit : found something that might help you (I need 2 or 3 more readings to understand everything, but....)
http://www.edn.com/design/systems-design/4359278/Overcoming-erase-write-endurance-limitations-in-EEPROMs

kiriti:
what diode should i be using here? battery will be 12 volts...

That depends on the battery and charger. The forward current rating of the diode should be the max. charging current and some extra. A car battery used with a 15A charger should have a diode capable of handling 20A.

well the data has to be retained after the powercut, but the data is written everytime a sensor is triggered, so at max 50 times a day... that gives me about 6 years!!

Why save it to EPROM at every reading, only store it to EPROM when the power failure occurs.

Tom