# Novel idea implements low-cost keyboard

-April 03, 2003

Many applications that use a microcontroller also use a keyboard. If your application uses a relatively powerful microcontroller, you can use several free I/O pins or an unused input with an ADC to effect an easy keyboard connection. But, if the microcontroller in your system has too few free I/O pins and no on-chip ADC, you can be in trouble. However, if your system doesn't require a high-performance keyboard, you can solve the problem by using the circuit in Figure 1. How does it work? At system initialization, the I/O connection is an output, set to logic 0; hence, C is discharged. In reading the keyboard, the following steps take place:

1. I/O (output) assumes the state logic 1, VOUT.

2. VC charges to logic 1 (VOUT) or to a voltage that RS and the other resistors determines. (You can set the output I/O to logic 1 by default. In this case, you can omit steps 1 and 2, and the routine becomes faster. This design uses 0 instead of 1 to have an inactive signal on the line when the keyboard is not checked.)

3. I/O becomes an input.

4. For a duration TMAX, the microcontroller checks the input I/O to see whether it resets to logic 0.

5. If, after TMAX, the input I/O is still at logic 1, no button has been activated.

6. If within TMAX, the input I/O resets to logic 0, the measured time indicates the activated buttons.

7. I/O becomes output again and resets to logic 0 to discharge C.

Several equations describe the operation of the scheme. First, assume some conditions: VOUT is the voltage of the output I/O at logic 1; VTH is the threshold for logic 0 input to the microcontroller; and RX is the value of the parallel combination of RA, RB, and the other resistors.

Figure 2 shows the timing diagram for the circuit of Figure 1. You can evaluate the duration of TX with the following expression: TXRXCloge (VC/VTH). If RX is not negligible with respect to RXMIN (but the RINPUT of the microcontroller greatly exceeds RX), then

where VOUT is the voltage at logic 1 on the I/O output. From the last equation, a condition for RX is:

Note that, if RA, RB, and the other resistors form an R-2R string, RXMIN is approximately equal to RA/2. RS limits the current from the microcontroller and must have a minimum value of VOUTMIN/VOUTMAX. This resistor creates a delay for charging and discharging C of approximately 5RSC. The following is an example of a small keyboard with four buttons: To choose RS, IOUTMAX of the microcontroller is 25 mA at VOUT=5V, so RSMIN≥200Ω. So this design uses RS=220Ω. RA, RB, RC, and RD are 1, 2.2, 3.9, and 8.2 kΩ, respectively. You can select values that greatly exceed RS. In this case, the effect of RS is negligible, but you should then consider the effects of the input resistance of the microcontroller.

The duration between two measurements is approximately 2 µsec (Listing 1). With one byte, the maximum duration, TMAX, is 512 µsec (when no button is pushed). So, time TX with RXMAX (in other words, RD) must be inferior to TMAX. Assuming that VTH is 1.5V (minimum), the equation for TX becomes

So, at the beginning of each measurement, you must append a delay of 5×220Ω×47 nF=52 µsec to charge C. Figure 3 shows the waveforms at the I/O pin and the returned values with different button combinations. The power consumption of the circuit, with C=47 nF, VCC=5V, and a keyboard reading every 30 msec, is approximately 0.04 mW (practically negligible). You can use this scheme in all applications that don't require great accuracy or high speed.

Is this the best Design Idea in this issue? Select at www.edn.com.