Using the 24LC32 EEPROM with Arduino involves wiring up the sensor to your Arduino-compatible microcontroller, installing the Adafruit_FRAM_I2C library and running the provided example code.

Wiring

Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the 24LC32 VIN.

Here is an Adafruit Metro wired up to the 24LC32 using the STEMMA QT connector:

  • Board 5V to EEPROM VIN (red wire)
  • Board GND to EEPROM GND (black wire)
  • Board SCL to EEPROM SCL (yellow wire)
  • Board SDA to EEPROM SDA (blue wire)

Here is an Adafruit Metro wired up using a solderless breadboard:

  • Board 5V to EEPROM VIN (red wire)
  • Board GND to EEPROM GND (black wire)
  • Board SCL to EEPROM SCL (yellow wire)
  • Board SDA to EEPROM SDA (blue wire)

Library Installation

You can install the Adafruit EEPROM & FRAM I2C library for Arduino using the Library Manager in the Arduino IDE.

Click the Manage Libraries ... menu item, search for Adafruit FRAM I2C, and select the Adafruit FRAM I2C library:

If asked about dependencies, click "Install all".

If the "Dependencies" window does not come up, then you already have the dependencies installed. 

If the dependencies are already installed, you must make sure you update them through the Arduino Library Manager before loading the example!

Example Code

#include "Adafruit_EEPROM_I2C.h"

/* Example code for the Adafruit I2C EEPROM breakout */

/* Connect SCL    to SCL
   Connect SDA    to SDA
   Connect VDD    to 3 - 5V DC
   Connect GROUND to common ground */
   
Adafruit_EEPROM_I2C i2ceeprom;

#define EEPROM_ADDR 0x50  // the default address!

void setup(void) {
  Serial.begin(115200);
  
  if (i2ceeprom.begin(0x50)) {  // you can stick the new i2c addr in here, e.g. begin(0x51);
    Serial.println("Found I2C EEPROM");
  } else {
    Serial.println("I2C EEPROM not identified ... check your connections?\r\n");
    while (1) delay(10);
  }
  
  // Read the first byte
  uint8_t test = i2ceeprom.read(0x0);
  Serial.print("Restarted "); Serial.print(test); Serial.println(" times");
  // Test write ++
  test++;
  i2ceeprom.write(0x0, test);

  // Try to determine the size by writing a value and seeing if it changes the first byte
  Serial.println("Testing size!");

  uint32_t max_addr;
  for (max_addr = 1; max_addr < 0xFFFF; max_addr++) {
    if (i2ceeprom.read(max_addr) != test)
      continue; // def didnt wrap around yet

    // maybe wraped? try writing the inverse
    if (! i2ceeprom.write(max_addr, (byte)~test)) {
        Serial.print("Failed to write address 0x");
        Serial.println(max_addr, HEX);
    }

    // read address 0x0 again
    uint8_t val0 = i2ceeprom.read(0);

    // re-write the old value
    if (! i2ceeprom.write(max_addr, test)) {
        Serial.print("Failed to re-write address 0x");
        Serial.println(max_addr, HEX);
    }    

    // check if addr 0 was changed
    if (val0 == (byte)~test) {
      Serial.println("Found max address");
      break;
    }
  }
  Serial.print("This EEPROM can store ");
  Serial.print(max_addr);
  Serial.println(" bytes");
    
  // dump the memory
  uint8_t val;
  for (uint16_t addr = 0; addr < max_addr; addr++) {
    val = i2ceeprom.read(addr);
    if ((addr % 32) == 0) {
      Serial.print("\n 0x"); Serial.print(addr, HEX); Serial.print(": ");
    }
    Serial.print("0x"); 
    if (val < 0x10) 
      Serial.print('0');
    Serial.print(val, HEX); Serial.print(" ");
  }
}

void loop(void) {

}

Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You should see that the sketch has found your connected I2C EEPROM and the maximum storage size for the EEPROM in bytes. Then, the byte arrays stored on the EEPROM are printed to the Serial Monitor.

Read and Write

#include "Adafruit_EEPROM_I2C.h"
#include "Adafruit_FRAM_I2C.h"

/* Example code for the Adafruit I2C EEPROM/FRAM breakout */

/* Connect SCL    to SCL
   Connect SDA    to SDA
   Connect VDD    to 3 - 5V DC
   Connect GROUND to common ground */
   
Adafruit_EEPROM_I2C i2ceeprom;
//Adafruit_FRAM_I2C i2ceeprom;

#define EEPROM_ADDR 0x50  // the default address!

void setup(void) {
  Serial.begin(115200);
  
  if (i2ceeprom.begin(0x50)) {  // you can stick the new i2c addr in here, e.g. begin(0x51);
    Serial.println("Found I2C EEPROM");
  } else {
    Serial.println("I2C EEPROM not identified ... check your connections?\r\n");
    while (1) delay(10);
  }

  float f = 3.141592;
  uint8_t buffer[4];  // floats are 4 bytes!
  memcpy(buffer, (void *)&f, 4);
  
  Serial.println("Writing float to address 0x00");
  i2ceeprom.write(0x00, buffer, 4);

  i2ceeprom.read(0x00, buffer, 4);
  memcpy((void *)&f, buffer, 4);
  Serial.print("Read back float value: ");
  Serial.println(f, 8);
}

void loop(void) {

}

Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You should see that the sketch has found your connected I2C EEPROM. Then, a float is written to the EEPROM's address 0x00 and is read back from the EEPROM.

This guide was first published on Sep 20, 2022. It was last updated on Apr 17, 2024.

This page (Arduino) was last updated on Apr 17, 2024.

Text editor powered by tinymce.