AT24C02 interfacing with arduino: Help me to debug

Hi,

I am interfacing the AT24C02 with Arduino for testing as I could not do it with 8051. So, for making things simple, I tried it in Arduino with following code. But could not get success.

could you help me to debug this ?

A0, A1, A3 are ground
WP is grounded
SDA = Pin 2 of arduino digital
SCL = Pin 3 of arduino digital.

Moreover, pulled up the SDA and SCL with 3K3 resistor to Vcc.

int SDA=2;
int SCL=3;

void setup()
{
    pinMode(SCL,OUTPUT);
    Serial.begin(9600);
    
    for(int i=0;i<9;i++) //Reseting the EEPROM
    {
        clockEeprom();
    }
    
    startEeprom();
}

void wait()
{
  delay(4);
}
void startEeprom()
{
  pinMode(SDA,OUTPUT);
  
  digitalWrite(SDA,HIGH);
  digitalWrite(SCL,HIGH);
  wait();
  digitalWrite(SDA,LOW);
  wait();
  digitalWrite(SCL,LOW);  
}

void stopEeprom()
{
  pinMode(SDA,OUTPUT);
  digitalWrite(SDA,LOW);
  digitalWrite(SCL,HIGH);
  wait();
  digitalWrite(SDA,HIGH);
  wait();
  digitalWrite(SCL,LOW);
}

void clockEeprom()
{
  digitalWrite(SCL,LOW);
  wait();
  digitalWrite(SCL,HIGH);
  wait();
  digitalWrite(SCL,LOW);
  wait();
}


void transferByteEeprom(unsigned char transferByte)
{
  unsigned char mask=0x80;
  pinMode(SDA,OUTPUT);
  while(mask>0)
  {
    if(transferByte & mask)
      digitalWrite(SDA,HIGH);
    else
      digitalWrite(SDA,LOW);
    
    mask=mask>>1;
    clockEeprom();
  }
}

unsigned char readByteEeprom()
{
  unsigned char inByte=0;
  unsigned char temp=0x01;
  
  pinMode(SDA,INPUT);
  
  for(int i=7;i>=0;i--)
  {
    clockEeprom();
    if(digitalRead(SDA)==HIGH)
    {
      inByte=inByte | (temp<<i);
    }
    
  }
  return(inByte);
}

void sendCharEeprom(unsigned char addr, unsigned char value)
{
  startEeprom();
  transferByteEeprom(0xA0);
  clockEeprom();
  
  transferByteEeprom(addr);
  clockEeprom();
  
  transferByteEeprom(value);
  clockEeprom();
  
  stopEeprom();
  clockEeprom();
}
unsigned char readCharEeprom(unsigned char addr)
{
  unsigned char temp;
   startEeprom();
   transferByteEeprom(0xA0);
   clockEeprom();
   
   transferByteEeprom(addr);
   clockEeprom();
   
   startEeprom();
   transferByteEeprom(0xA1);
   clockEeprom();
   
   temp=readByteEeprom();
   clockEeprom();
   
   stopEeprom();
   clockEeprom();
}
void loop()
{
  int i=0;
  unsigned char temp;
  
  for(i=0;i<10;i++)
  {
    sendCharEeprom(i,i+'a');
    }
    
    delay(1000);
    
   for(i=0;i<10;i++)
   {
     temp=readCharEeprom(i);
     Serial.println(temp,BYTE);
   }
   
   Serial.println("Success");
}

Thanks in advance.

From your signal names, it appears you may need this in your sketch

#include <Wire.h> // bring in Wire Library

and then make sure you have the right pins:
(these are the pins for a Duemilanove)

Using IIC (I2C) for interfacing to a device

  • Arduino analog 5 - I2C SCL, pin 24
  • Arduino analog 4 - I2C SDA, pin 23

I am using Arduino Duemilanove 2009 ATmega168 board.

I couldn't understand the pin 24 and pin 23.

Are analog pins used for output?

I thought I had removed those, that was a note for me referring back to the phsyical pins on the ATMega chip itself.

Yes, the analog pins can be used as digital input/output also. A0 is D14, A1 is D15, etc up to D19.

The ATMega has multiple functions set up on several (most) of its pins. I2C is one of those functions, and it operates on specific pins. In this case, analog 4 & 5.

SPI is another, operating on pins D10-11-12-13.