WRITE PROTECT (WP): The AT24C01A/02/04/08A/16A has a Write Protect pin that provides hardware data protection. The Write Protect pin allows normal Read/Write operations when connected to ground (GND).
BYTE WRITE: A write operation requires an 8-bit data word address following the device address word and acknowledgment. Upon receipt of this address, the EEPROM will again respond with a zero and then clock in the first 8-bit data word. Following receipt of the 8-bit data word, the EEPROM will output a zero and the addressing device, such as a microcontroller, must terminate the write sequence with a stop condi- tion. At this time the EEPROM enters an internally timed write cycle, tWR, to the nonvolatile memory. All inputs are disabled during this write cycle and the EEPROM will not respond until the write is complete (see Figure 8 on page 11).
The way I read that, you send the low-order address and the data. You are sending two bytes of address and then the data.
Ditto for the read (see Figure 11). You should send one byte of address, then do a requestFrom.
Well, thanks for your answers but I'm at the very beginning with this I2C EEPROM and you get me a little bet lost.
PaulS;
I'm just using a Sketch found somewhere on the web.
As it is my very first go with I2C EEPROM, I'm not really understanding the code actually.
And I'm not a programmer, so till now I've been mostly copying different pieces of code and modifying some part, then look what was happening.
Nick Gammon ;
The WP pin is to GND, so writing should be allowed.
You are talking about 2 Bytes of address that should be replaced by a only 1Byte address because the 24C02 has only 256 memory blocks. (if I understand well)
I've seen something talking about LSB and MSB concerning the address.
So I think, you might be talking about something that has to do with this. Is that right ?
But can you notice, on a previous message when I'm using Nick I2C Scanner on 2 different EEPROM, I got :
For the 24C02 :
I2C scanner. Scanning ...
Found address: 80 (0x50)
Done.
Found 1 device(s).
I've tested another one in SOIC package (Reference unreadable) and I get :
I2C scanner. Scanning ...
Found address: 80 (0x50)
Found address: 81 (0x51)
Done.
Found 2 device(s).
So, I deduce that the 2nd EEPROM was more than 256Bytes and needs an address on 2 byte (LSB and MSB)
Fortunately, we can see what happen on 2 examples. (2kB or more)
Unfortunately, the Write/Read sketch don't work for both !
I hope I'm clear enough as I'm a beginner and English is not my first language.
Well seen Miran Ligowyr !
Sorry, I'm guilty, I haven't properly done the modification.
I haven't neutralised the "Wire.write((int)(addr >> 8));" piece of code and then I've done it for the readData subroutine but not for the "writeData" subroutine.
Here is the code and now it works great !
#include <Wire.h> // for I2C
#define i2caddr 0x50 // device address for left-hand chip on our breadboard
byte d=0; // data to store in or read from the EEPROM
void setup()
{
Serial.begin(9600); // Initialize the serial line
Wire.begin(); // wake up the I2C
Serial.println("Writing data...");
for (int i=0; i<20; i++)
{
writeData(i,i);
}
Serial.println("DONE");
Serial.println("Reading data...");
for (int i=0; i<20; i++)
{
Serial.print(i);
Serial.print(" : ");
d=readData(i);
Serial.println(d, DEC);
}
Serial.println("DONE");
}
// writes a byte of data in memory location addr
void writeData(unsigned int addr, byte data)
{
Wire.beginTransmission(i2caddr);
// set the pointer position
//Wire.write((int)(addr >> 8));
Wire.write((int)(addr & 0xFF));
Wire.write(data);
Wire.endTransmission(1);
delay(10);
}
// reads a byte of data from memory location addr
byte readData(unsigned int addr)
{
byte result;
Wire.beginTransmission(i2caddr);
// set the pointer position
//Wire.write((int)(addr >> 8));
Wire.write((int)(addr & 0xFF));
Wire.endTransmission(1);
Wire.requestFrom(i2caddr,1); // get the byte of data
result = Wire.read();
return result;
}
void loop()
{
}
To see what happen,for the 24C02, I replaced "for (int i=0; i<20; i++)" with
"for (int i=0; i<260; i++)
and I got
But as I wanted to try to write and read, I have a new issue.
The sketch I'm using :
#include <Wire.h> // for I2C
#define i2caddr 0x50 // device address for left-hand chip on our breadboard
byte d=0; // data to store in or read from the EEPROM
void setup()
{
Serial.begin(9600); // Initialize the serial line
Wire.begin(); // wake up the I2C
Serial.println("Writing data...");
for (int i=0; i<255; i++)
{
writeData(i,i);
}
Serial.println("DONE");
Serial.println("Reading data...");
for (int i=0; i<255; i++)
{
Serial.print(i);
Serial.print(" : ");
d=readData(i);
Serial.println(d, DEC);
}
Serial.println("DONE");
}
// writes a byte of data in memory location addr
void writeData(unsigned int addr, byte data)
{
Wire.beginTransmission(i2caddr);
// set the pointer position
//Wire.write((int)(addr >> 8));
Wire.write((int)(addr & 0xFF));
Wire.write(data);
Wire.endTransmission(1);
delay(10);
}
// reads a byte of data from memory location addr
byte readData(unsigned int addr)
{
byte result;
Wire.beginTransmission(i2caddr);
// set the pointer position
//Wire.write((int)(addr >> 8));
Wire.write((int)(addr & 0xFF));
Wire.endTransmission(1);
Wire.requestFrom(i2caddr,1); // get the byte of data
result = Wire.read();
return result;
}
void loop()
{
}
The result I got in the Serial monitor that I put in a spread sheet.
You can notice, all the Bytes seems not to be written as they should be.
I modified the 10ms delay in the "writeData" subroutine but nothing change even with 20ms delay instead !
As I said, I'm at the very beginning with I2C EEPROM devices, so I've just copied the code from the web without really understanding what is happening.
You're right about the comment, but I have only 1 EEPROM connected.
So I think it doesn't really matter for my present issue.
I've changed the (1) with the () ind the write, but it doesn't make any change. Still wrong data in the EEPROM.
The sketch in now as below :
#include <Wire.h> // for I2C
#define i2caddr 0x50 // device address
byte d=0; // data to store in or read from the EEPROM
void setup()
{
Serial.begin(9600); // Initialize the serial line
Wire.begin(); // wake up the I2C
Serial.println("Writing data...");
for (int i=0; i<257; i++)
{
writeData(i,i);
}
Serial.println("DONE");
Serial.println("Reading data...");
for (int i=0; i<257; i++)
{
Serial.print(i);
Serial.print(" : ");
d=readData(i);
Serial.println(d, DEC);
}
Serial.println("DONE");
}
// writes a byte of data in memory location addr
void writeData(unsigned int addr, byte data)
{
Wire.beginTransmission(i2caddr);
// set the pointer position
//Wire.write((int)(addr >> 8));
Wire.write((int)(addr & 0xFF));
Wire.write(data);
Wire.endTransmission();
delay(10);
}
// reads a byte of data from memory location addr
byte readData(unsigned int addr)
{
byte result;
Wire.beginTransmission(i2caddr);
// set the pointer position
//Wire.write((int)(addr >> 8));
Wire.write((int)(addr & 0xFF));
Wire.endTransmission(1);
Wire.requestFrom(i2caddr,1); // get the byte of data
result = Wire.read();
return result;
}
void loop()
{
}
By the way. I'm having so hard time with this I2C EEPROM, would you have any good link with some sketches very well explained. I haven't been able to find something that could help me properly yet !
At the end, my project is to reprogram a Laser printer chip.
So, read an EEPROM (brand new and already programmed), then, write the data to the old one, so I've got a spare one.
I'll need some advice to do this. I've already read the data of the new 24C04.
I'll now need to inject these data in the old one.
This code waits until the device has finished writing (it won't respond until it is done). But your delay of 20ms should be plenty of time to finish a write. It's worth a try.
I'm using larger EEPROMs (e.g. 24LC1026) but other than that your code is the same as mine.
Sorry for the late response but I was on something else these time.
To make it working, I needed to power up the Arduino through an external 12VDC power supply instead of the USB cable from the computer or even a 5vDC supply through Vin of the ARDUINO.
I also replace the delay by El Supremo's piece of code witch I thing is much more clever than wasting time with Delay and being not sure that the job is done !
But the problem was raelly the power supply of the ARDUINO. At least it now works fine.
Now I'm sure the writing and reading is ok, how can I write the proper number in each piece of memory ?
See, I've read the Eeprom I want to copy so I stored all the data in an open office format as .ods (OoCalc)or .odt (OoWriter) format)
1/ Read the data
2/ Copy from the SerialMonitor
3/ Paste in a calc or writer page to save the data in a safe place.
But I don't know how to program the new Eeprom with these data.
I'm not going to write each data at a time in the serial monitor. here must be a better way for sure.