Arduino Pro Mini 328 - EEPROM

EEPROM is an internal memory which is not disappeared (deleted) when power is out.
Arduino Pro Mini which use Atmega328 has 32 KiB of Flash program memory. 1KiB = 1024 bytes -> 32*1024 = 32,768bytes




Memory will have address and content. Address will from 0 to 32,767; in which each address is presented for each memory slot with 1byte.




Following is example code to write data from analog input to EEPROM, then read and show through COM port


#include <EEPROM.h>

/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int addr = 0;
byte value;

void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  /***
    Need to divide by 4 because analog inputs range from
    0 to 1023 and each byte of the EEPROM can only hold a
    value from 0 to 255.
  ***/
  int val = analogRead(0) / 4;

  EEPROM.write(addr, val);  //write value "val" to address "addr"

  delay(100);

  // read a byte from the current address of the EEPROM
  value = EEPROM.read(addr);

  // increasing address
  addr = addr + 1;
  if (addr == EEPROM.length()) {
    addr = 0;
  }

  //print value through COM port
  Serial.print(addr);
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();
}

Result will as:











1 comment:

  1. Address will go from 0 to 1023, not 32767. You confuse flash program space with EEPROM memory.

    ReplyDelete