![4017_KM 3x3 Matrix Finished Product](https://arduinoplusplus.files.wordpress.com/2016/02/4017_km-3x3-matrix-finished-product2.jpg?w=150&h=148)
The basis of this post is the information found in the article here and its follow up here. These articles describe a technique that I thought looked interesting and decided to do a mini-project to test it out, avoiding some of the questionable variations described in those articles.
How it works
![4017_KM IC DIP](https://arduinoplusplus.files.wordpress.com/2016/02/4017_km-ic-dip.png?w=300&h=225)
In this situation, the CLOCK and RESET are directly controlled by the microcontroller and the decoded output (1 of 10) is routed through a switch to a single microcontroller input pin. By advancing the counter (toggling the clock), and checking the input pin, the microcontroller can identify when a key has been pressed. In the case of a keypress the circuit will be closed and the input pin will be driven high. A low signal indicates that the switch is open circuit and therefore not pressed.
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 4017 or by using a matrix style arrangement. The nice thing is that at all times the interface to the microcontroller 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 4017 counter’s remaining synchronized with the microcontroller counter. This is easiest done by explicitely resetting the 4017 at the start of every scan.
Hardware Implementation
As a proof of concept, and to test the software library described below, I implemented a 3×3 key matrix matrix using one 4017 IC. The circuit for the small keymatrix and the resulting board layout are shown below.Scaling the circuit up for more switches should be straightforward. The final product is show below, but I would recommend placing the tact switches on the other side of the PCB to make it a more practical keypad.
![4017_KM 3x3 Matrix Finished Product](https://arduinoplusplus.files.wordpress.com/2016/02/4017_km-3x3-matrix-finished-product2.jpg?w=300&h=297)
Software Implementation
To test the hardware, I implemented an Arduino library and test code to scan the key matrix and return the key identifier(s) for any keys that are pressed.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.
In conclusion, this technique is very effective I can see that this arrangement using the 4017 is relatively straightforward and has the potential to save a lot of I/O pins