Hi I'm a newbie and working on a project with Arduino Due(Atmel3X8E).I want to try and save the important variables to the eeprom during a power outage,using the brown out detector.Is it possible to do this? Thank you.
Not a good idea - if the power goes down when you writing to EEPROM, your EEPROM data will be corrupted. The brown-out detector goal is to shut off the MCU, before it goes mad due to low power voltage.
The solution of this issue - use some huge capacitors (about 4700 mkF) - like a small UPS device.
When external power goes off, you well get enough time and power to store your bytes safely.
How it works
When +5V is okay, the capacitor is charged on, and the light diode (pins 1-2) inside of PC817 is on, so the transistor part of PC817 (pins 3-4) is open, and POWER_GOOD signal is
When power goes off, capacitor starts to discharge and give power to the right part of scheme. The Schottky diode acts as a valve, that stops the power to go to the left part. The another diode, which is inside of PC817 now goes off, and the transistor goes to close - POWER_GOOD signal goes to
HIGH level and your program may start save data to EEPROM.
Why the optocoupler?
So, when we connect PIN directly to power, +5V, and connect AVR's VCC to +5V over the Schottky diode, we've got two diodes in parallel! This situation is very bad, because they may compete, which will conduct the current - and, in some cases, internal (which is weak), chip diode wins. All controller power current start flow thru this weak diode, heats it up and toasts the chip! Not very good.
An alternative solution to this is to use an "EERAM". This is an EEPROM with an SRAM buffer in front of it. A large capacitor is used to provide power while the contents of the SRAM are stored in the EEPROM.
It sounds complex, but it's simple since they are made as a dedicated chip. One good example is the 47C16 from Microchip. The EEPROM backup is completely transparent to the user, and you can use it just like an I2C connected SRAM chip (so no worries about wearing out the EEPROM by writing too often).
I think the gbg's circuit will fail in many situations. It will cause interrupt only when the power source voltage is quite low. If there is no considerable load before the diode filtering caps of the power source will likely hold enough charge and the "5V" will be falling slowly. Arduino will drain the the backup capacitor before it gets "power bad" interrupt.
Since OP is using 12V power supply a simple brownout detector giving warning when this source drops under some reasonable value (i.e. 9V) is the best way. A Zener diode and transistor (or the optocoupler) should be enough because the value of the threshold is not critical.