Skip to content

Instantly share code, notes, and snippets.

@compilerexe
Created February 7, 2016 00:48
Show Gist options
  • Save compilerexe/cbb7ec6eaa9f542e927a to your computer and use it in GitHub Desktop.
Save compilerexe/cbb7ec6eaa9f542e927a to your computer and use it in GitHub Desktop.
External I2C EEPROM to Arduino (24LC256)
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment