Arduino for BPI:bit 6 : how to use ESP32 EEPROM


(bpi team) #1

this will now share how to use EEPROM on the bpibit board.Using EEPROM to store and read data.

EEPROM (Electrically Erasable Programmable read only memory) refers to electric Erasable Programmable read-only memory. EEPROM can be used to solidify some data without the use of files and file systems, such as SSID or Password, user name and Password, user Settings and other data, which can achieve more complex applications.

The default EEPROM object size is 4096 bytes and the user operation address is 0~4095.

do the test with vscode + platformIO+ BPI:bit

main function

  • EEPROM.begin(size):open EEPROM

  • EEPROM.write(addr, data):Writes data to the storage space

  • EEPROM.commit(): This function needs to be called every time the address is written

  • EEPROM.read(addr):Read data from storage space

code

/*
该代码向 EEPROM 写入数据,然后再从 EEPROM 中读出来
*/
#include <EEPROM.h>

void setup() 
{
  Serial.begin(9600);
  Serial.println("Start write");

  EEPROM.begin(4096); //Request operation to address 4095 (for example, you only need to read and write a byte of address 100, and input parameter 101)
  for(int addr = 0; addr<4096; addr++)
  {
    int data = addr%256; //This is equivalent to int data = addr;Because the write method below is stored in bytes
    EEPROM.write(addr, data); //write data
  }
  EEPROM.commit(); //save data 。 also can use EEPROM.end()

  Serial.println("End write");

  for(int addr = 0; addr<4096; addr++)
  {
    int data = EEPROM.read(addr); //read data
    Serial.print(" ");
    delay(1);
    if((addr+1)%256 == 0) //Newline for every 256 bytes of data read
    {
      Serial.println("");
    }
  }
  Serial.println("End read");
}

void loop() 
{
}


Open the serial assistant. Remember to adjust the baud rate of the serial port assistant to 9600. Then press the reset button on the bpibit board to receive the complete information from the serial port assistant.