
Using the ATMEGA168/Arduino with a 24LC08 Serial EEPROM
by Lewis Loflin
[ My Homepage ] [ Electronics Mainpage ]
Explanation of program:
On power up or reset the "setup" is executed once, setting up the hardware and writing the text message "Adrunio" to the EEPROM. Then the "loop" section will run over and over. Whenever sw0 is pressed the text message "Arduino" is read from the EEPROM and sent via the serial port to a computer running for example Hyper Terminal. This demonstrates the use of the Wire.h library, serial ports, and an external switch tied to an input.
For the basic structure of an Arduino program see page 7 of Arduino Programming Notebook by Brian Evans
For information on setting up switches and pull ups on the Arduino see pages 4-6 on Arduino Projects by Tod Kurt

Basic I2C setup.
24LC08 Demo with the ATMEGA128
This is an 8-pin DIP serial EEPROM. It will store 1024 bytes. (0x3FF)
It uses I2C or "two wire" interface.
See 24LC08 Serial EEPROM
Pin designations for the 24LC08:
Pins 1, 2, 3 if tied to VCC (5 volts) address = 0x54. If tied to VSS 0x50. Only two can be used in a single circuit.
Pin 4 VSS or ground.
Pin 5 SDA or serial data. Tied to Arduino analog pin 4.
Pin 6 SCL or serial clock. Tied to Arduino analog pin 5.
Pin 7 WP or write protect. VCC disables write, VSS enables write.
Pin 8 VCC or +5 volts.


Switch connection with and without an external pull up resistor.
For this test use the circuit on the right.
#include <Wire.h> // specify use of Wire.h library.
byte sw0 = 3; // digital pin for switch connected to ground.
// address in 24LC08. values from 000 hex to 3FF hex.
int position_pointer = 0x3f0;
void setup()
{
pinMode(sw0, INPUT);
digitalWrite(sw0, HIGH); // turn on internal pull up
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // setup serial for output
// send test message "Arduino"
Wire.beginTransmission(0x50); // connect to 24LC08 device address
Wire.send(position_pointer); // beginning address within EEPROM
Wire.send("Arduino");
Wire.endTransmission();
}
void loop()
{
while (digitalRead(sw0) == 1) { } // wait here until switch is pushed.
Wire.beginTransmission(0x50); // link to 24LC08
Wire.send(position_pointer); // must act as a position pointer
Wire.endTransmission();
Wire.requestFrom(0x50, 7); // request 7 bytes from slave device 24LC08
// below will loop until 7 bytes are received.
while(Wire.available()) // slave may send less than requested
{
byte c = Wire.receive(); // receive a byte as character
Serial.print(c); // print the character
}
Serial.print("\n"); // next line
delay(1000); // wait one second.
}
|
Basic Wire.h Library
Wire Library:
This library allows you to communicate with I2C devices. On the Arduino, SDA (data line) is on analog input pin 4 (A4), and SCL (clock line) is on analog input pin 5 (A5). Tie both through 10k resistors to VCC. (+ 5 volts.) Functions are listed below.
Wire.begin() // for master
Wire.begin(address) // for slave
Description: Initiate the Wire library and join the I2C bus as a master or slave. The 7-bit slave address (optional) if not specified, the device defaults as a master. Returns: None
Wire.requestFrom(address, quantity)
Request bytes from another device. The bytes may then be retrieved with the available() and receive() functions.
Address: the 7-bit address of the device to request bytes from
Quantity: the number of bytes to request
Wire.beginTransmission(address) // address is a 7-bit address of the device to transmit to.
Description: Begin a transmission to the I2C slave device with the given address. Subsequently, queue bytes for transmission with the send() function and transmit them by calling endTransmission(). This acts as a position pointer for the device.
Wire.send(value)
Wire.send(string)
Wire.send(data, quantity)
Description: Sends data from a slave device in response to a request from a master, or queues bytes for transmission from a master to slave device (in-between calls to beginTransmission() and endTransmission()).
Parameters:
value: a byte to send (byte);
string: a string to send (char *);
data: an array of data to send (byte *);
quantity: the number of bytes of data to transmit (byte).
Wire.endTransmission()
Description: Ends a transmission to a slave device that was begun by beginTransmission() and actually transmits the bytes that were queued by send().
Wire.available()V
Description: Returns the number of bytes available for retrieval with receive(). This should be called on a master device after a call to requestFrom() or on a slave inside the onReceive() handler.
byte Wire.receive()
Retrieve a byte that was transmitted from a slave device to a master after a call to requestFrom or was transmitted from a master to a slave.
Returns the next byte received.
Wire.onReceive(handler)
Description: Registers a function to be called when a slave device receives a transmission from a master.
Parameters: handler: the function to be called when the slave receives data; this should take a single int parameter (the number of bytes received from the master) and return nothing, e.g.: void myHandler(int numBytes)
Wire.onRequest(handler)
Description: Register a function to be called when a master requests data from this slave device.
Parameters: handler: the function to be called, takes no parameters and returns nothing, e.g.: void myHandler()
There are both 7- and 8-bit versions of I2C addresses. 7 bits identify the device, and the eighth bit determines if it's being written to or read from. The Wire library uses 7 bit addresses throughout. If you have a data sheet or sample code that uses 8 bit address, you'll want to drop the low bit (i.e. shift the value one bit to the right), yielding an address between 0 and 127.

Home built test console for ATMEGA168 Arduino
Pictures and schematics:
Digital inputs for ATMEGA168/Arduino
Digital outputs for ATMEGA168/Arduino
ATMEGA168/Arduino test jig part A
ATMEGA168/Arduino test jig part B
LED Circuits connected to ATMEGA168/Arduino
Using the ATMEGA168/Arduino with a DC motor
Using the ATMEGA168/Arduino with a relay
Basic relay