Search code examples
memoryarduinoeeprom

What's the difference between EEPROM.read(address) and EEPROM[address]


I'm using EEPROM on Arduino to store some large constant array. I noticed that both EEPROM.read(address) and EEPROM[address] works for my reading. But there are few documentations on the EEPROM[address] method. I also experienced occasional memory crash with that method.

EEPROM.read(address) has not been fully tested for long run. It does take more storage space when compiling. Is it safer for its behavior behind the scene?


Solution

  • EEPROM.read(adress) ->Read the EEPROM (address starting form 0)and send its value as unsigned char.

    EEPROM[adress] -> reference eeprom cell with address

    To reduce the size of the you can use avr/eeprom library , which has various function and macros for the eeprom usage. This is a reliable library and well tested. avr/eeprom.h

    Sample Code

    #include <EEPROM.h>
    #include <avr/eeprom.h>
    
    void Eepromclr();
    
    void setup() {
    
      Serial.begin(9600);
    
      eeprom_write_byte((void*)0,12);
      int x = eeprom_read_byte((void*)0);\
      Serial.println(x);
    
      Eepromclr();
    
      eeprom_update_byte((void*)0,6);
      int y = eeprom_read_byte((void*)0);
      Serial.println(y);
    
    }
    void loop() {
    
    }
    
    void Eepromclr() {
      for (int i = 0 ; i < EEPROM.length() ; i++) {
      EEPROM.write(i, 0);
    }
    Serial.println("Eeprom is cleared");
    }