Electronic Design

# Connect any keyboard with any microcontroller using only one pin

After “Use A Tiny Microcontroller With A Large Keypad” was published (ELECTRONIC DESIGN, Sept. 2, 1997, p. 166), I got a number of responses that prompted me to develop a corollary design. Presented here is a design applicable to microcontrollers not equipped with a built-in ADC.

Looking at the figure, you’ll notice a familiar 4x4 keypad with a single difference —each row and column are separated by a resistor. If key #1 is pressed, the resistance between nodes A and B (R1AB) is equal to 1.5*R; for key #2—R2AB = 1.5*R+R = 2.5*R; and so on ... key #N—RNAB = N.5*R. This variable resistor network is connected to a 555 timer (CMOS version), configured as an oscillator with the period of oscillation equal to T = 1.4*RAB*C, directly proportional to RAB.

The oscillator runs only when a key is pressed, causing an interrupt request for the microcontroller. The INT0 pin is set up as edge-sensitive. The general algorithm can be described as: 1) after detecting the edge on the INT0 pin, wait 20 ms to eliminate ringing; 2) detect the next edge and start the internal timer; 3) the following edge stops the internal timer. The measured period (the time between two consecutive positive edges) will define the key number. Any microcontroller with a built-in timer (the vast majority of microcontrollers) can implement this idea.

For a specific example, I will use the 8051-family microcontroller with a 12-MHz clock. In this situation, the single count of the timer will be 1 µs. If we choose 47 nF for the capacitor and 3.9 kO as the resistor value, we will have T = 1.4*R*C = 256.6 ˜ 256 µs. That is the period difference between oscillations produced by two serial keys. After calculating the period interval produced by any key pressed, we need only to make integer division by 0xFF (25610) to get the key number of the depressed key. The value for R1 =1.5*R is chosen to provide the highest error margin for the elements by moving the period value in the middle of two consecutive numbers.

Some possible sources of errors in this application are: 1) component tolerances; 2) 256.6 µs instead of 256 for 1% resistors; 3) the closest value to 4*R, if R = 3.9k, will be 15.4K or 15.8K instead of the calculated 15.6k., an additional 1% error for each following column; 4) the 555 oscillator inaccuracy—temperature drift of 75 ppm/C° and variation 0.03% for a power-supply variation of ±0.1 V.