Friday, December 30, 2011

MCP23008 - I2C Expander - Button

Alright, so the LED blink worked. Let's see if I can get the I2C expander to read a button press and turn off the internal LED on the Arduino board when the button is pressed.

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);
}

MCP23008 - I2C Expander - Blink

I have discovered the MCP23008 I2C expander chip. I think I'll try the digital equivalent of "Hello World!" and make an LED blink. If this chip works like I think it should, then perhaps I can use it to save pins on my motor controller / encoder project!

Here is the breadboard image.



And here is the Arduino code to exercise the chip.

// Blink an LED using the MCP23008 I/O expander

//        +------+
// A5  <--|1   18|--> +5v
// A4  <--|2   17|-->
// GND <--|3   16|-->
// GND <--|4   15|-->
// GND <--|5   14|-->
// +5v <--|6   13|-->
//     <--|7   12|-->
//     <--|8   11|-->
// GND <--|9   10|--> 330 ohm --> LED --> GND
//        +------+

// 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 is output GP1-GP7 are inputs.
  Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
  Wire.write((byte)MCP23008_IODIR);
  Wire.write((byte)0xFE);
  Wire.endTransmission();
}

// Execution loop
void loop() {
  // Set GP0 of the GPIO register.
  Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
  Wire.write((byte)MCP23008_GPIO);
  Wire.write((byte)0x01);
  Wire.endTransmission();

  delay(1000);

  // Clear GP0 of the GPIO register.
  Wire.beginTransmission(MCP23008_ADDRESS | i2caddr);
  Wire.write((byte)MCP23008_GPIO);
  Wire.write((byte)0x00);
  Wire.endTransmission();

  delay(1000);
}

Why do this?

I have tinkered with digital electronics over the years but have never kept a log book or record of the things that I've done. This blog is an attempt to record projects so that I can refer back to them in the future. If others find this useful, all the better.