Pages: [1]   Go Down
Author Topic: Cannot Read/Write to an 24LC256 EEPROM chip  (Read 1747 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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.

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
« Last Edit: December 31, 2013, 10:40:10 am by kookoo_gr » Logged

Offline Offline
Edison Member
*
Karma: 50
Posts: 1702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your first sketch works for me.

The second one won't work because it is only writing one address byte in the read and write functions.

Pete
Logged

Offline Offline
Edison Member
*
Karma: 50
Posts: 1702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the first sketch, try adding
Code:
  while(!Serial);
after Serial.begin

Pete
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 228
Posts: 14053
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Be aware there is a latency after a write to an EEPROM of IIRC 5 millis()

Please use CTRL-T in the IDEfor proper indenting (makes code better readable)

I wrote a lib for I2C eeprom with test sketch - http://playground.arduino.cc/Main/LibraryForI2CEEPROM - that keeps track of the write latency.  you can give it a try,.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With some testing i got some results

if i move the command

Code:
Serial.println(eepromRead(0,0));                 //read from external eeprom at address 00

from the setup function at the loop function then the program works but my problem is that i want to read the eeprom once and not loop the read command
Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 71
Posts: 2614
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With some testing i got some results

if i move the command

Code:
Serial.println(eepromRead(0,0));                 //read from external eeprom at address 00

from the setup function at the loop function then the program works but my problem is that i want to read the eeprom once and not loop the read command
As el_supremo has suggested, try putting
Code:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
after Serial.begin in setup(). The Leonardo can take a little longer to initialize the serial port.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the suggestion did not work, the serial monitor is blank even when i waited for full minute
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

problem solved. The original sketch works perfectly as it should. If i change the delay at the setup() from delay(5) to e.g. delay(5000) the value of the eeprom will be displayed at serial monitor even if i do not use the while(!Serial) command.
Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 71
Posts: 2614
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Glad you got it sorted in the end. I have some SRAM chips that work fine on my UNO every time but on my MEGA I need to slow the SPI clock speed if using burst data mode (I think the Arduino is almost to slow for the device)
Logged


Pages: [1]   Go Up
Jump to: