simonclausen
Posts: 7
Joined: Thu Jul 11, 2013 12:06 pm

Getting past the 8 GPIO I/O pin limit

Thu Jul 11, 2013 4:34 pm

I am setting up a RPi to read the state on a number of inputs.

The source I need to read is either off or on, so that part is pretty simple. I use a relay to short a I/O pin and then read if it's on or off.

The problem is I will need to read the status of 25 of these on/off inputs. Do any of you kind people have a suggestion for extending the capability of the Pi?

Thank you!

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Getting past the 8 GPIO I/O pin limit

Thu Jul 11, 2013 9:32 pm

How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

simonclausen
Posts: 7
Joined: Thu Jul 11, 2013 12:06 pm

Re: Getting past the 8 GPIO I/O pin limit

Thu Jul 11, 2013 10:07 pm

I thank you. My search fu was not up to the task :)

PiGraham
Posts: 3939
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Getting past the 8 GPIO I/O pin limit

Fri Jul 12, 2013 6:59 am

The RasPi has 17 GPIO on the main header, and another 4 on the P5 header on a Rev2 board. Some have alternate functions, but they can all be configured as general purpose I/O.
You can scan 25 switches with 10 GPIO lines. Connect the switched in a matrix with one pin connected to all switches in the same row and the other pin connected to all switched in the same column. Wire each row to one of 5 GPIO outputs through 1k resistors and wire each column to one of 5 GPIO inputs.

Set a row output high then read each column input. If an input is high it means the switch at that row and column in the matrix is pressed. Clear the output then set the next one along high and read the inputs again. In five write/read steps you have read 25 switches.

You can use this scanning method to read up to 110 switches on a Pi with no IO expander.
10+11 = 21 GPIO lines
10 * 11 = 110 matrix points.

For such large numbers of switches dedicated interfaces make more sense in most cases, but it can be done.
Note that each matrix point is a switch link between two lines. You can't use matrix scanning to read lots of logic signals (more hardware would be required).
Since each point is only read when row is set and column sampled very short switch activations could be missed. This isn't a problem with push buttons / keyboards but may be an issues with some devices.

You can do the same with output, but often you will want the output to stay active between matrix scans. You could run 25 LEDs wired in a 5x5 matrix (which could be any physical arrangement), but each LED will be on for only 1/25th of the time, so it won't be as bright as it could be. This isn't a sound design because if, for any reason, all ten GPIO were on at the same time, perhaps because the wrong GPIO program is run by mistake, all the LEDs would be on an that would overload the Pi, possibly damaging the Pi.

simonclausen
Posts: 7
Joined: Thu Jul 11, 2013 12:06 pm

Re: Getting past the 8 GPIO I/O pin limit

Sat Jul 13, 2013 5:31 pm

I was not aware that the specific use GPIO's could be reconfigured like that.

Thank you for the info and the suggestion on setting the switches up in a matrix. It seems like a solid way to read the many inputs without adding the extra complexity of additional device.

Return to “Automation, sensing and robotics”