Skip to content

Instantly share code, notes, and snippets.

@hed0rah
Created March 6, 2026 23:43
Show Gist options
  • Select an option

  • Save hed0rah/54246fe785310dca895cc7a4a27506fd to your computer and use it in GitHub Desktop.

Select an option

Save hed0rah/54246fe785310dca895cc7a4a27506fd to your computer and use it in GitHub Desktop.
eeprom size detect with mirror method
#include <Wire.h>
#define EEPROM_I2C_ADDRESS 0x50
void setup() {
Serial.begin(9600);
delay(1000);
// init i2c bus
Wire.begin();
Serial.println("=== EEPROM Size Detector (Mirror/Wrap-around Method) ===");
// save original byte at address 0
byte original = readEEPROM(0);
Serial.print("Original byte at address 0: 0x");
Serial.println(original, HEX);
// write a unique test patter
writeEEPROM(0, 0xA5);
unsigned long detectedSize = 0;
const unsigned long MAX_CHECK = 131072UL; // 128 KB — safety cap ??
// test typical power-of-2 sizes where mirroring would show up
for (unsigned long addr = 128; addr <= MAX_CHECK; addr *= 2) {
if (readEEPROM(addr) == 0xA5) {
detectedSize = addr;
break;
}
}
// restore original value
writeEEPROM(0, original);
if (detectedSize > 0) {
Serial.print("Detected EEPROM size: ");
Serial.print(detectedSize);
Serial.print(" bytes (");
Serial.print(detectedSize / 1024UL);
Serial.println(" KB)");
Serial.println("This is the most common result for 24xx series chips.");
} else {
Serial.println("No mirroring detected up to 128 KB.");
Serial.println("Check connections, I2C address, or try a higher MAX_CHECK.");
}
Serial.println("\nDone. Your data at address 0 is restored.");
}
void loop() {
// null
}
// write 1 byte
void writeEEPROM(unsigned int addr, byte data) {
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((byte)(addr >> 8)); // MSB
Wire.write((byte)(addr & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(6); // wait for internal write cycle
}
// read one byte
byte readEEPROM(unsigned int addr) {
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((byte)(addr >> 8)); // MSB
Wire.write((byte)(addr & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(EEPROM_I2C_ADDRESS, (byte)1);
if (Wire.available()) {
return Wire.read();
}
return 0xFF;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment