The I2C is a multimaster, multislave serial single-ended computer bus and was invented by Philips in 1982. The atmel microcontrollers use a compatible to I2C serial bus that is named TWI (Two Wire Interface). The TWI supports serial communication at 100 kHz and 400 kHz. The master and slave devices that are connected to TWI bus have a unique address from 0-127.
You can say: “The I2C bus is used since 1982. Why you choose now to write this article ?”. The answer is that the last years many new sensors and devices ideal for hobbyists such as accelerometers, LCD, digital compass, ultra sound range finders etc have designed to work on TWI bus because TWI needs only two external pull-up resistors and is very easy to be handle by microcotrollers. Moreover, many people have wrote libraries for supporting these TWI slave devices. A TWI slave device is very easy to be handled for example by Arduino. In our case we will work with AVR studio 6 and the classic ATmega8 AVR that works at 8 MHz internal RC oscillator. The TWI speed is 100 kHz.
How TWI (I2C) bus works
Master writes one byte to TWI slave device
The master device (AVR) calls the slave devices by sending a START bit. Then, it sends the 7-bit slave address (0-127) and follows the Read or Write bit (R/W). Because the master needs to write to the TWI bus the R/W bit is logic ‘0’. After that, the slave device respond to the master by an ACK bit (Acknowledge = logic LOW). The master sends the number of bytes wants to the slave but after every sent byte the master waits for ACK bit from the slave device. After the last byte the master will receive the last ACK and then it will send the STOP bit to end the comunication between the master and the current slave device.
TWI devices used in this project
- 24C32: I2C 32kbit eeprom (4 kBytes x 8 bit = 32 kBits). Slave address 0xA0.
- GY26: I2C digital compass. Slave address 0xE0.
- LIS302DL: I2C 3-axes I2C/SPI accelerometer Slave address 0x3A. This sensor works at 3.3V DC (Vcc).
- DS1307: I2C Real-Time-Clock. Slave address 0xD0.
- PCF8574T: I2C-to-parallel converter. Usually used to drive dot matrix LCDs such as 16×2 or 20×4 characters. Slave address 0x4E. Some board designed to drive LCDs have different pin order from PCF8574T to LCD pins. Check your board version with an ohm meter.
For more detail: Working with TWI (I2C) sensors / Devices