Attaching an EEPROM to the Arduino is quite simple and the easiest way to do that is via the I2C bus. EEPROMs come in many forms but the 24 LS256 is a good choice as it is easy to use and pretty cheap (85 euro cents at my supplier). It can hold 256 kilobits of data (that’s 32 kilobytes).
The 24LS256 can also run on 3.3V which is handy if one is using a Lilypad or Pro Mini 3.3V. The 24LS256 uses 3 pins for selection of its address, so you can use up to eight at once on the same bus.
The above figure shows the base address register of the 24L256. It is addressed as follows: 1010A2A1A0. If you are only working with 1 EEPROM, the easiest is to connect A2-A0 with Ground. This gives the address of 1010000 which is 0x50 Hex. In reading and writing to the EEPROM one needs to realize that it has 32 kB (actually 32767) and therefore one byte is not enough to address all the memory.
So when one wants to send read and/or write requests, one needs to send two bytes – one for the MSB or higher end of the address (the 8 bits from left to right), and the next one for the LSB or lower end of the address (the final 8 bits from left to right).
If for example one wants to use address 21000, that goes as follows: In binary, 21000 is 0101001000001000. Split that up into 01010010 and 00001000, then convert the binary values back to numerical bytes to send with Wire.send().
That sounds more complicated than it is, as there are in fact two operands to help with that. This first one is >>, or bitshift right. This will take the MSB of the byte and drop off the lower end, leaving only the first 8 bits. To get the LSB of the address, one can use operator &, or bitwise and. This operand, when used with 0xFF will give the lower bits.
Writing data to the 24LC256
Writing data is quite easy. First initialize the I2C bus with:
Wire.beginTransmission(0x50); // for pins A0~A2 set to GND
then send some data. The first data to send are the two bytes for the address (25000) were one wants to write to the memory.
Wire.send(21000 >> 8); // send the MSB of the address Wire.send(21000 & 0xFF); // send the LSB of the address
Subsequently send the byte to store at address 21000 and then close the connection:
Wire.send(15); //just sending ‘15’ as example Wire.endTransmission();
That concludes the writing. now for reading:
Reading data from the 24LC256
Reading goes similar. First initialize the connection and provide the address of the data to read:
Wire.beginTransmission(0x50); // Chosen base address Wire.send(21000 >> 8); // send MSB of the address Wire.send(21000 & 0xFF); // send LSB of the address Wire.endTransmission();
Then, ask for the number of bytes of data starting at the current address:
Wire.beginTransmission(0x50); // base address Wire.requestFrom(0x50,1); // We want one byte Wire.receive(inbyte);
Here, inbyte is a byte variable chosen to store the data retrieved from the EEPROM.
The Power of the I2C bus is of course that various devices can be connected to the same lines. The top figure shows such a set up with two EEPROMs. It is key of course that they each have their own address. In the figure I have chosen to use addresses 0x50 and 0x51. One gets that by connecting A0-A2 to ground for one chip, but connecting A0 to Vcc (‘ High’) for the second chip. The resulting address is then 1010001.
A print layout i given below
Print design can be found here