Path: EDN Asia >> Design Ideas >> IC/Board/Systems Design >> Read 10 switches using only two I/O pins of MCU
IC/Board/Systems Design Share print

Read 10 switches using only two I/O pins of MCU

13 Mar 2013  | Aruna Rubasinghe

Share this page with your friends

There a number of ways to read multiple switch inputs using a reduced number of microcontroller-unit (MCU) pins. For example, you can use an analogue MCU pin to read multiple switches by assigning a unique voltage to each switch through a resistor network, or you can use a one-wire device, such as the Maxim DS2408 8-channel addressable switch.

The first method has several disadvantages: The MCU has to have an ADC function, debounce wait times reduce the polling rate, and an error results if the switch is opened during the ADC sampling time. The second method also has the drawback of comparatively low speed; it uses 1-wire communication, which requires continuous polling; and each poll generates an 8bit data sequence relevant to switch positions.

This Design Idea describes a method for reading multiple pushbuttons or open/closed switches using only two digital I/O pins and a timer interrupt of the MCU (figure 1). Optionally, a third I/O pin can be assigned to periodically reset the CD4017 (a cascadable decoded 1-of-10 Johnson counter) for reliable operation should an EMI or ESD event occur that could falsely clock the counter, or you can use the circuit shown in figure 2 and retain the two-pin feature. The diodes isolate the counter outputs in the event that two or more switches are closed at the same time. You can increase the number of switches connected by cascading multiple CD4017 ICs using a carry-out signal (pin 12) and a clock signal (pin 14).

Figure 1: You can easily expand this circuit to many more than 10 switches, yet still use only two MCU I/O pins, by cascading multiple CD4017 counters through their carry-outs to the following enables.

Reliable operation following the initial power-up reset depends on the CD4017 counter's remaining synchronised with the MCU counter. This synchronisation can be upset by an ESD or EMI event such as a nearby cell phone, so it would be wise to include in the firmware a periodic hardware reset to the CD4017 to keep the counts synchronised. Figure 2 shows how you can do this without having to use a third MCU pin.

Figure 2: You can add three resistors and a transistor to implement the occasional synchronising reset without using a third I/O pin.

For this function, you use the MCU's ability to keep its I/O pin in three different states: high, low, and, by temporarily changing the pin to an input, high impedance.

In the logic-high state, transistor Q1 turns on through R4, making the voltage on V1 logic high and the voltage on V2 below the logic-low level. This sets the clock pin to a logic high while keeping the reset pin at a logic low.

In the logic-low state, transistor Q1 turns off, making the voltage on V1 logic low and the voltage on V2 above the logic-high level. This sets the reset pin to logic high while keeping the clock in the logic-low state.

1 • 2 Next Page Last Page

Want to more of this to be delivered to you for FREE?

Subscribe to EDN Asia alerts and receive the latest design ideas and product news in your inbox.

Got to make sure you're not a robot. Please enter the code displayed on the right.

Time to activate your subscription - it's easy!

We have sent an activate request to your registerd e-email. Simply click on the link to activate your subscription.

We're doing this to protect your privacy and ensure you successfully receive your e-mail alerts.

Add New Comment
Visitor (To avoid code verification, simply login or register with us. It is fast and free!)
*Verify code:
Tech Impact

Regional Roundup
Control this smart glass with the blink of an eye
K-Glass 2 detects users' eye movements to point the cursor to recognise computer icons or objects in the Internet, and uses winks for commands. The researchers call this interface the "i-Mouse."

GlobalFoundries extends grants to Singapore students
ARM, Tencent Games team up to improve mobile gaming

News | Products | Design Features | Regional Roundup | Tech Impact