Working with TWI (I2C) sensors / Devices

 


 

 

Published on:

Designed by:

Programming language:

IDE:

Target MCU:

   3 October 2013

  Vassilis Serasidis on 29 September 2013   

   C

   AVRstudio 6

   ATmega8 (internal 8 MHz oscillator)

 

 

 

Introduction

 

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

START:

Start bit that sends the master device to the slave device.

Device address:

Master sends the 7-bit TWI slave device address.

WRITE:

This bit means that a write operation will be made from master to the slave device (logic LOW).

ACK:

The slave sends a logic LOW bit to the master to response that the byte had been sent from master, received by the slave device.

DATA:

The 8-bit data that master sends to the slave.

STOP:

The master sends a stop bit to terminate the communication between master and the current 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.

 

Master reads one byte from TWI slave device

 

START:

Start bit that sends the master device to the slave device.

Device address:

The 7-bit TWI slave device address.

WRITE:

This bit means that a write operation will be made from master to the slave device (logic LOW).

ACK:

The slave sends a logic LOW bit to the master for response that the byte had been sent from master, received by the slave device.

Word Address:

The slave device has data  in some address. This is the data address we want to read.

START:

Start bit that sends the master device to the slave device.

Device address:

The 7-bit TWI slave device address.

READ:

This bit means that a read operation will be made from master to the slave device (logic HIGH).

ACK:

The slave sends a logic LOW bit to the master for response that the byte had been sent from master, received by the slave device.

DATA:

The data that slave sends to the master.

NO ACK:

The master should not acknowledge the transfer but should sent a stop bit.

STOP:

The master sends a stop bit to terminate the communication between 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 16x2 or 20x4 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.

PCF8574T 

 

 LCD

D0

RS

D1

RW

D2

E

D3

BACKLIGHT PIN

D4

D4

D5

D5

D6

D6

D7

D7

 

If you don't want to use any of these TWI devices just open the AVRstudio 6 project I2C_sensors.atsln and comment-in the device you don't want to use. The main source code file is I2C_sensors.c .

 

Moreover, a USART library (Universal Synchronous and Asynchronous serial Receiver and Transmitter) is added in source code and initialized at 9600 bits per second in case you want to print data on serial port. Very useful.

 

Connections among TWI devices

All devices are connected to the same TWI bus (SDA for data and SCL for clock). The ATmega8 pin 27 (PC4) is SDA and pin 28 (PC5) is SCL. The R1 and R2 pull-up resistors are 4.7 kOhm.

 

 

Description of the project

 

The AVR initializes the USART at 9600 bps, the GY-26 digital compass, the 20x4 LCD with backlight ON, LIS302DL 3-axes accelerometer.

The 24c32 eeprom doesn't need initialization. All TWI slave devices use the default TWI slave address has been given from the factory.

  1. The AVR writes 5 bytes to 24c32 eeprom at address 0x00 - 0x04 with values 40-44 respectively.
  2. The AVR reads the 5 bytes from eeprom's address 0x00-0x04 and print the values on LCD on the 4th line. This is a test if the bytes were successfully written on eeprom.
  3. The AVR reads the day number (1-7)  the date DAY/MONTH/YEAR (DD/MM/YY) and time HOURS:MINUTES :SECONDS (HH:MM:SS) and prints the data on the 1st LCD line. If your DS1307 is not programed with the correct date/time edit the source code and comment-out the commands that sets the DS1307 Date and Time. Afrer source code re-compilation, program the AVR with the new hex file. Then, comment-out the commands who set the DS1307, re-compile thesource code and program again the AVR . The Date/Time settings can be done easily from the serial port by useng a serial terminal software such as termite, but this is a homework for you!
  4. The AVR reads th GY-26 digital compass degrees (integer 0 - 3650) and print the degrees on LCD (2nd line) with the appropriate form (ASCII string 0 - 365.0).
  5. The AVR reads the 3-axes LIS302DL accelerometer and print the values on the 3rd LCD line. X,Y,Z values are from -63 to +64.

 

Download AVR Studio 6 project with source code and libraries.

 

Created and published by Vassilis Serasidis on 3 October 2013