Summary of Transforming your AVR Microcontroller to the I2C or TWI Slave I/O Expander Project
This tutorial demonstrates using an AVR ATmega168 microcontroller as an I2C slave device to expand I/O capabilities. It details the hardware setup involving an I2C master controller and various slave components like the MCP23008 expander, RTC, and sensors. The guide covers configuration for addressing up to seven devices on a single bus and provides software tools required for coding and debugging the embedded system project.
Parts used in the I2C Slave Microcontroller Project:
- AVRJazz Mega328 board (ATmega328P) as I2C master
- JazzMate MCP23008 board (MCP23008 8-bit I2C I/O expander)
- AVRJazz Mega168 board (ATmega168) as I2C slave
- WinAVR GNU C compiler
- Atmel AVR Studio 4
- STK500 programmer with STK500 v2.0 bootloader
The I2C bus (read as I squared C) is one of the most important embedded system serial bus interface first introduced by Philips in 1980; using just two lines called SCL (serial clock) and SDA (serial data) respectively make the I2C bus is a perfect choice to provide additional I/O capabilities to your microcontroller project without changing your microcontroller type and design in order to increase the I/O port pins.
Today there are plenty choices of I2C slave I/O port expander devices available on the market such as Philips PCF8574 and Microchip MCP23008 for addressable 8-bit general I/O ports which capable of handling I2C standard bus speed of 100Khz or fast speed mode of 400Khz; the Microchip MCP23008 even can handle up to 1.7Mhz bus speed. You could read more information about using I2C interface on my previous posted blog How to use I2C-bus on the Atmel AVR Microcontroller.
The usage of I2C slave devices is not merely for the I/O expander but it also use to expand your microcontroller capabilities as well as it’s functionalities such as the I2C real time clock from Maxim DS1307 (Using Maxim DS1307 Real Time Clock with Atmel AVR Microcontroller), Microchip TC1321 10-bit digital to analog converter (DAC), Microchip MCP2331 12-bit analog to digital converter (ADC), Microchip MCP9801 high accuracy temperature sensor and Microchip 24AA128 16KB EEPROM.
Although there are many types of sophisticated I2C slave devices available on the market, sometimes we need the I2C slave device which has the capabilities beyond that, something more powerful, more flexible and yet easy to be configured; the answer to this requirement is to use the microcontroller it self as the I2C slave device, that is why most of the midrange class microcontrollers have the I2C slave peripheral feature build inside it. This is what we are going to learn on this tutorial, where the principal we learn here could be applied to other microcontroller type as well.
The following is the list of hardware and software used in this tutorial:
- AVRJazz Mega328 board from ermicro as the I2C master controller which base on the AVR ATmega328P microcontroller (board schema).
- JazzMate MCP23008 board from ermicro which base on the Microchip MCP23008 8-bit I2C I/O expander.
- AVRJazz Mega168 board from ermicro as the I2C slave device which base on the AVR ATmega168 microcontroller (board schema).
- WinAVR for the GNU’s C compiler
- Atmel AVR Studio 4 for the coding and debugging environment.
- STK500 programmer from AVR Studio 4, using the AVRJazz Mega328 and AVRJazz Mega168 boards STK500 v2.0 bootloader facility.
The MCP23008 8-bit I/O Expander I2C slave
The Microchip MCP23008 will be a perfect model for our I2C slave device as it has the 3-bit configurable address (000 to 111) which provides up to 7 devices that could be attached to the I2C bus which give total of 56 ports. MCP23008 also come with 11 internal register to control its operation
As the I2C slave device, mean the MCP23008 will be the passive device that depends on the I2C master device to initiate the communication. On this tutorial I will use the AVRJazz Mega328 learning board as the I2C master controller; the following is the C code for the I2C master controller.
For more detail: Transforming your AVR Microcontroller to the I2C or TWI Slave I/O Expander Project
- What is the primary function of the I2C bus?
The I2C bus provides additional I/O capabilities to a microcontroller project without changing the microcontroller type or design. - How many addressable devices can the MCP23008 support?
The MCP23008 has a 3-bit configurable address allowing up to 7 devices to be attached to the I2C bus. - Can the MCP23008 handle fast speed mode?
Yes, the Microchip MCP23008 can handle up to 1.7MHz bus speed. - Which boards are used as the I2C master and slave in this tutorial?
The AVRJazz Mega328 board acts as the master, while the AVRJazz Mega168 board serves as the slave device. - What software tools are required for this project?
The project requires WinAVR for the GNU C compiler and Atmel AVR Studio 4 for coding and debugging. - How does the MCP23008 communicate on the bus?
As an I2C slave, the MCP23008 is a passive device that depends on the I2C master to initiate communication.


