Home LearningBasics Arduino and MCP23017 example

Arduino and MCP23017 example

by shedboy71

The is a 16-bit, general purpose parallel I/O port expander for I2C bus applications.

The 16-bit I/O port functionally consists of two 8-bit ports (PORTA and PORTB). The MCP23017 can be configured to operate in 8-bit or 16-bit modes. Lets look at the pinout

mcp23017 pinout

mcp23017 pinout

The MCP23017 is a +5V device. So we connect VDD to the 5V terminal of the arduino and of course we connect VSS to ground.
The GPB0-GPB7 and the GPA0-GPA7 pins are the 16 I/O ports.
NC is Not Connected.
SCL is the serial clock line. This connects to analog pin 5 on the arduino.
SDA is the serial data line. It connects to analog pin 4 on the arduino.
INTA and INTB are interrupt pins for the outputs. We are not using these here.
The RESET pin is if you want the outputs all reset to 0. We will connect this to +5V.
A0, A1, and A2 are the address pins. This a key thing with this device, you can in fact have 8 of these connected if you use a different address each time, this of course would give a huge amount of outputs potentially.

Here is a table of the address combinations, we will have A0, A1 and A2 tied to 0v, so that means its address 0x20

MCP23017 addresses

MCP23017 addresses

Schematic

We haven't shown the arduino here this shows the LED outputs only

mcp23017 and 8 leds schematic

mcp23017 and 8 leds schematic

Code

We are not using any library

[codesyntax lang=”cpp”]

#include "Wire.h"

void setup()
{
 Wire.begin(); // wake up I2C bus
// set I/O pins to outputs
 Wire.beginTransmission(0x20);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // set all of port A to outputs
 Wire.endTransmission();
}

void loop()
{
  Wire.beginTransmission(0x20);
  Wire.write(0x12);      // address bank A
  Wire.write((byte)0xAA);  // value to send - all HIGH
  Wire.endTransmission();
  delay(500);
  Wire.beginTransmission(0x20);
  Wire.write(0x12);      // address bank A
  Wire.write((byte)0x55);  // value to send - all HIGH
  Wire.endTransmission();
  delay(500);
}

[/codesyntax]

Links
2 Pcs MCP23017-E/SP MCP23017 DIP28

Share

You may also like