#include <AT24Cxx.h>
#define i2c_address 0x50
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int address = 0;
AT24Cxx eep(i2c_address, 32); // Initialize using AT24CXX(i2c_address, size of EEPROM in KB).
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() {
// Check for incoming data
if (Serial.available() > 0) {
// Read binary data from the serial port and write to EEPROM
while (Serial.available()) {
int val = Serial.read();
Serial.println(val);
// Update the particular EEPROM cell
eep.update(address, val);
// Read the value back from EEPROM for verification
int readVal = eep.read(address);
// Check if the read value matches what was written
if (readVal == val) {
Serial.println("Write to EEPROM successful.");
} else {
Serial.println("Write to EEPROM failed.");
// You can add additional error handling here
}
// Advance to the next address, when at the end restart at the beginning
address = (address + 1) % eep.length();
delay(100); // Delay to prevent flooding the serial port
}
Serial.println("Write operation complete.");
}
}
The deviceAddress/SlaveAddress of my 24C32 is: 0x57 (1010111). I have tested the following sketch on 24C32 EEPROM located on the DS3231 RTC Module and it is working fine. (Arduino UNO)
The above is a possible device address for 24C32 EEPROM deendong on the logic values of the address selection bits (A2-A0). Change the address to 0x57 and rerun your sketch. It should work. (Arduino UNO)
I have uploaded your sketch and is working. This is the output.
49
Write to EEPROM successful.
50
Write to EEPROM successful.
Write operation complete.
51
Write to EEPROM successful.
52
Write to EEPROM successful.
Write operation complete.
Thanks for the answers, but I tried running an I2C scanner, but it's not picking up anything. I tried running your script, I tried running mine with 0x57 and it still fails .
I changed the pins to digital 2 and 3, then to upper SDA and SCL, still didn't work.
Are you using a stand-alone 24C32? If yes, consult data sheets and set an address for the device by the HIGH/LOW logic of the A2-A0 address lines (Fig-1).
No word about this in the datasheet, it is only mentioned that the input on those pins select the I2C address. I strongly suspect those lines must be tied to either Vcc or GND, and can not be left floating. If in doubt: never let an input float...