I2C eeprom problem

#include <Wire.h>

byte val = 12;

void setup()
{
  Serial.begin(9600);
  Wire.begin(); // join i2c bus
  Wire.beginTransmission(80); // transmit to device
  Wire.write(2);               // device address is specified in datasheet
  Wire.write(val);             // sends value byte  
  Wire.endTransmission();     // stop transmitting
  Wire.beginTransmission(80);
  Wire.write(2);
  Wire.endTransmission();
  Wire.requestFrom(80, 1);    // request 6 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read();    // receive a byte as character
    Serial.print(c);
}
}

void loop()
{

}

So after I connected the eeprom I did the i2C test and the device was found on address 80.
I also tried this code that was made by someone and it does display something but it just keeps displaying 255 no matter how the address is changed.

#include <Wire.h>    
 
#define disk1 0x50    //Address of 24LC256 eeprom chip
 
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
 
  unsigned int address = 0;
 
  writeEEPROM(disk1, address, 123);
  Serial.print(readEEPROM(disk1, address), DEC);
}
 
void loop(){}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
 
  delay(5);
}
 
byte readEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
 
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) rdata = Wire.read();
 
  return rdata;
}

Something is wrong and I don't know what.
Oh and this is the eeprom.

Your eeprom is small, and has a one byte register address. The second code you posted is working with a two byte register address. This is not the correct communication protocol for your eeprom, and the internal eeprom controller does not know how to talk to the world in that fashion.

In your version of the sketch, you are storing the value 12 in register 2. 12 is not a printable ascii character. If you change the 12 to 65, you should print "A".

char c = Wire.read();    // receive a byte as character
Serial.print(c);

If you want to see the numerical value change to

byte c = Wire.read();   
Serial.print(c);

Thank you, but the problem is there is still noting being displayed.

I have tested the i2c with some test and it does find it so I know I got the i2c connected ok.

I seriously don't know what I am doing wrong here.

This is my first arduino project and I am stuck. I have used Nick Gammon I2C test and when I unplug external eeprom it says 0 devices, when I plug it in it says 1 devices found at address 80 0x50 so I know that my external eeprom is connected alright.

The problem comes when I try to write the code to read and write, no matter what I try (and I have tried multiple codes from others + tried to make my own code).

This is the eeprom documentation

I have tried using libraries for similar eeprom (documentation says it's ok), I have tried manually writing the code (Wire.h stuff) and still nothing.

The three address pins are grounded as is the programming time control output (PTC pin 7).
SCL goes on A5 and SDA on A4.

Perhaps I need external resistors for clock and data lines?

#include <Wire.h>

byte val = 65;

void setup()
{
  Serial.begin(9600);
  Wire.begin(); // join i2c bus
  Wire.beginTransmission(80); // transmit to device
  Wire.write(2);               // device address is specified in datasheet
  Wire.write(val);             // sends value byte  
  Wire.endTransmission();     // stop transmitting
  delay(50);
  Wire.beginTransmission(80);
  Wire.write(2);
  Wire.endTransmission();
  Wire.requestFrom(80, 1);   

  while(Wire.available())    
  { 
    byte c = Wire.read();    
    Serial.print(c);
}
}

void loop()
{

}

When I open Serial Monitor everything is blank.

Try this

Umm, you linked to my other topic. And yes I already tried changing it to 65 and changing to byte and char. Nothing works.

TasmaniaKrama:
Umm, you linked to my other topic.

I know. Why have you got separate threads on the same topic?
Don't you think that might make you a little unpopular?

Sorry, I figured I might have posted in the wrong section since this might not be just the code problem. This is the first time I am working with arduino so maybe I didn't connect things correctly? I don't know.

I am using this arduino nano and I am not sure if I need to use external resistors for data and clock line.

Does this code print out "123". It's the second code you posted changed to use a one byte storage address.

#include <Wire.h>    
 
#define disk1 0x50    //Address of 24LC256 eeprom chip
 
void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
 
  unsigned int address = 0;
 
  writeEEPROM(disk1, address, 123);
  Serial.print(readEEPROM(disk1, address), DEC);
}
 
void loop(){}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) 
{
  Wire.beginTransmission(deviceaddress);
 // Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data);
  Wire.endTransmission();
 
  delay(5);
}
 
byte readEEPROM(int deviceaddress, unsigned int eeaddress ) 
{
  byte rdata = 0xFF;
 
  Wire.beginTransmission(deviceaddress);
 // Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,1);
 
  if (Wire.available()) rdata = Wire.read();
 
  return rdata;
}

Sadly no, the output is 255 which is byte rdata = 0xFF;

Seems like Wire.available() is not getting anything.

A couple of things to try...

Increase the delay at the end of the writeEEPROM() function to at least 10 ms. Datasheet states 10 ms per byte or 31.5 ms per page write time, during which the device will not acknowledge its address.

Check the byte returned by Wire.endTransmission().

@TasmaniaKrama, do not cross-post. Threads merged.

Martin-X:
A couple of things to try...

Increase the delay at the end of the writeEEPROM() function to at least 10 ms. Datasheet states 10 ms per byte or 31.5 ms per page write time, during which the device will not acknowledge its address.

Check the byte returned by Wire.endTransmission().

Thank you, I changed it to 50ms but it still doesn't work.
As for endTransmission, I am not sure how to check the byte returned. I am not so good with programming and I tried multiple things. How would I make it so that Wire.endTransmission byte is printed in serial monitor?
Edit: I checked writeEEPROM() --> Wire.endTransmission() and it returns 2 not 0.
2:received NACK on transmit of address
Any idea what that is?

I don't know how you tested the i2c bus previously to find your device address, but this scanner looks at the return value from wire.endTransmission. Do you find your eeprom on the bus with this scanner?

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

I2C scanner. Scanning ...
Found address: 80 (0x50)
Done.
Found 1 device(s).

Also I noticed that in the previous codes suggested if I disable the EEPROMwrite and only try to read a certain address I am able to get values.

However there seems to be something wrong when attempting to write.

Perhaps I need external resistors for clock and data lines?

Did you ever install the external pullups on SDA,SCL? It's a good idea. 4.7K or 2.2K can not hurt.

The internal pullups are weak.

cattledog:
Did you ever install the external pullups on SDA,SCL? It's a good idea. 4.7K or 2.2K can not hurt.

The internal pullups are weak.

Yes I placed 4.7k and I was able to find the solution.

Pin 7 is usually write protection and if you want to write you need to connect it to GND. However this eeprom had PTC programming time control (whatever that is) and you can only write on it if it is connected to 5v.

Thank you all for assisting me in this journey.

Pin 7 is usually write protection and if you want to write you need to connect it to GND. However this eeprom had PTC programming time control (whatever that is) and you can only write on it if it is connected to 5v.

Good job seeing this. It's actually on page 1 of the data sheet, and I didn't look closely enough at the picture of your wiring.

Programming Time Control (PTC), Pin 7, must be connected to either VDD
or left open-circuit.