I have tried below code to test the simple byte function and read the same.
Circuit is connected to port and I2C Scanner is detecting Chip @ 0x50 Address. Working @ 3.3V. WP on AT24C32 Pin is left open.
What else could be issue ?
Code: Select all
#include <Wire.h>
#define EEPROM_ADDRESS 0x50 // I2C address of the EEPROM
void setup() {
Wire.begin();
Serial.begin(9600);
}
void writeByteToEEPROM(byte data, int address) {
Wire.beginTransmission(EEPROM_ADDRESS); // Start communication with the EEPROM
Wire.write(address >> 8); // Send high byte of the address
Wire.write(address & 0xFF); // Send low byte of the address
Wire.write(data); // Send data byte
Wire.endTransmission(); // End communication with the EEPROM
}
byte readByteFromEEPROM(int address) {
byte data = 0;
Wire.beginTransmission(EEPROM_ADDRESS); // Start communication with the EEPROM
Wire.write(address >> 8); // Send high byte of the address
Wire.write(address & 0xFF); // Send low byte of the address
Wire.endTransmission(false); // End transmission with repeated start
Wire.requestFrom(EEPROM_ADDRESS, 1); // Request 1 byte from the EEPROM
if (Wire.available() >= 1) {
data = Wire.read(); // Read data byte
}
return data;
}
void loop() {
byte dataToWrite = 42; // Byte data to write to the EEPROM
int addressToWrite = 0; // Address in the EEPROM where you want to write the byte
writeByteToEEPROM(dataToWrite, addressToWrite); // Write the byte to the EEPROM
// Read the byte back from the EEPROM for verification
byte readData = readByteFromEEPROM(addressToWrite);
Serial.print("Data written to EEPROM: ");
Serial.println(dataToWrite);
Serial.print("Data read from EEPROM: ");
Serial.println(readData);
delay(1000);
}