Interfacing Arduino Uno with AT24C16 I2C EEPROM IC

I am trying to interface Arduino Uno with AT24C16 I2C EEPROM IC.I have made necessary connections and have written simple code for writing and reading data from AT24C16. Initially I wrote the data to one random memory location (in my code it is 514) and then read it back immediately after writing. I could read correct data. Then I wrote code for writing to memory locations 0,1,2,3 and 4 in the same sketch. This time also I could read correct data when I read those locations immediately after writing to them. The problem is after carrying out all thees operations, when I wrote the code to read data from, say 2nd location, it is reading the data that is there in 4th location. I got same result when i attempted to read 0th, 1st and 3rd memory locations. But if I ask for 514th location, it returned correct data. So I wrote the code for writing to 515th location and when I read 514th location, it showed the data that is present in 515th location.

I am sending the code that I have tried to write and also the output. I am new to Arduino coding.
Please help me.

working.ino (7.29 KB)

I can not confirm your problem. When I run the code you attached, I get the following print out which I think is what you intended.

Written
9
1
2
3
4
5
6
4
9
1
8

Your method of selecting the high and low byte of the address is unusual. More common is

Wire.write((int)(add >> 8)); // high byte of eerpom data address
  Wire.write((int)(add & 0xFF)); // low byte

Please read the How to use this forum posthttp://forum.arduino.cc/index.php?topic=149023.0

You will get more people to look at your code if you post it using the code tags icon </> so that it looks like this.

#include <Wire.h>

void setup()
{
  #define deviceaddress 0x50
  Serial.begin(9600);
unsigned int a=514;
//Serial.println(a,BIN);
byte b=a>>6;
//Serial.println(b,BIN);
byte c=a & 63;
//Serial.println(c,BIN);
Wire.begin();
Wire.beginTransmission(deviceaddress);
Wire.write(b);
Wire.write(c);
delay(5);
Wire.write(9);
delay(5);
//Serial.println(9, BIN);
Wire.endTransmission();
delay(5);
Serial.println("Written");
 byte rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
 // Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
    //Serial.println("read");
    Serial.println(rdata);
    //Serial.println(rdata, BIN);
    delay(1);}
    
        a=a+1;
//Serial.println(a,BIN);
 b=a>>6;
//Serial.println(b,BIN);
 c=a & 63;
 Wire.begin();
Wire.beginTransmission(deviceaddress);
Wire.write(b);
Wire.write(c);
delay(5);
Wire.write(8);
delay(5);
//Serial.println(9, BIN);
Wire.endTransmission();
delay(5);


  // Serial.println(a, BIN);
  
 // Serial.println(b,BIN);
 
// Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
 // Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
    //Serial.println("read");
   // Serial.println(rdata);
    
    delay(1);}  
    
    delay(100);
    
    a=0;
//Serial.println(a,BIN);
 b=a>>6;
//Serial.println(b,BIN);
 c=a & 63;
 Wire.begin();
Wire.beginTransmission(deviceaddress);
Wire.write(b);
Wire.write(c);
delay(5);
Wire.write(1);
delay(5);
//Serial.println(9, BIN);
Wire.endTransmission();
delay(5);


a=0;
//   Serial.println(a, BIN);
 b=a>>6;
//  Serial.println(b,BIN);
 c=a&63;
//Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
 // Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
    //Serial.println("read");
    Serial.println(rdata);
    
    delay(1);}  

    a=1;
//Serial.println(a,BIN);
 b=a>>6;
//Serial.println(b,BIN);
 c=a & 63;
 Wire.begin();
Wire.beginTransmission(deviceaddress);
Wire.write(b);
Wire.write(c);
delay(5);
Wire.write(2);
delay(5);
//Serial.println(9, BIN);
Wire.endTransmission();
delay(5);


a=1;
//   Serial.println(a, BIN);
  b=a>>6;
// Serial.println(b,BIN);
c=a&63;
// Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
//  Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
    //Serial.println("read");
    Serial.println(rdata);
    
    delay(1);}  

   a=2;
//Serial.println(a,BIN);
b=a>>6;
//Serial.println(b,BIN);
 c=a & 63;
 Wire.begin();
Wire.beginTransmission(deviceaddress);
Wire.write(b);
Wire.write(c);
delay(5);
Wire.write(3);
//delay(5);
//Serial.println(9, BIN);
Wire.endTransmission();
delay(5);

a=2;
// Serial.println(a, BIN);
  b=a>>6;
//  Serial.println(b,BIN);
 c=a&63;
// Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
//  Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
   // Serial.println("read");
    Serial.println(rdata);
    
    delay(1);}  

    a=3;
//Serial.println(a,BIN);
 b=a>>6;
//Serial.println(b,BIN);
 c=a & 63;
 Wire.begin();
Wire.beginTransmission(deviceaddress);
Wire.write(b);
Wire.write(c);
delay(5);
Wire.write(4);
delay(5);
//Serial.println(9, BIN);
Wire.endTransmission();
delay(5);


a=3;
//   Serial.println(a, BIN);
 b=a>>6;
//  Serial.println(b,BIN);
c=a&63;
// Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
//  Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
    //Serial.println("read");
    Serial.println(rdata);
    
    delay(1);}  

    a=4;
//Serial.println(a,BIN);
 b=a>>6;
//Serial.println(b,BIN);
 c=a & 63;
 Wire.begin();
Wire.beginTransmission(deviceaddress);
Wire.write(b);
Wire.write(c);
delay(5);
Wire.write(5);
delay(5);
//Serial.println(9, BIN);
Wire.endTransmission();
delay(5);

a=4;
//   Serial.println(a, BIN);
 b=a>>6;
//  Serial.println(b,BIN);
 c=a&63;
//Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
 // Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
  // Serial.println("read");
    Serial.println(rdata);
    
    delay(1);}  
    
    delay(100);
        a=5;
//Serial.println(a,BIN);
 b=a>>6;
//Serial.println(b,BIN);
 c=a & 63;
 Wire.begin();
Wire.beginTransmission(deviceaddress);
Wire.write(b);
Wire.write(c);
delay(5);
Wire.write(6);
delay(5);
//Serial.println(9, BIN);
Wire.endTransmission();
delay(5);

a=5;
//   Serial.println(a, BIN);
  b=a>>6;
// Serial.println(b,BIN);
 c=a&63;
// Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
 // Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
//   Serial.println("read");
    Serial.println(rdata);
    
    delay(1);}  
    
    delay(100);
    
    a=3;
//  Serial.println(a, BIN);
 b=a>>6;
//  Serial.println(b,BIN);
c=a&63;
// Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
 // Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
   //Serial.println("read");
    Serial.println(rdata);
    
    delay(1);}


    

a=514;
b=a>>6;
c=a&63;

 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
 // Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
 // Serial.println("read");
    Serial.println(rdata);
    
    delay(1);} 
    delay(100);
 a=0;
  // Serial.println(a, BIN);
 b=a>>6;
//  Serial.println(b,BIN);
 c=a&63;
// Serial.println(c,BIN);
 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
  //Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
    rdata = Wire.read();
  //Serial.println("read");
    Serial.println(rdata);
    
    delay(1);}
    
    a=515;
b=a>>6;
c=a&63;

 rdata = 0xFF;

 
  Wire.beginTransmission(deviceaddress);
  Wire.write(b);   // MSB
  Wire.write(c); 
 // Wire.write(0,a); // LSB
  Wire.endTransmission();
  delay(5);

  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) {
   rdata = Wire.read();
   // Serial.println("read");
    Serial.println(rdata);
   
 delay(1);} 
 delay(100);
   
}
void loop(){}

Dear Sir,

Thank you for considering my problem.
I modified the way of addressing the memory locations as you said. But still it is not happening.

Reason for the way of addressing that is used in code:
I think that the way of addressing depends on the memory size of EEPROM chip. I have used AT24C16 EEPROM IC which is of 16 Kbits capacity. Therefore we can access the memory locations starting from 0 to 16k/8 i.e., 2000. 2000 is represented by 12 bits i.e., 011111010000. So I shifted it by 6 bits instead of 8 bits to get MSB and performed AND operation with 63 (or 3F in hex) instead of 3F to abstract lower 6 bits. I want to know why I should shift it by 8 bits.

Please give suggestions on this.