4017 IC switch matrix controller and library. Up to 100 switches with 3 pins!

I recently came across these articles describing an implementation of one or more 4017 decade counter ICs used to implement a keyboard matrix.

I thought the idea and approach looked interesting and decided to do a mini-project to test it out.

Diodes isolate the 4017 counter outputs in the event that two or more switches are closed at the same time, allowing simultaneous presses to be detected properly. The number of switches connected can be increased by cascading multiple CD4017 or by using a matrix style arrangement. The nice thing is that at all times the interface to the Arduino remains just 3 pins (Clock, Reset and the digital output from the matrix). Two counters in a matrix arrangement can be used to monitor up to 100 switches, still just using 3 interface pins.

According to the articles, Reset can be omitted but I decided to leave it in because reliable operation following the initial power-up reset depends on the CD4017 counter’s remaining synchronized with the Arduino counter. This is easiest done by resetting the 4017 at the start of every scan.

I have also implemented a scalable library to read the key matrix using the 4017 IC. The library

  • Allows definition of the size of the keyswitch matrix
  • Manages the scanning of the 4017 to read the matrix
  • Detects the transition from OFF state to ON state for each key
  • Implements software debounce and software auto repeat
  • Allows detection of multiple simultaneous key presses.

As a proof of concept, and to test the software, I implemented a 3x3 matrix. Attached are JPG images for the circuit, board layout and final product. It works really well and I can see that this keyboard matrix arrangement has the potential to save a lot of I/O pins. Scaling my circuit up to more switches should be straightforward.

The library and all associated documentation, including Eagle CAD files, can be found at my code repository in the signature block below.

3x3 Matrix PCB.JPG

Great find Mate!

Noted that you have cautiously avoided the dodgy details in the original suggestions.

If you are going to use the keyboard, you would of course solder the switches to the other side of the PCB.

I agree some of the suggestions were a bit 'dodgy', but the basic idea has merit. And yes, the switches can/should be soldered to the other side to make a usable keyboard :).

Dear Marco,
thank you for the share.

I used the 4017 matrix to control 37 keys of a small piano. I used your code but i dont know if i have to connect the reset output from arduino and where to connect.
For this reason I get different values for the pressed key if i switch off and then on the whole circuit.
Can you give any advice. I use 2 4017 connected as on the edn article.

Thank you in advance

You should connect a pin from the Arduino to the reset pin of the 4017, as the library relies on this for resynchronising.

MD_4017_KM(uint8_t numKeys, uint8_t pinClk, uint8_t pinKey, uint8_t pinRst);

The pinRst tells the library which the reset pin is. You can wire it up to any pin as long as you then tell the library the same pin.

Your are right.
To your example using one 4017 it is obvious how you done it.
But if you see the matrix example of the first edn article 4x3_matrix_2_4017

both reset are connected to outputs of 4017. If library can handle this i ll give a try connect both reset to arduino output.

Thank you for your fast answer.

The point of reset is to return the scanning to the zero position, so all 4017 resets need to be connected to the same Arduino output pin, as you seem to be now doing.

Dear Marco,

I had connect both MR (master reset) pins of 4017's to pin 10 of Arduino (i did changes today, to late for yesterday).
Now seems that every time finds the right key pressed but i dont know how to connect clock pins.
Acording to article with 2 4017's first ic clock comes form MCU and the last output goes to clock pin of second 4017.

Maybe I have to arrange different the connections. If I let it as is I got 2 issues.
Fisrt is that keys starts from number 4 and i dont have all numbers, last key doesnt work.

Do you have any idea?

Thank you in advance.


Not sure that I understand your problem.

The clock signal should go into the clock of the first chip. The output on pin 12 of the 4017 is the clock for the next chip. Do you have a datasheet for the IC or are you just relying on the article? You should always get a datasheet for the chip you are using and get used to reading how to use the chips from the sheets (not always easy!).