Skip to main content
Renesas Singapore - Knowledgebase

Key switch input in microcontrollers (part 2)

Latest Updated:11/01/2007


Key switch input in microcontrollers (part 2)


Generally, ports are used for key input in microcontrollers. However, when the number of available ports is limited, it is not possible to use several ports just for key input.
One solution to this problem is to use analog inputs for key inputs. The figure below shows an example where analog inputs are used for four key inputs.

When any switch from SW1 to SW4 is pressed, resistors are used to divide the signal so that different voltage is applied to each analog input pin (ANIn). By converting the voltage from analog to digital, it can be determined which key was pressed.

[Setting of resistance values]
The next question is how to decide which resistance values should be set. The first thing to be decided is the voltage value to be used as the standard for determining input. For example, let us suppose that a voltage range from 0 to 0.8VDD is divided in 0.2VDD increments. This would mean that the input voltage values would correspond to the pressed switches as follows.

0.8VDD or above   No switches pressed
0.6VDD - 0.8VDD   SW4 was pressed
0.4VDD - 0.6VDD   SW3 was pressed
0.2VDD - 0.4VDD   SW2 was pressed
0         - 0.2VDD   SW1 was pressed

When setting resistance values, a margin of 0.1VDD is used so that the values used for judgment are centered. When doing this, R1 (= 100) is taken as the standard.

First, when R2 is determined as 0.3VDD when SW2 is pressed, R2 = R1 * 3/7, thus 43 is the approximate value in an E24 series resistor. Next, when SW3 is pressed, R3 is determined as approximating 0.5VDD, where 56 is the approximate value in an E24 series resistor. Similarly, 130 is calculated from 0.7VDD for R4 and 620 from 0.9VDD for R5.

  Values based on R1
R1 100
R2 43
R3 56
R4 130
R5 620


Coffee break

Resistance values are never easily divisible, and must be considered approximations. This is because they are divided into a geometric series from 1 to 10. For general division methods, when there are 12 divisions in this series, the series is called an E12 series, and when there are 24 divisions, the series is called an E24 series. Accordingly, the values in an E24 series are 1.0,1.1,1.2,1.3,1.5,1.6,1.8,2.0,2.2,2.4,2.7,3.0,3.3,3.6,3.9,4.3,4.7,5.1,5.6,6.2,6.8,7.5,8.2,9.1. Within this series, the values marked in bold are the resistance values of an E12 series.

Resistance ratios are determined as described above. Next, absolute values are determined. To make the A/D conversion more precise, resistance values should be kept as small as possible. But larger values are needed to minimize the current flow. For this approach, A/D conversion precision is not as important as low current flow, so priority is given to minimizing the current.
If the result determined based on R1 is applied using kΩ, the total becomes approximately 1MΩ, which means that the current flow during standby mode (when no switches are being pressed) is approximately 5µA during 5V operation.

Using the example above, a 20% increment is set to make the interval as wide as possible, but in order to keep the current flow during standby as small as possible, the R5 value is made as large as possible (by raising the voltage value for when no switches are pressed). To do this, we might use the following voltage settings.

No switches pressed:   0.95VDD
SW4 is pressed:   0.3VDD
SW3 is pressed:   0.2VDD
SW2 is pressed:   0.1VDD
SW1 is pressed:   0

In this case, when R1 is 100kΩ, R2 is 11kΩ, R3 is 15kΩ, R4 is 16kΩ, and R5 is 1.8MΩ. The total value is approximately 2MΩ, which can reduce the standby current by about half.
For description of the control program used for this hardware, see the FAQ section entitled"Simple ways to use A/D converters"
n circuits described so far, a priority among keys has been determined, so that when two switches are pressed at the same time, only the higher priority key between them will be acted upon. (The priority ranking is SW1 > SW2 > SW3 > SW4.)
The circuit shown below assumes that two keys are never pressed at the same time. With this circuit, calculations are simple because resistance values can be set independently based on only one switch being pressed at a time. Another advantage is that there is zero current flow when no switches are being pressed.

Suitable Products