MCP23008 Tutorial

Im struggling with the I/O expander mcp23008.
I can't find any where to start, does anyone have any good links for a tutorial.
I just want to switch on LEDS for now.

later an take input from a keypad, and possibly run my LCD to save on some pins.

Microchip has an app note (1034) called "Unique features of the MCP23x08" that may help a little. The datasheet is also quite detailed, but not very "tutorial".

Have you googled for examples that people have done for other chips (like PICs)? A lot of micros are being programmed in C these days, so it's easier to crib from other chip's example apps.

Ran

I used the data sheet to wire the io expander and set the address pins to ground. Iv read a few thread about the mcp23016 but i dont know what im looking for to make this work on my chip.

It looks like you have to select GPIO register then write the bits you want to set. I think it automatically switches to output when you write to the GPIO register otherwise you have to initialize the 0x00 IODIR register to set input/output pins. This should be enough to get started and make sure everything is connected and working. Writing to other registers should work the same way. This should flash any LED connected to any pin(with series resistor).

#include <Wire.h>

byte i2cAddr=B0100000;//device ID

void setup(){
Wire.begin();

}

void loop(){

Wire.beginTransmission(i2cAddr);
Wire.send(0x09);//select GPIO register
Wire.send(B11111111);//set register value-all high
Wire.endTransmission();

delay(500);

Wire.beginTransmission(i2cAddr);
Wire.send(0x09);//select GPIO register
Wire.send(B00000000);//set register value-all low
Wire.endTransmission();

delay(500);

}

I've been working on a project in C that uses the MCP23016, but using a ATtiny26L and not the Arduino, but the idea should be much the same.

  1. initialise the Two Wire mode into master mode using the Wire Library Wire - Arduino Reference
#include <Wire.h>

void setup (){
    wire.begin();                      // initialise the wire library and hardware
}
  1. Using the wire library send a command to the IODIR register to set the pins how you like them, ie either inputs or outputs or a combination of both. If you have tied all 3 address pins A0, A1, A2 low then the MCP23008 has a base address in your application of 0x40, the wire library should take care of the rest.
void setup() {
    wire.begin();                      // initialise the wire library and hardware

    Wire.beginTransmission(0x40);      // start talking to the device
    Wire.send(0x00);                   // select the IODIR register
    Wire.send(0xff);                   // set register value-all high, sets all pins as outputs on MCP23008
    Wire.endTransmission();            // stop talking to the devicevice
}
  1. now you can send whatever you like to the GPIO register in much the same way:
void loop() {
    Wire.beginTransmission(0x40);      // start talking to the device
    Wire.send(0x09);                   // select the GPIO register
    Wire.send(0xff);                   // set register value-all high
    Wire.endTransmission();            // stop talking to the device

    delay(500);                        // wait for 1/2 a second

    wire.begin();                      // initialise the wire library and hardware

    Wire.beginTransmission(0x40);      // start talking to the device
    Wire.send(0x09);                   // select the GPIO register
    Wire.send(0x00);                   // set register value-all low
    Wire.endTransmission();            // stop talking to the device

    delay(500);                        // wait for 1/2 a second
}

You should now have a blinking LED (assuming it has been connected the right way around!)

Don't forget to put pull up resistors on the SDA and SCL lines, which are on Arduino analogue input pins 4 and 5. Anything from around 1.8k ohm to 4.7k ohm should work ok.

An interesting site with more information on using the Arduino Wire Library can be found here: http://www.neufeld.newton.ks.us/electronics/?p=241

still having no luck, tired differnt chips leds and code examples, address etc.

heres how its wired
pin1 to analog 4
pin 2 to analog 5
resistors from pin 1 to 5v
resistor from pin 2 to 5v
pin 3 4 5 to Gnd
pin 6 to 5v
7 8 float
pin 9 to GND

pin 18 to 5V

pin 10 to led -> resistor -> GND

I have a project which uses 2 MCP23016 to drive the 7 segment leds .
Most of the time if people are having problems with any i2c device is due to the same fault, addressing.

The address of MCP23016 is 0 1 0 0 A2 A1 A0 X

Wire library ignores X , but other i2c library don't !!! So the address of a MCP23016 is
0x20 + ( a2 * 4) + ( a1 * 2) + ( a0 )

I will reply to myself to add the bit I`ve missed !!!!

1/ the MCP23016 and MCP23008 use the same address.

O.k, now you have the correct i2c address you need to set the DDR( Data direction Register )

I missed the first time it's about the MCP23008 , so I will get details on that chip.

This is how to output with the chip

First the DDR is 0 and 0 = output , 1 = input

Wire.beginTransmission(0x20 + A address);
Wire.send(0); < ----- this is the DDR
Wire.send(0); < ----- all 0s = all outputs
Wire.endTransmission();

How control the chip the reg (GPIO ) that is 0x9

Wire.beginTransmission(0x20 + A address);
Wire.send(9); < ----- this is the GPIO
Wire.send(0); < ----- all 0s = all off ?. Wire.send(255) = all_on
Wire.endTransmission();
Easy as that

And don`t forget then

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
}

#include <Wire.h>

void setup() {
Wire.begin(); // initialise the wire library and hardware
}

void loop() {
Wire.beginTransmission(0x20 + 7);
Wire.send(0);
Wire.send(0);
Wire.endTransmission();

Wire.beginTransmission(0x20 + 7);
Wire.send(9);
Wire.send(255);
Wire.endTransmission();
}

would this addressing be right
connecting A2 A1 A0 straight to 5v

as this will still not light the led. My RTC works fine so i know the arduino works correctly.

would this addressing be right
connecting A2 A1 A0 straight to 5v

a2 , a1 , a0 all to 5v yes 0x27

as this will still not light the led ?????

If that does not light any led you have a problem .

heres how its wired

pin1 to analog 4
pin 2 to analog 5
resistors from pin 1 to 5v
resistor from pin 2 to 5v
pin 3 4 5 to Gnd
pin 6 to 5v
7 8 float
pin 9 to GND

Yes all looks o.k .. what resistors are you using ? pin 1 / 2

what about pin 20 vdd ?

very important if you have missed that !!!

i only have 18pins but i do have the VDD connected to pin 18 5v
i have 10k resistors between 5v and the and pin 1 and 2.

is the reset ment to be tied to 5v aswell?

I use 4k7 resistor , but 10k will do.

Yes Reset is high ( 5v )

oops , I`m looking at the ssop diagram , pin 18 vdd

pin1 to analog 4
pin 2 to analog 5

pin 1 = scl , pin 2 = sda

analog 4 = sda , analog 5 = scl

pin1 to analog 4
pin 2 to analog 5

CLUE !!!!!

thanx guys
managed to get peter247 code to work.
in frustration i started swapping cables around. started swapping arduino pin 4 and 5 and the led started flashing.

Can finaly finish my project.

Yep , a little ahead of you unless you didn`t read the last post.

just after posting iv seen peter247 reply.
thanx for time sorry for the newb mistake, im normally much more carefull id got into my head was an addressing problem, rather then the wiring.
it turned out it was both.

I also program it with a different library out side of Arduino.

The same chips address now becomes ;-

0x40 + ( a2 *8 ) + ( a1 * 4 ) + ( a0 * 2 )

That`s the problem with converting code !!!