Go back

I2C EEPROM Read/Write with Arduino: A Complete Guide

Introduction

Have you ever needed to store configuration data or small amounts of non-volatile information in your Arduino projects? I2C EEPROMs are a perfect solution—they’re cheap, easy to use, and require only two wires for communication. In this tutorial, we’ll use the Velxio in-browser simulator to build a complete I2C EEPROM read/write test. You’ll learn how to write data to a virtual EEPROM at address 0x50 and read it back, verifying that everything works.

Velxio provides a SPICE-accurate analog simulation environment, so you can test your code without any physical hardware. Let’s dive in!

Circuit Walkthrough

Circuit diagram of i2c-eeprom-rw on the Velxio simulator canvas

The circuit is remarkably simple because the I2C EEPROM is integrated into the virtual environment. The only component we need is an Arduino Uno. The EEPROM is simulated internally and connected to the Arduino’s I2C pins (A4 for SDA and A5 for SCL). No external wiring is required—the simulator handles the I2C bus automatically.

To set up the circuit:

  1. Open the Velxio editor and add an Arduino Uno from the component picker.
  2. The I2C EEPROM is already part of the simulation; you don’t need to add it manually.
  3. The code will communicate with the EEPROM at address 0x50.

Velxio component picker modal browsing parts by category

You can find the Arduino Uno under the “Microcontrollers” category in the component picker. Once placed, the canvas will show the board ready for programming.

Code Walkthrough

Let’s examine the Arduino sketch that performs the read/write operations. The code is written in C++ using the standard Wire library for I2C communication.

// I2C EEPROM Read/Write Test
// Virtual EEPROM at address 0x50
// Writes values to registers, then reads them back.

#include <Wire.h>

#define EEPROM_ADDR 0x50

void writeEEPROM(byte reg, byte value) {
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write(reg);    // register address
  Wire.write(value);  // data
  Wire.endTransmission();
  delay(5); // EEPROM write cycle time
}

byte readEEPROM(byte reg) {
  Wire.beginTransmission(EEPROM_ADDR);
  Wire.write(reg);
  Wire.endTransmission();

  Wire.requestFrom(EEPROM_ADDR, 1);
  if (Wire.available()) {
    return Wire.read();
  }
  return 0xFF;
}

void setup() {
  Wire.begin();
  Serial.begin(9600);

  Serial.println("============================");
  Serial.println(" I2C EEPROM R/W Test (0x50)");
  Serial.println("============================");
  Serial.println();

  // Write test pattern
  Serial.println("Writing test data...");
  for (byte i = 0; i < 8; i++) {
    byte value = (i + 1) * 10;  // 10, 20, 30, ...
    writeEEPROM(i, value);
    Serial.print("  Write reg[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.println(value);
  }

  Serial.println();
  Serial.println("Reading back...");

  // Read back and verify
  byte errors = 0;
  for (byte i = 0; i < 8; i++) {
    byte expected = (i + 1) * 10;
    byte actual = readEEPROM(i);
    Serial.print("  Read  reg[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.print(actual);

    if (actual == expected) {
      Serial.println("  [OK]");
    } else {
      Serial.print("  [FAIL] expected ");
      Serial.println(expected);
      errors++;
    }
  }

  Serial.println();
  if (errors == 0) {
    Serial.println("All tests PASSED!");
  } else {
    Serial.print(errors);
    Serial.println(" test(s) FAILED.");
  }
}

void loop() {
  // Nothing to do
  delay(1000);
}

How the Code Works

Key Concepts for Students

  1. I2C Protocol: I2C uses two wires (SDA and SCL) and a 7-bit address to communicate with multiple devices. The EEPROM address 0x50 is common for many AT24C-series chips.
  2. EEPROM Write Cycle: EEPROMs require a few milliseconds to write data internally. The delay(5) in writeEEPROM() ensures the write completes before the next operation.
  3. Dummy Write for Read: To read from a specific register, you first send the register address (a write operation), then issue a repeated start or stop/start to read the data. The code uses a stop/start approach.
  4. Error Checking: The sketch verifies each read against the expected value, which is a good practice for testing communication.

Common Pitfalls and Debugging Tips

Velxio editor toolbar with compile and run controls

Use the toolbar to compile and run your code. The Serial Monitor will display the test results.

Suggested Extensions

  1. Write Larger Data: Modify the code to write and read a string or an array of bytes.
  2. Page Write: Many EEPROMs support page writes (writing multiple bytes in one transaction). Try implementing that.
  3. External EEPROM: Add a physical EEPROM component (if available in Velxio) and wire it to the Arduino.
  4. Data Logging: Use the EEPROM to store sensor readings and retrieve them later.

Conclusion

You’ve successfully built an I2C EEPROM read/write test in Velxio! This project demonstrates the fundamentals of I2C communication and non-volatile memory. The virtual environment makes it easy to experiment without hardware. Now, try extending the code or integrating it into a larger project.

Ready to build your own? Open the live example in Velxio and start coding: I2C EEPROM Read/Write Example. Happy simulating!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
SPI Loopback Test: Master the Protocol with Velxio
Next Post
I2C RTC Clock with DS1307 on Velxio