Using Four MCP23017’s to Control 64 Reed switches

This section describes how to read the 64 Reed switches directly in Python using Symbus. In my original build I used a centipede board, which is just 4 MCP23017’s stuck together.

I’ve built the full 64 grid board as described here and it works!

Each MCP23017 can control 16 inputs or outputs. For our chess computer we are only interested in inputs (the Reed switches). So there’s 4.

The basic approach is described in this excellent tutorial.  It describes the basics of wiring an MCP23017. An important point to note is that we are using four, so each must have its own address that is set as part of the physical wiring. You can have up to eight MCP23017s. We just want four and I have chosen the addresses 0x21,0x22,0x23, 0x24 (see the Python Code)as 0x20 is used by another component. 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 incorporate a Mux.

mcp23017-addresspins1-300x203

Python Program to read MCP23017s

This approach uses SYMBUS to read the MCP23017, so you need to install that first. (I tried using WiringPi, but there seem to be version problems and I couldn’t get it to work)

My code for this routine is in this Zip file:

mcpscanv2

 

You should be able to figure out whats going on by comparing my code with its comments to the tutorial. The differences are that I set up internal pull ups, which simplifies wiring

Its all done by comparing binary values in registers (1 for open, 0 for closed), which makes it lighting fast and very efficient.

The program reads the board and prints out the x,y coordinates of a square and how it changes (open or Close).

Wiring for the four MCP23017

is shown below, but again it is just an extension of the tutorial.

This is how you wire up one MCP23017:

max23017_bb

For the whole board you just wire up four of these, but change the wiring of A0, A1 & A2 which changes the I2C address

The complete board: (slight error on number 3 but you get the idea)

fourmax23017

 

 

8 Responses to Using Four MCP23017’s to Control 64 Reed switches

  1. Jason Wenger says:

    This whole problem can be solved with a lot less wiring and a lot less interface logic. All you need is 8 DO and 8 DI, or 4 DO and 16 DI, etc, wired through a switch matrix. Here’s a more detailed look.

    https://howtobuildapinballmachine.wordpress.com/tag/matrix/

    Essentially, you have a set of ‘row enable’ DO pins, and only one is pulled high at a time. Then you read values off the columns, which are tied to weak pulldowns. Each switch connects a row and a column, but to prevent complex paths through multiple switches on inactive rows leading to weird results, each switch is wired inline with a diode.

    In the case of a chessboard, they’re actually physically arranged in a matrix — On pinball machines, they’re usually scattered all over and only in a matrix logically.

    • Max says:

      You are right the Solus project uses this approach and wiring is described in detail. I chose the simple all switch version, because it is simple to wire and test. The logic is also very simple. You loop around 64 switches testing for close or open. Not the most elegent but it works well.

  2. Plamen Petkov says:

    Indeed a classical X-Y scanning scheme could be used.
    To reduce numbers of I/O, two 8 bit shift registers ‘164 could be used as X and Y drivers, interfaced by 2 wires (SD and SCLK) and two chip selects.
    For change detection a complete set of 8×8 bits from previous scan would have to be compared to similar one from current scan.

  3. Ron says:

    Hi there,
    I have builded this configuration exactly as drawed above on a breadboard with 4 MCP23017 and used some buttons attached to it.
    Ofcourse I configured/wired the address individually (0x21 until 0x24)
    I tested each IC individually connected to the I2C bus to see if it works fine, that is:
    – address okay
    – if I press a button it states Open, if I release a button it states Close

    Now I connected all of the IC’s on the I2C bus and now I get an unwanted behaviour:
    The first IC (0x21) when pressed the attached button it states Open, if I release a button it states Close
    The second IC (0x21) when pressed the attached button it loops unendly between the states Open and Close, not exactly what you want for reading a chessfield……

    Next I measured the Voltage on the Vcc and ground for each IC but this is 3.9V for each one of them so that seems to be okay.

    What can be the cause here ??

  4. Ron says:

    sorry, typo in typing THIS message(not in my experiment); second IC = 0x22 not 0x21.

  5. Max says:

    Ron,
    I am sorry, but I have not looked at this project in detail in over 3 years. Are you running your software from a Pi or an Arduino?
    From what I remember, the controlling software, needs to define a switch as input or output, see the NOX example for Pi. Also when you attach multiple MCP23017s to a PI the cumulative resistances can be a problem. The Arduino is more robust. This is why I ended up using a switch.

  6. Ron says:

    Hi Max,
    Thx for the quick reply. I indeed use a Pi. The switches are fine, as I said it does a good job running standalone. But as soon it is connected to the I2C bus the problem comes in.
    The cumalitive resitances sound more plausible.
    I just ordered an 8 channel I2C hub so that I can give each MCP it’s own I2C channel.

    • Max says:

      Ron,
      Yes I tried that see Design for easy to build one system Chess Computer it made things better, using a TCA9548A I2C multiplexer, but mine was still flaky and I got bored before I could solve it.
      However you may want to look at the Centipede Shield which I used in my first project with an Arduino. It is essentially four MCP23017s on a pcb. It worked first time, but with an Arduino, but I see no reason you should not address it as 4 x MCP23017s. If you want one, I have a few spare and could create a listing on ebay for them at £15. (a bargain)

      However if you do intend to proceed with this project I strongly recommend you use an Arduino to control the board. Its what Arduinos are good at, the Pi is too precious and susceptible to voltage etc variations. Arduinos are cheap and tough as old boots.

Leave a Reply

Your email address will not be published. Required fields are marked *


*