Monday, February 17

Buttons and the MCP23017

Lets talk about the MCP23017, Buttons and the Raspberry PI GPIO Board.

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


Monday, February 10

An Introduction

My amazing girlfriend (yes a geek with a girlfriend), gave me a Raspberry PI for Christmas. Wow I thought the possibilities are endless. What am I going to do with it?

So many projects, 1 Pi and so little time. My choice to build a Games Arcade Machine for my mancave.

Yes I know there are many many tutorials out there on how to do it, what to do etc etc. But I decided that not all the tutorials are complete. Some only show you how to connect 2 buttons and a joystick to the Pi, others show more information. I decided 3 weeks into my project that maybe it would be a good idea to blog what I have done. So that maybe I could help others.

So the project: Build a retro game machine using a Raspberry Pi.

I am at the wiring up stage, but I will go back and document everything I have done so far.

But first, this initial post.

About me and this project.

So I am a geek, I love computers and anything related. I have been programming for a very long time. I started off on a Commodore VIC 20, programming in BASIC. Later I moved to a 386 DX 40, and coded in PASCAL. I am now a professional programmer and I code in just about anything the client wants.
Java, C, C++, C#, PL/SQL are the languages I am busy with at the moment. I have an Hons Degree in Computer Science from the University of Pretoria, and have been busy with a MSc in forever.

But the idea of this project is not to do something that I know. It is to build an awesome toy from scratch and learn something while doing it.

So besides the fact that I have a Computer Science Degree I decided to drop myself in the deep end:


  • I am going to do woodwork, something I did in grade 8 and 9 and never done it since.
  • I am going to try my hand at electronics, so I did a course in microprocessors but I have never really played with something like this so its going to be fun. (MCP23017 to be exact)
  • I am going to try and learn Python, well enough to get by and build my arcade machine. I did 2 weeks of Python in 1999 as part of a programming languages course, so it has been a while.
  • Graphics and vinyl for the machine. (GIMP)
  • Power Supplies and Electricity (Way beyond varsity physics or wiring a plug :), this time I could break something )


Most importantly the Raspberry PI and how it works.

So as you can see, I am a novice. I am going to try things I have never done before and in the end I hope to learn something, have fun and have something to show for my time.

I would appreciate comments, suggestions.

I shall post pictures, videos and source code. I will also store all the code I generate on GitHub and make it opensource.

I hope that you enjoy the blog as much as I enjoy making a games arcade.