Before I start, please download the MCP23017 Manual. Then search Google for the Raspberry PI GPIO layout or follow these links below:
Now that you have this, please study it and study it some more. You need to know what revision of the board you have. I have a revision 2 board. So I shall continue as if it is a revision 2 board. Please note that revision 1 has a different PIN layout and that the MCP23017 will be on BUS 0, while revision 2 is on BUS 1. But don't worry about that for now.
So why the MCP23017. We for starters I wanted a lot of buttons:
- 4 System buttons (Enter, Pause, Tab and Escape)
- 2 Joysticks which each have 4 buttons
- 6 fire buttons for each player (2 players)
- 2 coin buttons
- And an on off button which will be added later
That's a total of 26 buttons. The PI has 26 Pins on the header GPIO, and some more if you are willing to solder. Of these 26 Pins, some have special purpose, some are for voltage and others are ground. So you are left with 8 default IO Pins, and more if you configure them correctly. This would not be enough for me.
In addition I did not want run the risk of breaking my Pi. So the solution was the MCP23017. One MCP23017 provides an additional 16 GPIOs that you can use.
What is really cool is that you can string the GPIOs together.
I don't know much about electronics except for microprocessors in varisty. So I studied what I could find on the web. My bible ended up being the MCP23017 Manual and an article on Raspberry Pi Spy.
If you follow the website and the design, you should be able to turn on an LED like I did.
LED as demonstrated on Raspberry Pi Spy |
The commands to activate the LED |
After following the command steps, you can jump into some basic Python Code.
The Python Code was great, however it lacked a few things:
The board used resistors for the buttons and secondly checking which button was pressed was done in a loop.
I knew that the Pi has internal Pull Up and Pull Down resistors. So I was wondering if the MCP23017 has the same, which it does. I modified the Python code to use this feature of the IC.
In addition I wanted to move away form a loop. The MCP docs states that it has an interrupt facility. So I enabled mirroring on the interrupts. Meaning that the events on bank A and bank B were ORed to both INT A and INT B. This allows me to run one wire from the IC to the Pi. Then treating the PIN on the Pi as a button I could use the interrupt functionality on the Pi.
In otherwords, I wait for an input event on the Pi more specifically Pin 17. If an input occurs, I know it was triggered by the MCP23017. I then ask then MCP23017, which Pin caused the interrupt and check what the value is of the Pin. This is all well documented in the manual.
In addition, I wanted to run more than one MCP, so I wired 2 together in parallel, and changed the address of the second, (2 ground, 1 to VCC). You can see the wiring in the pics below.
I then wrote up some Python code and added it to GitHub.
The code has enough comments in it to explain exactly what it does.
As for the wiring, the SDA and SCL of the Pi are wired directly to both MCPs. I use the 3.3V of the Pi has the VCC, and Gnd of the Pi as the ground of my circuit. INT A of MCP B is wired to Pin 17 and INT A of MCP A is wired to Pin 18.
In addition MCP B has an address of 0x20 (All address pins wired to ground) and MCP A has an address of 0x21 (A0-A1 is ground and A2 is VCC)
All buttons are wired to the IO Pins of the MCP and to ground.
You can see this from my pictures below:
In addition I wired everything to chocolate blocks so that I can easily connect and disconnect the buttons.
I stuck everything to the box with double sided tape.
The GitHub code is configured for Mame4All, but you can change it as you see fit.
I hope this was straightforward and you don't have any questions, but if you do please feel free to leave some.
Top view of my controllers, B (left), A (right) |
Addresses of the ICs |
NO is connected to the IO and Gnd to Ground |
MCP23017 |
My Pi and my Controller in their boxes |
View of the wires that go to the Pi |