Arduino Projects Electronic Circuits

Arduino Power-Down & Auto-Save

Arduino Power-Down & Auto-Save-SuperCap ModulePerhaps 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.

Arduino Power-Down & Auto-Save-PD Adapter Circuit

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.

Arduino Power-Down & Auto-Save-Arduino Connection

// 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:

Arduino Power-Down & Auto-Save-SuperCap Module

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