Electronic – Multiplexing Inputs on a microcontroller

inputmicrocontrollermultiplexer

I have a 4 way DIL switch that I use to provide input to a microcontroller, and I need to free up a port to use for something else.

Is there a way to multiplex 4 inputs into 3 microcontroller ports? I'm thinking that there may be some way to have 2 pins as inputs, and a 3rd output pin that can be driven high to read switch 1 and 2, and low to read switch 3 and 4.

My current simple design is shown below, can I do this with just 3 pins?

schematic

Best Answer

Charlieplexing takes advantage of tristate pins of the microcontroller, but it costs extra diodes and somewhat more complex programming.

With N IO-pins you can address N(N-1) switches / LED's.

The Charlieplexing with switches article describes how it works with switches and LEDs

Tree pin and 6 switch Charlieplexing