0

MCP23017 I2C expander with TCA9548A multiplexer
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Please be positive and constructive with your questions and comments.

MCP23017 I2C expander with TCA9548A multiplexer

by Dougi22 on Sat Feb 16, 2019 6:47 am

Hi

I'm trying to build a chess board using reed switches to sense magnets on the bottom of the chess pieces. I have a raspberry pi connected to a TCA9548A multiplexer. On channel 0 I have an Adafruit 16×8 LED Matrix Driver Backpack which works well. I am trying to connect an MCP23017 i2c port expander to channel 1 to connect the reed switches. Basically following this approach:

http://chess.fortherapy.co.uk/home/a-wo ... -computer/

I'm doing my coding from scratch though and I'm having problems both understanding and getting anywhere with sensing the input from the reed switches. At this stage I'm just trying to get some kind of signal from the reed switches and have the following code:

Code: Select all | TOGGLE FULL SIZE
from Adafruit_GPIO import I2C
tca = I2C.get_i2c_device(address=0x71)
import time

def tca_select(channel):
    """Select an individual channel."""
    if channel > 7:
        return
    tca.writeRaw8(1 << channel)

tca_select(1)

IODIRA = 0x00
GPPUA = 0x0C

GPIOA  = 0x12 # Register for inputs

tca.write8(IODIRA,0xFF)
tca.write8(GPPUA,0xFF)

while True:
    a = tca.readU8(GPIOA)
    print a


I also attach an image of the wiring, which I've checked extensively and am pretty sure it's correct.

When I run this code I just get an integer value of 18 printed over and over. If I put a magnet next to the reed switch or if I swap the reed switch for a push to make switch and press it nothing happens. The 18 just carries on printing to the terminal.

My understanding is that when we read the register GPIOA = 0x12 with tca.readU8(GPIOA) we are reading a register that contains a byte array of values of 0 or 1. e.g. [0, 0, 0, 0, 0, 0, 0, 0] where each 0 or 1 represents the state of one of the So I'm unsure why I am seeing a value of 18 of datatype int?

I don't completely understand how I should be doing this, but I THINK the tca = I2C.get_i2c_device(address=0x71) bit basically creates the tca object which we use any time we want to read or write to/from an i2C device such as the led backpack, or in this case the MCP23017. So rather than using SMBus or similar we use the tca object. I then understand that before using the i2c device we need to change the multiplexer channel to the channel that the i2c device is connected to. After that point we can just use methods such as write8() and readU8() directly on the tca object to read and write to/from the i2c device.

In the chess tutorial he sets the i2c address for each of the MCP23017 components, but then goes on to say that this isn't strictly necessary because each MCP23017 is on a separate channel of the multiplexer. So although I have set the A0, A1 and A3 pins on the MCP23017 to give it an address of 0x21 as per the tutorial I don't think this is necessary?

So the question is, why am I seeing the 18 printed to terminal and am I approaching this in the right way?

Any pointers would be greatly appreciated. Thank you!
Attachments
IMG_0818.JPG
IMG_0818.JPG (151.25 KiB) Viewed 97 times

Dougi22
 
Posts: 9
Joined: Sun Dec 23, 2018 10:19 am

Re: MCP23017 I2C expander with TCA9548A multiplexer

by adafruit_support_mike on Sun Feb 17, 2019 3:03 am

Do connect the MCP23017's address pins either high or low. If they aren't connected to anything, their voltages will float, which basically means the chip will think it has different addresses at random times.

Also connect the /RESET pin to 3.3V unless you specifically want to reset the chip. Again, floating control pins cause trouble.

Regarding the TCA9548A, it looks like the person who wrote the page you linked wasn't aware of the MCP23017's address pins. You can have 8 MCP23017s connected to the same SDA and SCL lines as long as their address pins are set to different values. The way they're using the TCA9548A just gives you 8 MCP23017s that have the same address.

For starters, try connecting the MCP23017 directly to the RasPi. Get that connection working, and then you can worry about adding a multiplexer if you decide you need one.

adafruit_support_mike
 
Posts: 57106
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP23017 I2C expander with TCA9548A multiplexer

by Dougi22 on Sun Feb 17, 2019 3:25 am

Thanks Mike. I'll do exactly that :)

Dougi22
 
Posts: 9
Joined: Sun Dec 23, 2018 10:19 am

Re: MCP23017 I2C expander with TCA9548A multiplexer

by Dougi22 on Sun Feb 17, 2019 4:53 am

Thanks very much. That worked. I am now able to get a decimal value back that I can convert to binary to give a 1 or 0 for the state of each reed switch :)

The reason that the chessboard tutorial chap gave for using the multiplexer was as follows:

"Note that as explained elsewhere, 4 is about the maximum number of devices you can reliably attach to the Pi without some careful calculation of pull up resistors. So in the final design I incorporated a Mux."

I will need to connect 4x MCP23017s, an HT16K33 LED backpack (driving 64 leds), an LED display and a few switches.

Do you think that the multiplexer is unnecessary?

Thanks so much for your amazing support. It's very impressive and greatly appreciated.

Dougi22
 
Posts: 9
Joined: Sun Dec 23, 2018 10:19 am

Re: MCP23017 I2C expander with TCA9548A multiplexer

by adafruit_support_mike on Mon Feb 18, 2019 2:47 am

The RasPi's internal I2C pull-up resistors are kind of weak, and that can cause trouble for long runs of cable, but I2C is reasonably forgiving.

Adding 10k pull-ups on each MCP23017's SDA and SCL pins should keep things working. As a rule of thumb, you want about 1mA to 2mA flowing through the pull-ups when a line is pulled low. Anything less than 0.5mA, or more than 5mA, can get flaky.

The RasPi's internal pull-ups are between 40k and 70k, which puts the current between 0.05mA and 0.1mA.. okay for a couple of devices inside a cellphone (where the microprocessor was designed to work), but a little weak for larger projects.

adafruit_support_mike
 
Posts: 57106
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP23017 I2C expander with TCA9548A multiplexer

by Dougi22 on Mon Feb 18, 2019 3:01 am

Thanks. I'll take a look at how to add pull-up resistors. That's a new one on me, but sounds like it might be simpler than using the multiplexer.

Much appreciated :)

Dougi22
 
Posts: 9
Joined: Sun Dec 23, 2018 10:19 am

Re: MCP23017 I2C expander with TCA9548A multiplexer

by adafruit_support_mike on Tue Feb 19, 2019 2:14 am

Adding pull-ups is nothing more than connecting a resistor between the pin in question and (in this case) the 3.3V rail.

A GPIO pin configured for input has an equivalent resistance of a few million Ohms, and compared to that, a 10k resistor looks like a low-resistance connection to 3.3V.

A GPIO pin configured as an output and set LOW has an equivalent resistance of maybe 100 Ohms, and compared to that, a 10k resistor looks large. The GPIO pin can pull its side of the 10k resistor to GND without having to work too hard.

adafruit_support_mike
 
Posts: 57106
Joined: Thu Feb 11, 2010 2:51 pm

Re: MCP23017 I2C expander with TCA9548A multiplexer

by Dougi22 on Tue Feb 19, 2019 2:21 am

Thank you :)

Dougi22
 
Posts: 9
Joined: Sun Dec 23, 2018 10:19 am

Please be positive and constructive with your questions and comments.