){.nav-menu .menu-item-has-children a{padding-right:20px}.nav-menu .menu-item-has-children a:after{position:absolute;right:7px;top:10}}.vce-ad-container{margin:30px auto;display:block;clear:both;width:728px;position:relative;max-width:100%}.vce-ad-below-header{margin-bottom:0}@media only screen and (max-width:1200px) and (min-width:1024px){.vce-ad-container{width:630px}.vce-ad-below-header{width:728px}}@media only screen and (max-width:1023px){.vce-ad-below-header,.vce-ad-container{width:728px}}@media (max-width:767px){.vce-ad-container{width:375px}}@media (max-width:400px){.vce-ad-container{width:300px}}@media (max-width:320px){.vce-ad-container{width:280px;max-width:100%}}:root .main-navigation ul ul{opacity:1}@media only screen and (min-width:1024px) and (max-width:1200px){.container{max-width:1000px}.vce-main-content{width:700px}.main-navigation ul ul{display:none}.top-header{overflow:hidden}.main-navigation a{padding:15px 9px 15px}.search-header-wrap a{padding:15px}.header-3-wrapper .site-branding{max-width:300px}h1.entry-title{width:540px}.entry-title{display:block}}@media only screen and (min-width:670px) and (max-width:1023px){.header-3-wrapper .site-branding{max-width:300px}.header-3-wrapper .nav-menu>li>a{padding:0 15px}h1.entry-title{width:540px}.entry-title{display:block}}@media only screen and (max-width:1023px){.container{max-width:90%}.vce-main-content{width:100%;padding:15px 0!important}.main-navigation ul ul{display:none}.vce-res-nav{float:left;position:absolute;left:0}.header-3-wrapper{height:50px!important;padding-top:0!important;text-align:center}.header-3-wrapper .site-branding{position:relative;float:none;top:0!important;left:initial!important;text-align:center;max-width:initial;padding:3px 0 7px}.site-branding{padding:4px 0 7px;display:inline-block}.site-branding img{max-width:120px;max-height:28px}.main-header .header-3-wrapper .site-title,.main-header .header-3-wrapper .site-title a.has-logo{line-height:40px!important;font-size:26px!important}.sidebar{margin:0 auto 20px;float:none!important;display:block;clear:both;padding-top:0!important;width:100%;max-width:300px}h1.entry-title{max-width:100%}.vce-responsive-nav{display:block;margin:10px 0 9px}.nav-menu,.top-header{display:none}.main-header .site-title,.main-header .site-title a{line-height:36px!important;font-size:40px!important}li>ul{margin:0}}@media only screen and (min-width:200px) and (max-width:670px){html{margin-top:0!important;overflow-x:visible}html body,button,input,textarea{font-size:14px;line-height:22px}.container{max-width:95%}.vce-main-content{padding:0 0 10px!important}.main-box,.sidebar .widget{margin-bottom:10px}.vce-wrap-right{width:100%;max-width:100%;text-align:center}.vce-single .entry-header{margin:20px 0}.vce-wrap-right{text-align:center}body h1.entry-title{font-size:24px;line-height:30px;padding:0 20px}body p{margin-bottom:20px}body h1{font-size:24px;line-height:34px;margin-bottom:10px}body h4{font-size:18px;line-height:28px;margin-bottom:10px}.sidebar .widget-title{line-height:22px}.vce-wrap-right>ul{float:none;display:inline-block;margin:10px 0 0}}#cookie-notice *{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}#cookie-notice .cookie-notice-container{display:block}#cookie-notice.cookie-notice-hidden .cookie-notice-container{display:none}.cookie-notice-container{padding:15px 30px;text-align:center;width:100%;z-index:2}.cn-close-icon{position:absolute;right:15px;top:50%;margin-top:-10px;width:15px;height:15px;opacity:.5;padding:10px;outline:none}.cn-close-icon:before,.cn-close-icon:after{position:absolute;content:' ';height:15px;width:2px;top:3px;background-color:#fff}.cn-close-icon:before{transform:rotate(45deg)}.cn-close-icon:after{transform:rotate(-45deg)}#cookie-notice .cn-button{margin:0 0 0 10px}.cn-button.bootstrap{font-family:-apple-system,BlinkMacSystemFont,Arial,Roboto,"Helvetica Neue",sans-serif;font-weight:400;font-size:13px;letter-spacing:.25px;line-height:20px;margin:0;text-align:center;text-transform:none;display:inline-block;touch-action:manipulation;white-space:nowrap;outline:none;box-shadow:none;text-shadow:none;border:none;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;text-decoration:none;padding:8.5px 10px;line-height:1}.cn-button.bootstrap{color:#fff;background:#20C19E}.cn-text-container{margin:0 0 6px 0}.cn-text-container,.cn-buttons-container{display:inline-block}@media all and (max-width:900px){.cookie-notice-container #cn-notice-text{display:block}.cookie-notice-container #cn-notice-buttons{display:block}#cookie-notice .cn-button{margin:0 5px 5px 5px}}@media all and (max-width:480px){.cookie-notice-container{padding:15px 25px}}

