4x4 Keypad interfacing with AVR ATmega16/ATmega32

The keypad is an input device which generally used in applications such as calculator, ATM machines, computers, etc.

50.8k views2 min readUpdated Aug 9, 2023
4x4 Keypad interfacing with AVR ATmega16/ATmega32

Overview of 4x4 Matrix Keypad

The keypad is used as an input device to read the key pressed by the user and to process it.

The 4x4 keypad consists of 4 rows and 4 columns. Switches are placed between the rows and columns. A keypress establishes a connection between the corresponding row and column between which the switch is placed.

In order to read the keypress, we need to configure the rows as outputs and columns as inputs.

Columns are read after applying signals to the rows in order to determine whether or not a key is pressed and if pressed, which key is pressed.

For more information about the keypad and how to use it, refer to the topic 4x4 Keypad in the sensors and modules section.

4x4 Keypad
4x4 Keypad

 

Connection Diagram of 4x4 Keypad with ATmega16/32

Keypad Interfacing with ATmega microcontroller
Keypad Interfacing with ATmega16/32

 

 

 

Detect key pressed and print on LCD16x2 using ATmega16/32

Here, we are going to interface the 4x4 keypad with AVR ATmega16/ATmega32 and will display the pressed key on LCD16x2.

LCD16x2 is used here in 4-bit mode.

4x4 Matrix Keypad Code for Atmega16/32

/*
   4x4 Keypad Interfacing with ATmega16/32
   http://www.electronicwings.com
 */ 
 
#include "LCD16x2_4bit.h"
#include <avr/io.h>
#include <util/delay.h>


#define KEY_PRT 	PORTA
#define KEY_DDR		DDRA
#define KEY_PIN		PINA

unsigned char keypad[4][4] = {	{'7','8','9','/'},
				{'4','5','6','*'},
				{'1','2','3','-'},
				{' ','0','=','+'}};

unsigned char colloc, rowloc;

char keyfind()
{
	while(1)
	{
	    KEY_DDR = 0xF0;           	/* set port direction as input-output */
	    KEY_PRT = 0xFF;

	    do
	    {
		KEY_PRT &= 0x0F;      		/* mask PORT for column read only */
		asm("NOP");
		colloc = (KEY_PIN & 0x0F); 	/* read status of column */
	    }while(colloc != 0x0F);
		
	    do
	    {
		do
		{
	            _delay_ms(20);             /* 20ms key debounce time */
		    colloc = (KEY_PIN & 0x0F); /* read status of column */
		}while(colloc == 0x0F);        /* check for any key press */
			
		_delay_ms (40);	            /* 20 ms key debounce time */
		colloc = (KEY_PIN & 0x0F);
	    }while(colloc == 0x0F);

	   /* now check for rows */
	    KEY_PRT = 0xEF;            /* check for pressed key in 1st row */
	    asm("NOP");
	    colloc = (KEY_PIN & 0x0F);
	    if(colloc != 0x0F)
            {
		rowloc = 0;
		break;
	    }

	    KEY_PRT = 0xDF;		/* check for pressed key in 2nd row */
	    asm("NOP");
	    colloc = (KEY_PIN & 0x0F);
	    if(colloc != 0x0F)
	    {
		rowloc = 1;
		break;
	    }
		
	    KEY_PRT = 0xBF;		/* check for pressed key in 3rd row */
	    asm("NOP");
            colloc = (KEY_PIN & 0x0F);
	    if(colloc != 0x0F)
	    {
		rowloc = 2;
		break;
	    }

	    KEY_PRT = 0x7F;		/* check for pressed key in 4th row */
	    asm("NOP");
	    colloc = (KEY_PIN & 0x0F);
	    if(colloc != 0x0F)
	    {
		rowloc = 3;
		break;
	    }
	}

	if(colloc == 0x0E)
	   return(keypad[rowloc][0]);
	else if(colloc == 0x0D)
	   return(keypad[rowloc][1]);
	else if(colloc == 0x0B)
	   return(keypad[rowloc][2]);
	else
	   return(keypad[rowloc][3]);
}

int main(void)
{
	LCD_Init();
	LCD_String_xy(1,0,"Press a key");
    while(1)
	{
	    LCD_Command(0xc0);
	    LCD_Char(keyfind());       /* Display which key is pressed */
	}
}

Components Used

Powered byMouser Electronics
  • ATmega 16

    ATmega 16

    556-ATMEGA16L-8PU

  • Atmega32

    Atmega32

    556-ATMEGA32-16PU

  • 4x4 Keypad Module

    4x4 Keypad Module

    × 1

    Keypad is an input device which is generally used in applications such as calculator, ATM machines, computer etc.

  • LCD16x2 Display

    LCD16x2 Display

    485-1447

Downloads

Comments4

Join the discussion — share a question or tip.

  • AN

    Is this code only supports ps/2 keypads only or ethernet keypads also?

  • LO
    LoyikNziendolo

    bonjour besoin d'aide d'un code description j'ai un tableau a deux dimension de type char tab[100][100] et je emetre et recevoir des données vers un serial pc en utilisant la communication UART

  • MU
    muneneamos1999

    Please,also provide an alternative code for 4x4 keypad that uses one analogue pin

  • HI
    hiteshpatidar20

    please explain the coding how it is working