I/O Expander Shield with MCP23017

From LinkSprite Playgound
Jump to: navigation, search

Description

I/O Expander shield is a shield used to expand the number of I/Os of an Arduino Uno. It is based on the chipset MCP23017. The chipset MCP23017 communicates with Arduino Uno through I2C interface. It adds GPIOA and GPIOB, a total of 16 I/Os. There are two LEDs and two buttons on the shield. User can decide if they want to use the built-in LED/buttons or not by using the 4-bit DIP switch.

IOExpand shield 1.jpg IOExpand shield 2.jpg

Schematics

Code

<syntaxhighlight lang="c">

  1. include <Wire.h>

const byte mcp_address=0x20; // I2C Address of MCP23017 Chip const byte GPIOA=0x12; // Register Address of Port A const byte GPIOB=0x13; // Register Address of Port B const byte IODIRA=0x00; // IODIRA register const byte IODIRB=0x01; // IODIRB register

void setup() {

 //Send settings to MCP device
 Wire.begin();              // join i2c bus (address optional for master)
 Wire.beginTransmission(mcp_address);
 Wire.write((byte)IODIRA); // IODIRA register
 Wire.write((byte)0x03); // set GPIOA-0/GPIOA-1 to inputs
 Wire.endTransmission();

}

void loop() {

 Wire.beginTransmission(mcp_address);
 Wire.write((byte)GPIOA); // set MCP23017 memory pointer to GPIOB address
 Wire.endTransmission();
 Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
 int inputs=Wire.read(); // store the incoming byte into "inputs"

 if((inputs&0x01)==0)
 {
   Wire.beginTransmission(mcp_address);
   Wire.write(GPIOA);    // address bank A
   Wire.write((byte)0x04);  // value to send GPIOA-2 HIGH
   Wire.endTransmission();
 }
 if((inputs&0x02)==0)
 {
   Wire.beginTransmission(mcp_address);
   Wire.write(GPIOA);    // address bank A
   Wire.write((byte)0x08);  // value to send GPIOA-3 HIGH
   Wire.endTransmission();
 }
 else
 {
   Wire.beginTransmission(mcp_address);
   Wire.write(GPIOA);     // address bank A
   Wire.write((byte)0x00);  // value to send GPIOA LOW
   Wire.endTransmission();
 }

} [/syntaxhighlight]

Resources