Previous Entry Share Next Entry
i2c IO Expander
tibman wrote in arduino_related

Need more digital IO ports?

Another i2c post. This one is about a 16 port IO Expander that works well with the arduino. I'm driving a bunch of leds with it now, i guess you could get eight of these things and drive 128 leds through i2c if you wanted.

Part Description: IC I/O Expander I2C 16B 28SDIP
Digikey part number: MCP23016-I/SP-ND
Price from Digikey: $1.90 US on 20 Sep 09

You will also need two additional parts to support the chip.
1x 33pF Cap
1x 3.9k Resistor

Wiring Diagram

Once the chip is setup, it's very easy to use. The device's i2c address is 0100xxx. Connecting an addr pin to ground makes it '0' and to +5v makes it '1'. This gives you enough addresses to use eight expanders at the same time. Also, this device didn't like i2c bus speed over 100k :(

I ended up writing a library (class) to manage the IO expander. It's kind of big so i'm just going to put the important bits here.

//Basic Write command
void Write(byte reg, byte data1, byte data2)
  Wire.send(reg); // Command

//Read inputs
void Read()
  //Set read from GP0
  Wire.send(0x00); // Command: Access to GP0
  //Recieve bytes from module
  Wire.requestFrom(addr,2); //only two bytes
  { io_data[0].set(Wire.receive()); io_data[1].set(Wire.receive());}
  else {Serial.println("ERROR: request from GP0 failed");} //FIXME - this should be removed

The Datasheet has a list of commands. The important ones are 0x06 (io direction) and 0x00 (pin status). All reads and writes have to be done in pairs. So even if you only want to read one pin, you have to read two bytes (16 bits) and extract the one you want.

I had a difficult time doing bitwise stuff (my first time really) and made a debug function to help. If you are having problems i recommend something like this, it helps so much.

I think it's a great little chip. If you ever feel like your arduino is running out of IO space, drop $2 into one of these. Well worth it!

Two leds and a Button hooked up to the IO Expander

  • 1

Re: IO Expander Code

No problem, it gets a lot easier once you get the chip setup and talking to the arduino. The library i made is still around but i'd like to rewrite it. Initially it was using an array of boolean values to hold the bits.. which turned out to be very inefficient. I discovered that bitwise operations is the way to go and hardcoded those operations into the sketch.

The important thing is that you can read and write bytes to the chip. The addr is a BYTE that is 01000000 if all the chip address pins are grounded. Using the Wire library you can fully communicate with the IO expander. Are you using the same chip from this tutorial?

I'll be visiting family this weekend but can rewrite this tutorial with the entire sketch posted, if that will help? I come from a programming background and have a hard time with the electronics parts instead of the coding ones... looking at this tutorial it isn't very helpful from a software viewpoint, hah. Sorry about that.

  • 1

Log in

No account? Create an account