Mcp23008 - Anyone attempted this i2c expander?

I found half of the addresses, but the last I just cant find.

`
| A0 | A1 | A2
0 | | |
1 | x | x |

4 | | | x
5 | x | | x
`

This is a table over how the A0-A2 pins can be pulled to VDD to set an address. But how do I find the last? These were pretty easy, I just tried to put some wires in, and then tried a few numbers in the address variable, but the rest seems to be harder to find, any ideas?

Looks like your A1 pin isn’t connected properly. Each of A0-A2 should be tied either high (1) or low (0), and they set the address using binary arithmetic:

addr A2 A1 A0 0 | 0 0 0 1 | 0 0 1 2 | 0 1 0 3 | 0 1 1 4 | 1 0 0 ...etc

Peter

I just tried to pull A1 high, set address to 2, and I get nothing except errors.

Starting to getting it to do what I want… This code is displaying the last second in the current time. (left out the library that can be found on github)

It is a step towards my goal, but I am trying to find a better way to handle multiple ports.

What probably would work is to do like I did, and define each port from 0 to 5. (6 numbers on the final display with one shift register each)

But I would prefer not having long lists with just port0, port1, port2, and led1, led2, led3… and so on.

In my setNum function I did manage to cook it all down to just two lines of code, that just reads a string to determine if the pin should be set high or low. But haven’t found a way to do it so it also includes the port.

My idea was to just call setNum(3, 0); and it would then show a zero on the display with address 3.

`
// Configure I2C bus
hardware.i2c89.configure(CLOCK_SPEED_100_KHZ);

// Create i/o port instances (note: each device on the same bus should have a different device address)
i2cPort0 <- MCP23008(hardware.i2c89, 0); // pinstrapped to device address 0
i2cPort1 <- MCP23008(hardware.i2c89, 1); // pinstrapped to device address 1
i2cPort2 <- MCP23008(hardware.i2c89, 2); // pinstrapped to device address 2
i2cPort3 <- MCP23008(hardware.i2c89, 3); // pinstrapped to device address 3
i2cPort4 <- MCP23008(hardware.i2c89, 4); // pinstrapped to device address 4
i2cPort5 <- MCP23008(hardware.i2c89, 5); // pinstrapped to device address 5

// Assign leds to port pins
led1 <- i2cPort1.pin1; // MCP23008 has pin1 to pin8
led2 <- i2cPort1.pin2; // MCP23008 has pin1 to pin8
led3 <- i2cPort1.pin3; // MCP23008 has pin1 to pin8
led4 <- i2cPort1.pin4; // MCP23008 has pin1 to pin8
led5 <- i2cPort1.pin5; // MCP23008 has pin1 to pin8
led6 <- i2cPort1.pin6; // MCP23008 has pin1 to pin8
led7 <- i2cPort1.pin7; // MCP23008 has pin1 to pin8
led8 <- i2cPort1.pin8; // MCP23008 has pin1 to pin8

// Configure pins as output
led1.configure(DIGITAL_OUT);
led2.configure(DIGITAL_OUT);
led3.configure(DIGITAL_OUT);
led4.configure(DIGITAL_OUT);
led5.configure(DIGITAL_OUT);
led6.configure(DIGITAL_OUT);
led7.configure(DIGITAL_OUT);
led8.configure(DIGITAL_OUT);

ledTable <-[led1, led2, led3, led4, led5, led6, led7, led8];

numBits <- [“11011101”, “00010001”, “10111100”, “10110101”, “01110001”, “11100101”, “11101101”, “10010001”, “11111101”, “11110001”];

function setNum(addr, number)
{
for (local i = 0; i < numBits[number].len(); i++)
{
ledTable[i].write(numBits[number].slice(i,i+1).tointeger());
}
}

function updateDisplay()
{
local theTime = time().tostring();

setNum(1, theTime.slice(-1).tointeger());

imp.wakeup(1, updateDisplay);
}
updateDisplay();`

I think I came up with a good solution…

`// Configure I2C bus
hardware.i2c89.configure(CLOCK_SPEED_100_KHZ);

numBits <- [“11011101”, “00010001”, “10111100”, “10110101”, “01110001”, “11100101”, “11101101”, “10010001”, “11111101”, “11110001”];

function setNum(addr, number)
{
local i2cPort = MCP23008(hardware.i2c89, addr); // Create i/o port instances (note: each device on the same bus should have a different device address)

local led1 = i2cPort.pin1;
local led2 = i2cPort.pin2;
local led3 = i2cPort.pin3;
local led4 = i2cPort.pin4;
local led5 = i2cPort.pin5;
local led6 = i2cPort.pin6;
local led7 = i2cPort.pin7;
local led8 = i2cPort.pin8;

local ledTable = [led1, led2, led3, led4, led5, led6, led7, led8];

for (local i = 0; i < ledTable.len(); i++)
{
ledTable[i].configure(DIGITAL_OUT);
}

for (local i = 0; i < numBits[number].len(); i++)
{
ledTable[i].write(numBits[number].slice(i,i+1).tointeger());
}
}

function updateDisplay()
{
local theTime = time().tostring();

setNum(1, theTime.slice(-1).tointeger());

imp.wakeup(1, updateDisplay);
}
updateDisplay();`

Only problem I can see is that the pin is configured each time the display is updated, but I guess that would not really matter that much.