Using EEPROM & I2C

08 Mar 2019

Started a project months ago with plans to save settings to an eeprom. I wrote some test code at the time to make sure i2c wouldn't be a hurdle. Here's what I've got:

#include "mbed.h"
I2C i2c(I2C_SDA, I2C_SCL );
int readVariable;
int address = 0xa1;
char array[1] ;
char arrayRead[1];
int main()
    while (1) {
        array[1] = 0x07;        // just a random number
        i2c.write(address,array,1);     //write to address,arrayRead,1);  //read from address
        readVariable = arrayRead[0];        
        printf("variable = %i\r\n", readVariable);

It simply writes a single number to an address. It works, but I'm not sure how to proceed.

I need to save a list of 18 8 bit variables at a time to 8 "banks"

I think I need to offset my addresses, but I haven't seen (or understood) the examples I've seen. Can anyone point me in the right direction? I'm using a nucleo 32f303k8 and 24aa64f eeprom if that makes any difference.


09 Mar 2019

I haven't tried it but looking at your code it appears to have been originally designed to handle more than one value at a time. I would suggest trying these changes:

1. Increase the size of char array[1] -> char array[18]

2. Increase the size of char arrayRead[1] -> char arrayRead[18]

3. Populate the array with your 18 values and then write them using:

i2c.write(address, array, 18);

4. Make similar changes to all the other array references

None of this should be enclosed in the while loop unless you really want to write and read the values forever.

13 Mar 2019

There's a good library for using the 24C series Eeproms that I've used successfully. The number you're giving doesn't exactly match the list they have, but path of least resistance is probably to just order a compatible part.

Please log in to post a reply.