External EEPROM write/read problems (Arduino Uno)

0
22

I have Arduino Uno(Atmega328p) and external EEPROM connected via I2C.

My code in Arduino Studio:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#define EEPROM_ADDR 0x50

#include <M24AA01.h>

M24AA01 dev(0);

byte data[1] = {1};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC);

  writeEEPROM(1);
  uint8_t readData = readEEPROM(1);

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(5,30);
  display.print("read:");
  display.print(data);
  display.display();


}

void loop() {
  // put your main code here, to run repeatedly:

}

void writeEEPROM(unsigned int eeaddress) 
{
  Wire.beginTransmission(EEPROM_ADDR);

  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.write(data[0]);

  Wire.endTransmission();

  delay(5);
}


uint8_t readEEPROM(unsigned int eeaddress) 
{
  uint8_t rdata = 0xFF;

  Wire.beginTransmission(EEPROM_ADDR);

  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB

  Wire.endTransmission();

  Wire.requestFrom(EEPROM_ADDR,1);

  if (Wire.available()) rdata = Wire.read();

  return rdata;
}

My physical connection of the EEPROM:

PIN 1: A0 – 5V(VCC)

PIN 2: A1 – 0(GND)

PIN 3: A2 – 0(GND)

PIN 4: VSS – 0(GND)

PIN 5: SDA – SDA

PIN 6: SCL – SCL

PIN 7: WP – 0(GND)

PIN 8: VCC – 5V(VCC)

I have a DIP version of EEPROM.

From the documentation I read that address of EEPROM is 0x50 (1010000). A0 is connected to 5V, but anyway A0,A1,A2 are “dont cares” as per documentation, so I believe address is fine.

No matter what I write to the EEPROM, in this case I just write 1 byte of data (number 1) – it reads 255.

One thing I noticed is that I do not specify in the code if I want to write or read (in documentation the 8th bit is read/write bit, read = 1, write = 0), but I do not know how to use it in code.

LEAVE A REPLY

Please enter your comment!
Please enter your name here