Communicate with an I2C EEPROM Device
This example shows how to store and retrieve data from an EEPROM on an I2C device. Using EEPROM you can read, erase, and rewrite individual bits of data from the sensor’s memory. Before using this example, wire an EEPROM to the I2C pins on your Arduino hardware.
This example uses 24LC02B – I2C Bus Serial EEPROM.
Create an I2C connection on your Arduino® hardware.
Create an Arduino object and include the I2C library.
a = arduino('COM22','Uno','Libraries','I2C');
Scan for I2C address on the Arduino hardware.
addrs = scanI2CBus(a)
addrs = 8×1 cell array {'0x50'} {'0x51'} {'0x52'} {'0x53'} {'0x54'} {'0x55'} {'0x56'} {'0x57'}
Create an I2C device object using the address and the arduino object.
eeprom = device(a,'I2CAddress','0x54');
The bus number defaults to 0. Refer to your device datasheet to get the correct address for your device.
Write “Hello World!” to the EEPROM from chip AT24C02. This device has eight bytes per page. You must specify the page address in front of each byte of data to be written.
Write the first byte of the string, “Hello Wo” to address 0.
write(eeprom,[0 'Hello Wo']);
Write the second byte of the string, “rld!” to address 8.
write(eeprom,[8 'rld!']);
Write 0
in order to read from the first byte of the first
page.
write(eeprom,0);
Read data from the EEPROM.
char(read(eeprom,12))
ans = Hello World!
Write 5 to the register with address 80 on the EEPROM chip.
writeRegister(eeprom,80,5);
Read data from the address 80 on the EEPROM chip.
readRegister(eeprom,80)
ans = 5