Arduino Power-Down & Auto-Save

Perhaps you will try tomorrow one microcontroller experiment that can store variable data in its EEPROM (electrically erasable programmable read only memory). Presented here is an idea to use inbuilt EEPROM of Arduino to save data between power cycles. EEPROM is a type of non-volatile/flash memory simply stores data even with its power removed. Fortunately, all Arduino models have inbuilt EEPROM available, and it is incredibly useful because it remembers data even when the power is unavailable, you could store the state of the Arduino. This guide is a minimalistic approach to detect sudden power down and automatically store a variable to an EEPROM address.

Add-On Hardware

First of all, see the simple add-on hardware setup for Arduino Uno. Note that this approach of course works for other models of Arduino as well. The hardware is in fact nothing but a regulated linear power supply backed by a super capacitor, with a crude power-down detector circuit. The linear power supply is used to deliver regulated 5V dc to the Arduino board while the power-down detector is wired to give a steady high-level input to one interrupt pin of Arduino as long as the input power is available. During a power-down condition an Interrupt Service Routine (ISR) is triggered that stores the variable to a pre-defined address of the EEPROM. The super capacitor ensures that the Arduino has enough power available to safely detect the power-down state and store the requisite ‘volatile’ variable to the EEPROM.

First Hardware Test

For testing your add-on hardware, simply complete the setup as pointed below. Next, upload the test code to Arduino, and power up the entire setup. Since the input power is available, connected INT pin of Arduino Uno ‘sees’ a high-level voltage  (greater than 3V) and nothing takes place until you switch off input power to the hardware. If switched off, you can see that the onboard LED (D13) of Arduino starts flashing to indicate a power-down condition.

// Power-Down Add-On Hardware Test Code
const byte ledPin = 13; // Onboard LED
const byte interruptPin = 2; //D2
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT); //D13 as Output
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, FALLING); // ISR
}
void loop() {
digitalWrite(ledPin, state); // Enable LED
}
void blink() {
state = !state;
}

Backup Time Calculation

It’s assumed that Arduino will work until 5V from the 1F/5.5V super capacitor drops to about 4V, and the maximum current demand is well below 100mA. My Arduino Uno (16MHz/5V) used for the test setup itself took about 48mA of current. If so the ‘theoretical’ expected backup time extends up to 10 seconds (t= ∆VC/I  =  (5V-4V) x1F / 100mA = 10s) – enough well to store a variable. Remember, peripherals (if used) are in switched off state at power-down as they are wired here on a separate 12V power rail.

EEPROM Read & Write

Now let’s look at how to write and read some data in real-world. Here is a half-baked example code:

// Save variables automatically to Arduino EEPROM on power-down

#include <EEPROM.h> // Library comes with Arduino IDE

int SomeVariable; // Some Variable 
int PD_PIN =2; // D2 used for  power-down detection (INT.0)

int EE_ADDR = 0; // The EEPROM address to be used

void setup() {

  EEPROM.get(EE_ADDR, SomeVariable);   // Retrieve last stored value of SomeVariable from EEPROM
 
  attachInterrupt(digitalPinToInterrupt(PD_PIN), PD_ISR,FALLING); // Set-up Interrupt Service Routine (ISR)
}

void loop() {
  // Do something great
  }

void PD_ISR () { // ISR to be get called on power-down state
   
    EEPROM.put(EE_ADDR, SomeVariable); // Put SomeVariable to the EEPROM
    
}

For more details, do refer EEPROM examples in the Arduino IDE as it’s good and worthful indeed (practice makes perfect)!

& finally, the “super capacitor module” prepared by me for the experiments:

Wrapping Up

With Arduino, the built-in EEPROM is a pretty cool way to store data permanently. The Arduino boards (Atmega328P) have an emulated EEPROM space of 1024 bytes. Each EEPROM position can save one byte, i.e. you can only store an 8-bit number (you cannot write letters) which includes integer values between 0 and 255. Keep note that you don’t write too often to the EEPROM as it has a limited lifetime (erasing memory also is a writing operation). Interesting to note is that Arduino’s EEPROM is specified to handle 100,000 write/erase cycles for each position i.e. each memory cell of the EEPROM can be accessed a hundred thousand times before burnout.

Functions:

  • EEPROM Clear = Clear the bytes in the EEPROM.
  • EEPROM Read = Read the EEPROM and send its values to the computer.
  • EEPROM Write = Store values from an analog input to the EEPROM.
  • EEPROM Get = Get values from EEPROM and prints as float on serial.
  • EEPROM Put = Put values in EEPROM using variable semantics.
  • EEPROM Update = Store values read from A0 into EEPROM, writing the value only if different, to increase EEPROM life.

The EEPROM documentation on the Arduino website has lots more examples (https://www.arduino.cc/en/Reference/EEPROM)

Leave a Comment X

18 Comments

  • Thanks for sharing this. Exactly what I was looking for. I’ve built it on breadboard and it works great. I think I’ll get a PCB made up for my project.

    • @Luke Hurst: Thank you for your valuable feedback. Appreciate it and I’m glad to hear that it’s of value for you!

      PCB for this small project is not designed yet as I tested it on a bit of perforated circuit board. If you’ve designed a special PCB for you, I’ll be happy to know more on that. Cheers!

      • Hi. I’ve designed the PCB and had some made. It works great. Drop me an email and I will share details with you.

        • Luke Hurst:
          That’s nice! I’m especially interested to see how it came out. Could you take pictures of your PCB and send them my way (designlab.technode@gmail.com). I’d be happy to link to it on this page. Thanks!

          • Hi Luke & T.K.
            Good job, thanks.
            Would like to build one too. Can I buy the PCB from you Luke.

            Henk Wijtman
            Rotterdam
            the Netherlands

  • I connected output from my 5vdc power supply per schematics, did not install D1 diode though, everything else the same per your drawing on the output.
    I see voltage drop to 4.2vdc at pin 5v.
    Also I have about 300-350mA draw from the power supply, as I have display connected.
    Any idea?
    Thanks
    Rock

    • @Rock: If you want to use an external dc power supply, just omit 7805 (and the surrounding parts) and feed around 5.7VDC input (not 5V) across C2. If so, you’ll get circa 5VDC at the output.

      I’m afraid, the back up supply from the supercapacitor C4 is not amble enough to support the LCD in power down mode. Thanks!

      • That is what I thought.
        Even 4.2v for VCC will do the job, just reference that voltage to analog readings and I should be fine, correct? without having to re design the supply voltage.
        Regarding the super capacitor, what do you think correct capacitance should be?
        I did try 3f and it does not seem enough, any idea? there is around 400mA current draw to be precise.
        Many thanks for the reply.
        Rock