Introduction: Add I2C EEPROM to Arduino
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 or 24LC256 is a good choice as it is easy to use and pretty cheap (85 euro cents at my supplier). The 24LC256 holds 256 kilobits of data (that is 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 24LS256 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 0×50 Hex. In reading and writing to the EEPROM one needs to realize that it has 32 kB (actuall32767) and 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 do that. This first one is >>, also known as 'bitshift right'. This will take the highest (left) part of the byte and drop off the lower end, leaving only the first 8 bits. To get the lower (right) end of the address, one can use operator &, also known as ' bitwise AND'. This operand, when used with 0xFF will give the lower bits.
Writing data to the 24LS256
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
That concludes the writing. now for reading:
Reading data from the 24LS256
Reading is more or less 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
Then, supply the number of data bytes to read starting at the current address:
Wire.beginTransmission(0x50); // base address
Wire.requestFrom(0x50,1); // We want one byte
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 0×50 and 0×51. 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 = 0x51
And that is it.
A print design can be found here
Approx. cost: 2 euro
We have a be nice policy.
Please be positive and constructive.