Read/Write to/from EEPROM

I want to add an external EEPROM to store some simple values between 0 and 255. I'm using an AT24C256 external EEPROM board. So for example, I want to specify memory space 1, and store, say 217. Memory space 2, store the number 45. I found the following sketch, and I'm getting errors, which I can see why, but not sure how to correct them. It's saying "writeI2CByte was not declared in this scope." I'm not sure how to declare it.

As a side note, I added MODE and the subsequent if statement for it. This is just for testing purposes so it's not constantly writing to the EEPROM.

I'm sure there's other errors, and I might also be whizzing up the wrong tree in the first place altogether.

#include <Wire.h>
#define ADDR_Ax 0b000 //A2, A1, A0
#define ADDR (0b1010 << 3) + ADDR_Ax
int MODE;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  writeI2CByte(0, 1);
  Serial.println(readI2CByte(0));
  MODE=1;
}

void loop() {  

if (MODE ==1){
void writeI2CByte(byte data_addr, byte data){
  Wire.beginTransmission(ADDR);
  Wire.write(1);
  Wire.write(217);
  Wire.endTransmission();
  MODE = 2;
  delay(50);
}
}
byte readI2CByte(byte data_addr){
  byte data = NULL;
  Wire.beginTransmission(ADDR);
  Wire.send(1);
  Wire.endTransmission();
  Wire.requestFrom(ADDR, 1); //retrieve 1 returned byte
  delay(15);
  if(Wire.available()){
    data = Wire.read();
  }
  return data;
  Serial.println(data);
}

Put out the function definition out of the loop().

1 Like

A program is a list of definitions, but function definitions only allowed at top level, so not inside loop() - this is a quirk of C and C++.

The function definition is outside the loop(), but the code is very badly indented.

No.
The definition is inside the if inside the loop.

Yes you are right!
I've missed the second function definition... :sweat_smile:

Hi @tpaairman

try this:

#include <Wire.h>
#define ADDR_Ax 0b000 //A2, A1, A0
#define ADDR (0b1010 << 3) + ADDR_Ax
int MODE;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  writeI2CByte(0, 1);
  Serial.println(readI2CByte(0));
  MODE = 1;
}

void loop() {

  if (MODE == 1)
  {
     //????????????
  }
}
  void writeI2CByte(byte data_addr, byte data) 
  {
    Wire.beginTransmission(ADDR);
    Wire.write(1);
    Wire.write(217);
    Wire.endTransmission();
    MODE = 2;
    delay(50);
  }

  byte readI2CByte(byte data_addr) 
  {
    byte data = NULL;
    Wire.beginTransmission(ADDR);
//    Wire.send(1);       ///  send ????????
    Wire.endTransmission();
    Wire.requestFrom(ADDR, 1); //retrieve 1 returned byte
    delay(15);
    if (Wire.available()) {
      data = Wire.read();
    }
    return data;
    Serial.println(data);
  }

So, this is a sketch that you have not created. Why not you try to create your own sketch from the following tips to write/read data byte 0x23 into/from location 0x1234?

1. Build connection diagram between UNo and EEPROM as per Fig-1 given below.

eepromAT24C256
Figure-1:

2. Upload the following sketch in UNO.

#include<Wire.h>
byte myData = 0x23;  //data byte to be stored at location 0x1234 of EEPROM
int eepromAddress = 0x1234;  //EEPROM location that will hold data byte 0x23

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  //--- wrieitng 0x23 at location address 0x1234
  Wire.beginTransmission(0x50); //EEPROM's i2C address
  Wire.write(highByte(eepromAddress));  //pointing beginning address of EEPROM
  Wire.write(lowByte(eepromAddress));
  Wire.write(myData); 
  byte busStatus = Wire.endTransmission();
  if (busStatus != 0)
  {
    Serial.print("EEPROM is not found...!");
    while (1);  //wait for ever
  }
  Serial.println("EEPROM is found.");
  delay(5); //write cycle delay
  //---reading back the data--------------
  Wire.beginTransmission(0x50);
  Wire.write(highByte(eepromAddress));  //pointing target location address of EEPROM
  Wire.write(lowByte(eepromAddress));
  Wire.endTransmission();
  //--------------------------------------------
  byte n = Wire.requestFrom(0x50, 1); //reading 1 bytes data from location 0x1234
  byte recData = Wire.read();
  Serial.print(recData, HEX); //Serial Monitor should show: 23
}

void loop()
{

}

After looking into things further, I found info to put me on the right track. Being new to all of this, I didn't catch that I needed to define the operation in the setup and not the loop. But I'm now able to write and read to the EEPROM.