1

I am building a project where I have to store the digital state which is Boolean (either HIGH or LOW) of each I/O port such that after power off the state of I/O ports store in EEPROM and I can retrieve this I/O port's state after power on.

This is my code..

    #include <IRremote.h>
    #include <EEPROM.h>
    int EEPROMaddress = 0;
    int EEPROMaddress1 = 1;
    byte EEPROMbyte = EEPROM.read(EEPROMaddress);
    byte EEPROMbyte1 = EEPROM.read(EEPROMaddress1);
    int RECV_PIN = 11;
    int led = 10;//1FE50AF
    int led1 = 9;//1FED827 
    boolean previousState = bitRead(EEPROM.read(EEPROMaddress), led);
    boolean previousState1 = bitRead(EEPROM.read(EEPROMaddress1), led1);
    boolean state;
    boolean state1;
    IRrecv irrecv(RECV_PIN);
    decode_results results;
    void setup()
    {
      Serial.begin(9600);
      pinMode(10,OUTPUT);
      pinMode(9,OUTPUT);
      irrecv.enableIRIn();
    }
    void loop() {
      if (irrecv.decode(&results)) 
      {
        Serial.println(results.value, HEX);
        switch(results.value)
        {
           case 0x1FE50AF:
           if (previousState == LOW)
           {state = HIGH;
            digitalWrite(led,state);
           previousState=state;
           bitWrite(EEPROMbyte,led,state);
           EEPROM.update(EEPROMaddress,EEPROMbyte);
           delay(50);}
           else
           {state=LOW;
           digitalWrite(led,state);
           previousState=state;
           bitWrite(EEPROMbyte,led, state);
           EEPROM.update(EEPROMaddress,EEPROMbyte);
           delay(50);
           }
           break;
            case 0x1FED827:
           if (previousState1 == LOW)
           {state1=HIGH;
           digitalWrite(led1,state1);
           previousState1=state1;
           bitWrite(EEPROMbyte1,led1, state1);
           delay(50);}
           else
           {state1=LOW;
           digitalWrite(led1,state1);
           previousState1=state1;
           bitWrite(EEPROMbyte1,led1,state1);
           delay(50);}
           break;
}   
        irrecv.resume(); 
      }
}
4
  • Out of curiosity, how are you going to do this after power off? And if you do it before power off, the EEPROM has a limited number of writes it can do.
    – Nick Gammon
    Commented Jan 14, 2017 at 6:55
  • Are these input or output ports you want to store the state of?
    – Nick Gammon
    Commented Jan 14, 2017 at 6:56
  • In my code, the state of each arduino's pin will change randomly within a loop and present(changed) state of each pins will store in EEPROM parallely. So that if power failure will occurs any instance of time during running this code then after power on I can get the each pin's state from EEPROM which were before shutdown. Commented Jan 14, 2017 at 14:55
  • 1
    The chip on the Uno is designed for 100,000 writes to EEPROM. That's a lot, but if you are doing it every time around a loop, you may exceed that number in a couple of minutes.
    – Nick Gammon
    Commented Jan 14, 2017 at 20:53

3 Answers 3

3

Since this is Arduino SE I'm going to do this with the Arduino functions, there are other ways of course.

Write a bit to EEPROM:

byte EEPROMbyte = EEPROM.read(EEPROMaddress);
bitWrite(EEPROMbyte, pinBit, pinState);
EEPROM.update(EEPROMaddress, EEPROMbyte);

Read a bit from EEPROM:

boolean pinState = bitRead(EEPROM.read(EEPROMaddress), pinBit);

This is for updating/reading a single bit at a time as asked. If you need to read/write multiple bits at the same time(as I'd assume you are doing after power on) then it's more efficient(and less wear on the EEPROM in the case of the write) to do all the bits at once instead of one EEPROM read/write per bit.

I assume by "I/O ports" you mean Arduino pins. If you're working directly with the PORT registers(e.g. PORTB) then you can just write or read the whole register to the EEPROM, no need to deal with the individual bits.

9
  • Thank you Sir for your great contribution. In my code, the state of each arduino's pin will change randomly within a loop and present(changed) state of each pins will store in EEPROM parallely. So that if power failure will occurs any instance of time during running this code then after power on I can get the each pin's state from EEPROM which were before shutdown. have some doubt which EEPROM library need to run these code which you given in your comment? Commented Jan 14, 2017 at 15:05
  • It's the EEPROM library included with the Arduino AVR Boards core but there should be a similar API for any hardware core for an MCU with EEPROM(or pseudo-EEPROM in the case of ESP8266). Which board are you using?
    – per1234
    Commented Jan 14, 2017 at 17:53
  • I am using arduino uno board.. .after successful implication in this board I will implement it on AVR atmega 32 MCU later Commented Jan 14, 2017 at 18:48
  • You just need to add #include <EEPROM.h> to the top of the sketch and the code I wrote will work fine. The core you use for ATmega32 will probably either have the same library or reference the Arduino AVR Boards core so it will work either way. If it doesn't you can use avr-libc's avr/eeprom.h library, you just need to change EEPROM.read/EEPROM.update to eeprom_read_byte/eeprom_update_byte. Keep in mind that the ATmega328P/ATmega32's EEPROM is only rated for 100000 writes/cell so you need to do some calculations of the expected lifetime and maybe do some wear leveling.
    – per1234
    Commented Jan 14, 2017 at 23:53
  • Sir I have written my sketch as per your instruction but the memory part is not functioning.. If I make any particular ardunio's pin HIGH before power off after power on it goes to LOW but it should be HIGH.. I have Added my Ardunio's Sketch in my Post Please check it. Thank you Commented Jan 18, 2017 at 7:24
0

Note that writing EEPROM is slow - on the order of milliseconds per write - which may limit the performance of your sketch.

A better technique is to provide a limited backup power supply to the MCU; enough to complete a set of EEPROM writes + a safety margin. A capacitor could possibly provide enough run time if doesn't have to power much else. Then, detect and interrupt on a failure of the main supply, immediately write your backup data to EEPROM, and enter an infinite loop to wait for the backup supply to fail. When the system resets, examine the EEPROM to be sure there is valid backup data there, restore what needs to be restored, and resume your task.

Update:

A search for "arduino backup power" finds a number of articles related to switching an Arduino from mains power to battery power when the mains fails. You would only need a very small battery if you're willing to only save necessary information then shut down until the power returns. A capacitor might keep your MCU running long enough to do that. Designing the circuit depends on too much we don't know about your system, though.

In fact, simply switching a backup battery as some of these articles describe, or better, running on a battery which is kept on a mains charger, may make a much simpler system that can surviVe reasonably long power outages without shutting down. Again, we don't know how often or how long your power goes out, and how reliable your system is required to be.

1
  • Thank you sir for your suggestion. But I understand your suggestion but its not clear to me how it will done. Actually I am beginner in this field.. so my request to you please explain me details with an example whatever you wanted to say.. then it will be very helpful to me. Commented Jan 18, 2017 at 6:29
0

You can store any bits or bytes into eeprom in byte units. The eeprom itself doesn't care whether the data ISS bits or bytes or multi bytes.

Your code only needs to make sure that consistency is maintained in storying and retrieving of eeprom data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.