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)




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

Leave a Reply

Your e-mail address will not be published. Required fields are marked *