Keypad using ADC: a microcontroller application

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:
  3. If the “LEFT” button is pressed, the voltage to ADC will be:

  4. If the “RIGHT” button is pressed, the voltage to ADC will be:
  5. If the “SELECT” button is pressed, the voltage to ADC will be:
  6. If no button is pressed, the voltage to ADC will be:

2. 10bit ADC conversion

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

/*********************************************************/
// keypad using adc
// 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 read_adc()
{
  ............................................
  ... created by youself, how to read the adc
  ............................................
}

int get_key()
{
       int k_num;            // number of key
       int adc_read_val;     // conversion value

       // Convert voltage from voltage devider
       // This function must be adjusted to the microcontroller adc
       adc_read_val = read_adc();
       for (k_num = 0; k_num < NUM_KEYS; k_num++)
	{
	   if (adc_read_val < adc_button_table[k_num])
	      { return k_num }
	}
}

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

  // read key
  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 .

Avatar of Bambang Siswoyo

About Bambang Siswoyo

I am a college lecturer since 1988 at UB (University of Brawijaya) in the electronics department. My experience: Applications of Microprocessor system, Applications of Control System, Applications of Field Programmable Gate Array, Computer Programming in computer instrumentations. My hobby: Music especially playing the saxophone, hiking and exploring the countryside.
This entry was posted in Electronics and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>