Problem addressing two (or more?) I/O expanders via i2c

JayAuckland

New Member
Using this code: hi2csetup i2cmaster, 64, i2cfast, i2cbyte I can set up and communicate with a MCP23016 I/O expander at address 01000000 and do anything I want with it.
Substituting 0x40 for the 64 works just as well. I'm working on the implication that decimal 64 and hex 0x40 both equal binary 01000000, and the experience that either seem to work just fine.

However, when I connect a second MCP23016, setting its address pins so its address is 01000010, I can find no way to successfully communicate with it when substituting the 64 with either 66 (decimal equivalent of 01000010) or 0x42.

I've poked around the Forum and several semi-helpful web sites and postings and still can't quite get my head around how to do this properly, or what the principle behind it is? Can someone help me before my brain turns to mush?
 

AllyCat

Senior Member
Hi,

Yes you are correct that 64 (decimal), 0x40 or $40 (Hex) and %01000000 are just different ways to represent the same byte value, and 66, $42, etc. should work if (only) A0 has been changed to 1.

But are you sure that the address really is set to 001 ? Personally, I would just have tried all the possible addresses 64, 66, 68, 70, 72, 74, 76 and 78. Or only two days ago, hippy linked to a PICaxe program which can search the bus for any active Slave devices, see post #7 here.

Cheers, Alan.
 

lbenson

Senior Member
Re AllyCat's question, "But are you sure that the address really is set to 001"--is A0 on the MCP23016 pulled high? Or on some modules, is the pulldown resistor on A0 removed?

Scanning the entire I2C address range is a good idea.
 

JayAuckland

New Member
Hi guys and thanks for your prompt thoughts!
The second MCP23016 (they're on a breadboard) is definitely set for address 001. And when I take the first MCP23016 out of the circuit and change the second's address pins to 000 it responds exactly as the first one was doing.
Lol... I was contemplating working my way through heaps of addresses (66, 68, 70 etc.) one by one in the blind hope that suddenly something good would emerge but was hoping to avoid that by having a quick moan about my problem here in the forum!
I'll try Hippy's cool sounding code later today.
If you're under lock-down take care and don't go stir-crazy!
J.
 

JayAuckland

New Member
Thanks everyone, I found the problem... knowing how little knowledge I have of this stuff I too readily jumped to the conclusion that I was getting something wrong due to misunderstanding the coding aspects and had not triple-checked the breadboard jumper leads! Ruling out the decimal/hex aspect thanks to your reassurance I did a much more thorough hardware check and finally spotted that I had inexplicably reversed the order of the address pins and because it was so basic I never really thought to question it! I'm clearly becoming a catastrophist!

lbenson, you mention having a pull-up resistor on the address pin(s)... I've always connected address pins on things like eeprom chips etc. to either 0v or +5v without resistors and haven't had any obvious issues... are pull up and/or pull down resistors advisable?

J.
 

AllyCat

Senior Member
Hi
I had inexplicably reversed the order of the address pins
That's exactly what I suspected but, with little initial information, there were numerous other possibilities so I made my reply intentionally unspecific. Thanks for being honest. ;)
... are pull up and/or pull down resistors advisable?
For microcontrollers like the PICaxe, generally yes. The difference is that most pins are Input / Output (GPIO) and the program might "accidentally" set one or more as an output. That's quite easy because there are instructions to write to a whole port (8 pins) not just to a specific named pin. If the output is of a polarity opposite to the external link, then the pin current can be sufficiently large to damage the chip. That's particularly the case with pull-up links, because the internal pull-down FETs are "stronger" than the pull-ups, so the current will very probably exceed the ratings, and may be sufficient to fuse (blow) the internal bond wire. Also, there's little need to use external pull-ups because the PICaxe chips (particularly M2s) have internal "Weak Pull-up" resistors available on most pins (the PULLUP xx command).

Cheers, Alan.
 

Hemi345

Senior Member
lbenson, you mention having a pull-up resistor on the address pin(s)... I've always connected address pins on things like eeprom chips etc. to either 0v or +5v without resistors and haven't had any obvious issues... are pull up and/or pull down resistors advisable?

J.
For the address pins on most I/O expanders or EEPROM, I just tie them directly to ground or vcc. Like Alan stated, pullup/down resistors need to be used if the pins are I/O and not just inputs.
 
Top