#include <Wire.h>
#include "AT24CXX.h"
#define EEPROM_I2C_ADDRESS 0x50
// Define the size of your EEPROM chip in KiB (Kilobytes)
// e.g., 32 for AT24C32, 256 for AT24C256
#define EEPROM_SIZE_KB 32
// Initialize the library object
// The constructor usually takes the I2C address and the size in KB
AT24Cxx eep(EEPROM_I2C_ADDRESS, EEPROM_SIZE_KB);
void setup() {
Serial.begin(9600);
while (!Serial); // Wait for Serial Monitor to open
Wire.begin(); // Start the I2C bus
Serial.println("\nAT24Cxx EEPROM Basic Test");
// --- Writing data ---
int address = 0; // Start address for writing
byte dataToWrite = 42; // The data byte to store (value 0-255)
Serial.print("Writing value ");
Serial.print(dataToWrite);
Serial.print(" to address ");
Serial.print(address);
Serial.println("...");
// Use the library's write function (might be writeByte or similar depending on the specific library version)
// Check your specific library documentation for the exact function name if 'write' doesn't work.
// The common functions are `write` for bytes and `put`/`get` for larger types.
eep.write(address, dataToWrite);
delay(10); // A small delay is required after each write operation for the EEPROM to complete the cycle
// --- Reading data ---
byte dataRead = eep.read(address); // Read the byte from the same address
Serial.print("Reading from address ");
Serial.print(address);
Serial.print(": ");
Serial.println(dataRead);
// Verification
if (dataRead == dataToWrite) {
Serial.println("Data successfully written and read back!");
} else {
Serial.println("Error: Data mismatch!");
}
}
void loop() {
// Nothing needed in the loop for this simple example
}