Matrix

With the exception of analog inputs, all switches can be wired in a matrix. This is a technique often used in keyboards, keypads and other devices with many buttons. The basic idea is wiring the buttons in an array with rows and columns, and writing a code that “activates” each row at a time, checking all columns on that row for connection before closing the row and moving on to the next. Effectively scanning through the whole table of buttons. Instead of using 14 pins for 14 buttons, you can use 14 pins as a 7 x 7 matrix, allowing for 49 buttons.

The disadvantage is the required diodes for every switch and more complex wiring.

Find your pins

DDC is built around planning a table of switches, so naturally, when wiring the switches as a matrix, you'll just follow the the way you've planned it. Lets say your switch table consists of 4 rows and 5 columns. First thing is to determine which pins you're using. For these 4 rows and 5 columns, that's 9 pins.

This is an example if how to set it up.

  • Row 1 to 4: 15, 14, 16, 10

  • Column 1 to 5: 3, 4, 5, 6, 7

Of course, you're free to chose whichever pin as row/column. I usually wire my switches first, and then see where the wires will end up when they reach the microcontroller.

Wiring the switches

A button matrix can look complex, an even worse if you're including encoders, encoders with pushbuttons and hat-switches/7-way switches. However, in practice it is quite simple. It is the same connection repeated for each block in the matrix:

A wire goes from a column pin on the microcontroller --> to a diode --> to a switch --> back to a row pin on the microcontroller.

  • A switch with the matrix adress (1,2), or Row 1 Column 2, will be wired like this:

  • Adding switch with matrix adress (2,2) or Row 2 Column 2:

  • Adding switch with matrix adress (2,1) or Row 2 Column 1:

Switch pins

  • For any switch that only uses a single block in the matrix; a pushbutton, toggle switch, etc., which pin on the switch is connected to the row/column does not matter. Just pick something. Of course, if your button has more than one throw (for instance SPDT switch), make sure you pick pins that are actually connected when you press the button.

  • For all switches that uses more than 1 block in the matrix (with few exceptions) the general idea is that the switch has many paths to a common end point. For instance, with a 2-bit encoder, pins often labled as A, B and C (common). Where A and B are the two individual paths to C, which both can be open or closed, independently. As below.

As a general rule, all COMMON pins should go to a row. So pin A and B are on the column side (with a diode between microcontroller column pin and the switch column pins, facing in the switch's direction) and C goes to a row pin. This is why an encoder in the matrix setup take up two blocks (two columns) on the same row. And since no switches have more than one common (except encoders with pushbutton and 7-way switches, but these can also be regarded as two seperate switches), no switches can have more than one pin that goes to a row.

In the case of a 4-bit encoder, it is much the same, just that there now are 4 individual paths to the common pin. Here, pins 1-4 should each have their column pin, and pin 5 to a row. This kind of encoder takes up 4 blocks on the same row in the matrix setup.

Above is the button matrix used in the Dahl Design SW1 steering wheel. It includes pushbuttons, 2-way toggles, 3-way toggles, 2-bit encoders and 7-way switches. Note that the two commons on the 7-way switches (for the buttons and encoder) go to same row. They dont have to, but it makes the wiring easier. That also means all the blocks from this switch must be on that row, requiring the matrix to be 7 columns wide.

Here is an alternative way to wire the same 7-way switch, with the commons from the hat switch and the encoder going to different rows. The hat switch uses columns 1-5 on row 2, the encoder uses columns 1-2 on row 3. Essentially splitting the switch into two individual switches. This is better for a smaller matrix, in this case the project uses a 6 row 5 column matrix. The same wiring is possible for an encoder with a pushbutton.

Last updated