PDA

View Full Version : [Question(s)] Arduino Uno Brown-Out Detection Software



BrentB
10-29-2012, 12:29 PM
How do I use the brown-out detection that is apparently built into the Arduino Uno? I need to save off eeprom before the power goes completely off, and do not want to write to eeprom unless/until it is necessary.

I've seen some data on setting trigger voltages and stuff (fuses?), but I am completely unsure of how to execute code when the brown-out occurs. I'd hope this could be set up somewhat like the external interrupts. I don't want to continuously check some memory value. I'm currently using interrupt 0 to watch for a button push. Interrupt 1 is available, but I'm guessing the brown-out capability in the Arduino is going to function differently from those 2 external interrupts.

jwatte
10-29-2012, 02:41 PM
I recommend reading the Atmel Atmega328p data sheet. It describes exactly how the brown-out detector works, starting on page 47 (in the copy I have -- may move based on errata)

Unfortunately, I don't think the built-in brown-out detector will work for your needs -- it works by pulling the internal RESET signal when the voltage goes too low; this won't let you actually get any time to execute any code.
You can build an external brown-out detector which triggers an interrupt when voltage goes low, and if you put a big capacitor across +5V and GND, you will have enough cycles left to do something before it runs out. Tuning this detector so it's not too sensitive (going off as a false positive) yet still gives you enough time to run, might be a challenge.

A simpler option is to simply write what you need to EEPROM every five minutes or so, plus/and/or when "important" events have happened and need to be remembered. With 100,000 write cycles, and 5 minute intervals, that gives you about a year before the EEPROM wears out at its rated 100,000 cycles. You can write level sections of EEPROM by cycling through base addresses, for significantly longer time (depending on how small your data is.)

BrentB
10-29-2012, 03:51 PM
Thank you! Your response was excellent and quick. Are you sure about the 100,000 rewrite cycles for eeprom and that that limit is per byte?

I have read p47, but it left me with more questions than I started. I'm not real confident in that space, but was coming up with the same conclusion as you: this won't do what I want, because it is a "reset" trigger, not set up to register an emergency-interrupt service function that would fire off on a brown-out.

My critical cycle time is probably going to be no less than 1/2 hour, so 100,000 cycles would then last 5.7 years (my app runs 24/7/365). We're still in R&D, and the truth is that 1/2 hour is currently only a guess.

Originally I was told 1,000 cycles was the rewrite limit on this eeprom, not 100,000 cycles, so I was also wondering then about changing my base address just like you suggested, but didn't know if the cycle limit was byte-based, or if the cycle limit was true for all eeprom writes, no matter where the data goes.

My crucial data is only 2-4 bytes, so if I can write 100,000 cycles to those bytes and then move my base, the eeprom would be good for decades/centuries, which of course would be totally fine. It would then be a lot better and easier to use this 100,000-rewrite design (maybe with base addressing), instead of the brown-out interrupt design.

So again, may I ask, are you sure about the 100,000 rewrites and the per-byte granularity of that eeprom limit?

tician
10-29-2012, 06:15 PM
For the most popular AVR's (ATmega48/88/168/328 - Arduino, ATmega164/324/644/1284 - arbotix, and ATmega1281/2561/1280/2560 - ArduinoMega/CM-5/CM-510/CM-700), the EEPROM is byte writable and byte readable with 100,000 Erase/Write cycles, while the FLASH memory is page/sector writable and byte/word readable with 10,000 Erase/Write cycles. There are several less popular/specialized AVR's that only get 1000~2000 Erase/Write cycles for both EEPROM and FLASH (ATmega128RFA1).

jwatte
10-29-2012, 10:55 PM
EEPROM write cycles are typically per "page" of EEPROM, although it seems the AVR EEPROM (at least the 328p) has a page size of one byte -- I can see nothing else in the data sheet.

I would still come up with a timestamp and CRC for each piece of data. With a four-byte timestamp, four-byte data, and two-byte CRC, this means 10 bytes per item, so 102 items in EEPROM. If you cycle through them like this, you'll presumably extend lifetime by 102 times, so even a once-a-minute write will let the EEPROM last approximately 20 years.

You should probably try to contact an Atmel development representative at some point, though -- I don't know whether 100,000 writes is "MTBF" or "guaranteed" or whatever, and whether it depends in any way on temperature and operating environment. Also note the warning about writing during brown-out. Easiest is to set the brown-out reset to 4.7V and not worry about it :-) But with a good CRC algorithm, you'll typically not have a problem with corrupt data, as it would simply be treated as "not written."