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.


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:



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:


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)




13 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.

    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 ??

    • Tacker says:

      I don’t know that you’ll ever see this as it is now 2 years later, but maybe somebody else can benefit. The MCP23017 library from adafruit has the option to enable the built-in pull-up resistors. Once these are activated, the pin will be active low and should not be so sporadic.

  4. Ron says:

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

  5. Max says:

    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:

      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.

  7. Johan Jomy Kuruvilla says:

    Hey Max,
    I am indeed interested in your project and I would like to make one myself instead of using the led blinking to make move , I would like to make a robotic arm. I believe you would support and guide me through out the project.
    Thanking you

    • Max says:

      Johan, it is over three years since I built this project, so I don’t remember all the detail. You have my encoragement but I will not be able to provide support.
      My advice is to build it in a modular fashion and I would start with the robot arm commands.
      ie write a program that can perform the following functions with the robot arm.
      Lift Piece X, Y (ie E2)
      Place piece X,Y
      to accurately lift and place pieces on the board. With a special place for captures.
      So a move E2E4 becomes Lift(E2), Place(E4). If a capture is involed that would then have to be done first.
      If you can do this then you just substitute your robot routines for ones where the LED is switched on & Off
      Best of Luck!!
      People have had more luck with underboard movements, rather than arms.see

  8. 9acca9 says:

    You manage in other way the mcp23017 here that on tic tac toe. I mean on tic tac toe you have a1, b1, c1…(i mean in threes), then a2,b2,c2… son in the mcp is like this
    GPA “bacbacba”
    GPB “c………….”
    but here you put like this (for example)
    GPA “aaaaaaaa”
    GPB “bbbbbbb”
    (I haven’t soldered yet but I’m a little scared to do it this new way.
    Did you change the code here? I suppose I could modify it if not, but it adds more work (for me that I am ignorant)).
    In the tic tac toe it will be with this order the cables:
    GPA babababa
    GPB abababab


  9. 9acca9 says:

    Hi Max.
    Can you please tell me why you got that a0,a1,a2,a3,a4,a5… etc… i mean if you use that for each mcp ok, but you are using this in the others:
    a24,a25.a26, etc…
    Like if all mcp where taking as a large mcp… is that is acting like that?

Leave a Reply

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