How to read and write from AT24C256B eeprom with Arduino Nano

AT24C256B_bb


#include <Wire.h>
#include "AT24C256.h"

AT24C256 g_eeprom(0x50);

void printContents() {
  int eeaddress = 0;
  byte value;

  while (true) {
    value = g_eeprom.read(eeaddress++);
    if ((value == 0xFF) || (eeaddress == 32768)) {
      Serial.println();
      break;
    }
    Serial.print((char)value);
  }
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
  printContents();
}

void loop() {
  while (Serial.available()) {
    int i, m;
    String data = Serial.readStringUntil('\n');

    for (i = 0, m = data.length(); i < m; ++i) {
      g_eeprom.write(i, data[i]);
      Serial.print("writing ");
      Serial.print(data[i]);
      Serial.print(" in ");
      Serial.println(i);
    }
    g_eeprom.write(i, 0xFF);
    printContents();
  }
  delay(1);
}

Leave a Reply

Your email address will not be published. Required fields are marked *