I am trying to write and read data to an 24LC256, I2C EEPROM along with my Arduino Leonardo. The following code is copied from a video tutorial found at youtube Arduino Tutorial #8: I2C Communication - YouTube
#include <Wire.h>
#define ADDRESS 0x50
void setup()
{
Serial.begin(9600);
Wire.begin();
delay(15);
eepromWrite(0,0,125); //write 125 to external rom at address 00
delay(5);
Serial.println(eepromRead(0,0)); //read from external eeprom at address 00
}
void loop()
{
}
void eepromWrite(byte highAddress, byte lowAddress, byte data)
{
Wire.beginTransmission(ADDRESS);
Wire.write(highAddress);
Wire.write(lowAddress);
Wire.write(data);
Wire.endTransmission();
}
byte eepromRead(byte highAddress, byte lowAddress)
{
Wire.beginTransmission(ADDRESS);
Wire.write(highAddress);
Wire.write(lowAddress);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,1); //request one byte of data from eeprom at address 0x50
while(!Wire.available())
{
}
return Wire.read();
}
When running the code, at the serial monitor i have no output at all. I ran an I2C scanner and i get the message
I2C scanner. Scanning ...
Found address: 80 (0x50)
Done.
Found 1 device(s).
which means that the chip is connected correctly. I tried various sketches for reading/writing to the eeperom but the closest thing to a result that appeared at the serial monitor was with this code.
#include <Wire.h>
void eepromWrite(byte address, byte source_addr, byte data) {
Wire.beginTransmission(address);
Wire.write(source_addr);
Wire.write(data);
Wire.endTransmission();
}
byte eepromRead(int address, int source_addr) {
Wire.beginTransmission(address);
Wire.write(source_addr);
Wire.endTransmission();
Wire.requestFrom(address, 1);
if(Wire.available())
return Wire.read();
else
return 0xFF;
}
void setup() {
Wire.begin();
Serial.begin(9600);
for(int i = 0; i < 10; i++) {
eepromWrite(B01010000, i, 'a'+i);
delay(100);
}
Serial.println("Bytes written to external EEPROM !");
}
void loop() {
for(int i = 0; i < 10; i++) {
byte val = eepromRead(B01010000, i);
Serial.print(i);
Serial.print("\t");
Serial.print(val);
Serial.print("\n");
delay(1000);
}
}
but then again the Serial monitor was showing the following data
1 105
2 61
3 255
4 255
5 255
6 255
7 255
8 255
9 255
0 255
Any help is much appreciated