MCP23008 and leds not working

Hello folks. In an effort to introduce myself to I2C, I wired a MCP23008 port expander to an Arduino Uno and some leds. Seemed pretty simple but I can't get it to work. Below is a image of the connections.

I've tried the two sketches below:

#include <Wire.h>



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

    Wire.beginTransmission(0x20);      // start talking to the device
    Wire.write(0x00);                   // select the IODIR register
    Wire.write(0xff);                   // set register value-all high, sets all pins as outputs on MCP23008
    Wire.endTransmission();    // stop talking to the devicevice
    Serial.begin(9600);
} 
 void loop() {
    Wire.beginTransmission(0x20);      // start talking to the device
    Wire.write(0x09);                   // select the GPIO register
    Wire.write(0xff);                   // set register value-all high
    Wire.endTransmission();            // stop talking to the device
Serial.println ("on");
    delay(500);                        // wait for 1/2 a second

    Wire.beginTransmission(0x20);      // start talking to the device
    Wire.write(0x09);                   // select the GPIO register
    Wire.write(0x00);                   // set register value-all low
    Wire.endTransmission();            // stop talking to the device
Serial.println ("off");
    delay(500);                        // wait for 1/2 a second
}
#include <Wire.h>

byte i2cAddr=B0100000;//device ID


void setup(){
  Wire.begin();

    
}

void loop(){

  Wire.beginTransmission(i2cAddr);
  Wire.write(0x09);//select GPIO register
  Wire.write(B11111111);//set register value-all high
  Wire.endTransmission();
 
  delay(500);
  
  Wire.write(0x09);//select GPIO register
  Wire.write(B00000000);//set register value-all low
  Wire.endTransmission();

  delay(500);

}

I think they are essentially the same.
Either sketch, the leds remain unlit all the time.
I ran I2C_Scanner and it came back with an address of 0x20.
When the first sketch was ran, it did print alternately 'on', 'off' every half second to the serial monitor.
I switched the data and clock signals to A5 and A4 and I reversed polarity on an led but the results were the same.
I keep thinking it's gotta be something simple but I can't spot it. Help Please. - Scotty

 void loop() {
    Wire.beginTransmission(0x20);      // start talking to the device
    Wire.write(0x09);                   // select the GPIO register
    Wire.write(0xff);                   // set register value-all high
    Wire.endTransmission();            // stop talking to the device
Serial.println ("on");
    delay(500);                        // wait for 1/2 a second

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

After you've been talking to the device? Does that make sense?

Of course not; probably a copy/paste error. Error removed from sketch and post. However, makes no difference in the results. - Scotty

the address is not 0x20 but 0x40 .
and i am missing this address when you send again, you do a wire.stop but no wirestart
check also if sequintial data register is set

    Wire.write(0x00);                   // select the IODIR register
    Wire.write(0xff);                   // set register value-all high, sets all pins as outputs on MCP23008

Doh!....
Go take another look at the IODIR bits in the datasheet... :wink:

--- bill

Bill, I don't understand.
According to the data sheet, the IO Direction Register address is 0x00.
When a bit is set, the corresponding pin becomes an input. When a bit is clear, the corresponding pin becomes an output.

  • Scotty

That is correct.
But then go back and look at your code where you are setting the pins to outputs
(The lines I pointed out) and compare it to what you just wrote..
You will soon be kicking yourself...

--- bill

All too obvious now. Dang it! I hate it when I do that. Thanks for all your input guys. All is good now. Moving on. - Scotty