Read 100 Switches with 3 Digital Pins!

4017_KM 3x3 Matrix Finished ProductFor various reasons, digital I/O pins are always at a premium when implementing micro controller projects. There have been various multiplexing schemes described to allow more than one switch to be connected to a small number of pins, but some time ago I came across a very simple but highly scalable method that uses the 4017 decade counter to connect many switches (more than 100 if needed) to just 3 digital inputs.

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 DIPThe 4017 decade counter is a “5-stage divide-by-10 Johnson counter” with 10 decoded outputs and a carry out bit. The counter is advanced on the positive edge of the CLOCK signal when the CLOCK ENABLE signal is in the logical 0 state, and cleared to a zero count by a logical 1 on the RESET line. The 10 decoded outputs are normally in the logical 0 state and go to the logical 1 state only at their respective time slot, remaining high for 1 full clock cycle. The carry-out signal completes a full cycle for every 10 clock input cycles and is used as a carry signal (CLOCK) to any succeeding counter ICs. To put it simply, each time the clock is toggled one of the outputs is turned on, sequentially from 0 to 9 and then the carry, which can be used to clock another chip in the sequence. This way a scanning cycle can be established with 10 rows in one IC and 10 columns in another IC, or 100 switches.

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

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.

The library and all associated documentation, including Eagle CAD files, can be found at my code repository.

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.

Advertisements

4 thoughts on “Read 100 Switches with 3 Digital Pins!

  1. Hi,

    Is there an email address or someway I could get in touch with you? I’d like to ask some questions regarding a 10×10 version of this that I would really love to discuss with you 🙂

    Like

      1. Okay cool – few questions then:

        1. Do you build the PCBs yourself? Or is there a third party company you use to design them up?
        2. Could the buttons be closer together, almost touching? Or does that infringe on the PCB design?

        Basically I’m looking to build something similar that would be an array of 10×10 buttons, to be used under a battleships grid, so that when a ship is placed, an attached Arduino would know the exact ‘grid references’ that the ship is placed in. A built PCB seems to me the most direct route of doing this, as opposed to trying to wire the thing myself (I’m a software engineer, not so much on the hardware front), however I have no idea as where to start. I was hoping I could find individuals I could pay to build me said components but have had little luck with that.

        Thanks!

        Like

      2. I designed the PCB using Eagle CAD and then sent them for manufacturing at Seeed Studio (China). Then soldered the components myself. Seeed also offer an assembly process for prototype boards and will supply parts, but I have never used that service. I get most of my parts from Tayda Electronics online, but there are lots of suppliers.

        As far as the spacing that largely depends on the type of switch. As this was meant for human fingers I spaced them to suit. There are smaller switches (even surface mount ones) that could be packed in closer, depending on the application.

        As far as workflow is concerned, hardware design using CAD starts with the circuit, then that gets converted to a pcb design, manufacturing the PCB, assemble components and test. CAD is available free and there are many versions. PCB is relatively cheap and depends on the size of the board – cheapest boards at Seeed are about $1 each, minimum quantity 10, plus delivery – and take a couple of days to make plus delivery time.

        Hope that helps.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s