Saturday, September 22, 2012

So Many Inputs, So Few GPIO Pins

I keep coming up with so many improvements to my interface design (I'm up to version 6 now) that I'm not getting much built.  I decided that I had to make the pins for the SPI and I2C buses available.  But that means using five GPIO pins that I had other plans for.  I have to have at least eight inputs to handle the motion detectors and door sensors for the alarm system.  At least four relay outputs would be nice too, so the Raspberry Pi is quickly running out of GPIO pins.
Time to get a little help from another useful IC - the multiplexer.   There are several possible choices, but the obvious one is the 74151, of which I conveniently happen to have a few.

This diagram shows how this needs to be connected.  GPIO pins 1, 2, and 3 are set to output mode and used as address lines to select which input to read.  GPIO pin 0 is set to input mode and connected to the output of the multiplexer.

(Note: I always refer to WiringPi pin numbers, not the standard  BCM numbering.)

The result is that the interface can still have 8 inputs, but only use 4 GPIO pins to do it.

I will probably do the same thing (in reverse) to allow two GPIO pins to provide four select lines for the SPI bus.  It may be a while until I do that since the SPI bus is for future expansion.  I don't have any devices for it yet, just ideas.


  1. Very informative blog!
    I am trying to use the 74LS151 with piface, again to increase the number of inputs i can read. I connected pin 5 directly to one of piface inputs, but it seems that reading is not reliable. Any ideas what might me wrong? Is piface input incompatible with TTL levels?

  2. The PiFace is based on the MCP23S17 SPI GPIO extender, which I believe is compatible with TTL levels. As long as the address pins on the 151 are stable (i.e. not changing too quickly) then I see no reason for this not to work.
    Try testing the 151 without the PiFace. Use a voltmeter to see if the output pin is what you expect for the given inputs.