I recently came across these articles describing an implementation of one or more 4017 decade counter ICs used to implement a keyboard matrix. http://www.edn.com/design/power-management/4413677/Connect-a-4-3-matrix-keyboard-to-a-microcontroller-using-two-I-O-pins http://www.edn.com/design/components-and-packaging/4408027/Read-10-or-more-switches-using-only-two-I-O-pins-of-a-microcontroller
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 top 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.