In this post, I will explain how to design a keypad by using an internal ADC of the microcontroller or microprocessor system.

ADC will be used to determine which any key is pressed in the state. In principle, there is a circuit that can generate a differrent voltages at each place of push button pressing. A simple circuit is by applying a voltage divider circuit using some  series resitors, which can lead to different voltages for each keystrokes.

For example, it is necessary to distinguish the five key functions in the program the microcontroller, the UP, DOWN, RIGHT, LEFT, and SELECT button. There are required 6 pieces of resistor: 2K, 220, 330, 1K, 3k3 and 10K, in order to generate the  rising voltage at ADC of microcontroller by successive key presses: UP, DOWN, LEFT, RIGHT, SELECT.

1. The circuit will work as follow:

1. If  the “UP” button is pressed, the voltage to ADC will be 0 Volt, because the 2K resistor will be pulled down to ground.
2. If the “DOWN” button is pressed, the voltage to ADC will be:
$\frac{330\Omega&space;}{330\Omega+2K\Omega}.5V=0.7082V$
3. If the “LEFT” button is pressed, the voltage to ADC will be:
$\frac{330\Omega+620\Omega&space;}{330\Omega+620\Omega+2K\Omega}.5V=1.6102V$
4. If the “RIGHT” button is pressed, the voltage to ADC will be:
$\frac{330\Omega+620\Omega+1K\Omega&space;}{330\Omega+620\Omega+1K\Omega+2K\Omega}.5V=2.4684V$
5. If the “SELECT” button is pressed, the voltage to ADC will be:
$\frac{330\Omega+620\Omega+1K\Omega+3K3\Omega&space;}{330\Omega+620\Omega+1K\Omega+3K3\Omega+2K\Omega}.5V=3.6207V$
6. If no button is pressed, the voltage to ADC will be:
$\frac{330\Omega+620\Omega+1K\Omega+3K3\Omega+10K\Omega&space;}{330\Omega+620\Omega+1K\Omega+3K3\Omega+10K\Omega+2K\Omega}.5V=4.4203V$

If the output of the voltage divider circuit (“to ADC”) fed to the 10bit ADC input, then any key presses will produce a decimal:

1. UP button: 0V will be 0 decimal.
2. DOWN button: 0.7082 Volt will be 145 decimal.
3. LEFT button: 1.6102 Volt will be 329 decimal.
4. RIGHT button: 2.4684 Volt will be 505 decimal.
5. SELECT button: 3.6207 Volt will be 741 decimal.
6. No Button: 4.4203 Volt  will be 905 decimal.

3. How to programming tricks

In fact every keystrokes are not exactly generate a voltage as described in point 1. Means a range slightly above or below the voltage. Thus, the voltage value of the middle is taken, for example: the “UP” from 0V to 0.7082 / 2 = 0.3541V, the “DOWN” from 0.7082V to 0.7082 + (1.6102-0.7082) / 2 = 1.1592 V, and so on.
More easily calculated from point 2, the decimal after being converted by the ADC.
1. The button of  “UP” on pressed state when the conversion result of ADC is defined below value of 145/2 or 72 decimal.
2. The button of  “DOWN” on pressed state when the conversion result of ADC is defined below value of 145+(329-145)/2 or 237 decimal.
3. The button of  “LEFT” on pressed state when the conversion result of ADC is defined below value of 329+(505-329)/2 or 417  decimal.
4. The button of  “RIGHT” on pressed state when the conversion result of ADC is defined below value of 505+(741-505)/2 or 623 decimal.
5. The button of  “SELECT” on pressed state when the conversion result of ADC is defined below value of 741+(905-741)/2 or 823 decimal.
6. No button on pressed state when the conversion result of ADC is defined below value of 905+(1024-905)/2 or 964 decimal.

C  Programming

/*********************************************************/
// created by: Bambang Siswoyo
// Electronic department - University of Brawijaya
/*********************************************************/

int adc_button_table[6] = { 72, 237, 417, 623, 823, 964 };
int NUM_KEYS = 6;
#define UP      0
#define DOWN    1
#define LEFT    2
#define RIGHT   3
#define SELECT  4
#define NOKEY   5

{
............................................
............................................
}

int get_key()
{
int k_num;            // number of key

// Convert voltage from voltage devider
for (k_num = 0; k_num < NUM_KEYS; k_num++)
{
{ return k_num }
}
}

void main()
{ // main program for testing only
int btn;

btn = get_key();
// action
switch(btn)
{
case UP:
//do something when var equals 0
break;
case DOWN:
//do something when var equals 1
break;
case LEFT:
//do something when var equals 2
break;
case RIGHT:
//do something when var equals 3
break;
case SELECT:
//do something when var equals 4
break;
case NOKEY:
//do something when var equals 5
break;
}

Last updated by at .