Electronic Design

# Use a tiny microcontroller with a large keypad

Like a student needing just one more night to prepare for a coming exam, a microcontroller designer often wishes for just one more I/O line as a project approaches its final stages. Even for the relatively capable 8X752 microcontroller at 28 pins, with its high processing power, direct connection of a 5 × 5 keypad would steal 10 I/O lines from a total of 21 lines, or would require a special interface.

For a small, low-cost 16-pin microcontroller like SGS-Thomson’s ST6201/2, with only 9 I/O lines, the prospect of using any keypad isn’t a glamorous one. Fortunately, the microcontroller designers have included an 8-bit analog-to-digital converter (ADC) into this microcontroller. The ADC may not be completely suitable to monitor high-resolution sensors, but it can be an excellent resource for scanning a keypad of theoretically up to 256 keys. Without being too ambitious, I will describe a connection for a 16-key keypad. Having 16 keys makes the design (hardware and software) much simpler, but the following guidelines also will work for a larger number of keys.

The 4 × 4 keypad shown is a typical layout, except that the rows and columns are separated by resistors (see the figure). The resistance between point A and ground—R(n)—depends on which key (n) is pressed: 0 Ω if the first key is pressed, 0.5 kΩ if the second is pressed, and so on with steps equal to 0.5 kΩ, up to 7.5 kΩ if the 16th key is pressed. R(n) = (n − 1) × 0.5 kΩ. The current source will excite the resistor network and the resulting voltage drop will be measured by the ADC. For a 0-5 V analog-to-digital range, a current source slightly higher than 2.5 V/7.5 kΩ = 0.33 mA is suggested. For instance, the value 0.34 mA fits the bill; it will offset the voltage drop, excluding jumps of 2 LSBs due to possible analog-to-digital conversion noise. Also notice that only half of the maximum input range is used. This makes it possible to:

1. Get a 17th condition: No key is pressed when the ADC data equals 0xFF.
2. Get the pressed key’s number by shifting the analog-to-digital conversion data three bits to the right (or division by 8).

The larger the keypad, the more attention you must pay to the resistor values and tolerances, ADC noise, and the accuracy of the current-source setting. In addition, if the result of the 256 divide by keys’ number isn’t power of 2, your software will be a little more complicated. Another approach might be just to use the second analog input for an additional 16 keys.