Here is the breadboard image.
Here is the Arduino code.
// Read a button press using the MCP23008 I/O expander
// +------+
// A5 <--|1 18|--> +5v
// A4 <--|2 17|--> button --> GND
// GND <--|3 16|-->
// GND <--|4 15|-->
// GND <--|5 14|-->
// +5v <--|6 13|-->
// <--|7 12|-->
// <--|8 11|-->
// GND <--|9 10|-->
// +------+
// Libraries
#include "Wire.h"
// Base address of device
#define MCP23008_ADDRESS 0x20
// Registers
#define MCP23008_IODIR 0x00
#define MCP23008_IPOL 0x01
#define MCP23008_GPINTEN 0x02
#define MCP23008_DEFVAL 0x03
#define MCP23008_INTCON 0x04
#define MCP23008_IOCON 0x05
#define MCP23008_GPPU 0x06
#define MCP23008_INTF 0x07
#define MCP23008_INTCAP 0x08
#define MCP23008_GPIO 0x09
#define MCP23008_OLAT 0x0A
// Globals
static const uint8_t i2caddr = 0x00;
// Main
void setup() {
// Initialize the wire library
Wire.begin();
// Initialize the MCP23008.
// Set the IODIR register.
// GP0-GP7 are inputs.
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write((byte)MCP23008_IODIR);
Wire.write((byte)0xFF);
Wire.endTransmission();
// Set the GPPU register.
// Enable GP7 pull-up.
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write((byte)MCP23008_GPPU);
Wire.write((byte)0x80);
Wire.endTransmission();
}
// Execution loop
void loop() {
// Select GPIO register.
Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
Wire.write((byte)MCP23008_GPIO);
Wire.endTransmission();
// Read the GPIO register
Wire.requestFrom(MCP23008_ADDRESS | i2caddr, 1);
uint8_t data = Wire.read();
// Set the Arduino LED to the state of the button
digitalWrite(13, data & 0x80);
// Wait a tiny bit
delay(10);
}