Electronic – How to read an large array of switches with some microcontroller

matrixswitches

I have been asked by someone whether or not I could do some programming on a large array (something like 18 x 16) of analogue switches.

I would want to wire up an array of analogue switches (you know, on-off toggle switches) that are in a grid pattern with rows and columns so that I can at a single moment understand the state of all those switches and do something with code on the basis of it.

I realize that microprocessors have limited GPIO's, I would need 288 ports to read that many switches. What is a circuit I can use to reduce the number of ports to something more manageable like less than 48?

Best Answer

For your example of an 18x16 matrix, you will need a maximum of 34 GPIO lines. One side will be configured as push-pull outputs, and the other as inputs.

Here is a smaller matrix as an example:

enter image description here

We will use the columns as inputs, and the rows as outputs. Initially set all of the outputs low.

Configure the column inputs to use internal pull-down resistors, or supply external ones (e.g. 100K).

When reading the matrix, one by one change one of the row pins high. Then look at the column pins, and see if any are high instead of normally low (due to the pull-downs).

For example, if SW2 is enabled, then Col1 will be high only when Row2 is high.

The diodes prevent feedback when multiple switches are thrown (thanks, Tut).

You can save another 12 pins by using a 4-to-16 line decoder such as the 74HCT7514 to drive the row outputs, so now you need just 22 GPIO pins to read the 288 switches.