Interfacing Matrix Keypad with PIC Microcontroller

Interfacing Matrix Keypad with PIC Microcontroller

This tutorial explains ‘What is a Matrix Keypad ?’ and ‘How to Interface Matrix Keypad with PIC Microcontroller ?’.

Matrix Keypad

Matrix Keypad is a very useful and userfriendly when we want to design certain applications like Calculator, Telephone etc. Matrix Keypad is made by arranging push button switches in rows and columns. Just imagine, if you want to interface a 4*4 (16 keys) matrix keypad with a microcontroller.  In the straight forward way, you will need 16 pins of a microcontroller for that, but by using a simple technique we can reduce it to 8 pins. In the matrix keypad switches are connected in a special manner a shown in the figure below.



Pressed keys can be detected by Scanning. For the sake of explanation, lets assume all column connections (Col1 – Col4) are input pins and all row connections (Row1 – Row4) are output pins. In the normal case (not scanning) all column inputs where in LOW (GND) state. For scanning keypad,

  1. A Logic HIGH signal is given to Col1 of column inputs.
  2. Then each Row output (row1 – row4) is scanned one by one. If any of the key belongs to first column is pressed, the Logic high signal from the Col1 will pass to that row. Through we can detect the key.
  3. This process is repeated for all columns if we want to detect multiple keys.

In this post I am explaining only about detecting one key at a time. For explaining the working I am using a 4*4 matrix keypad and the result is displayed in a Seven Segment Display. Matrix Keypad scanning is stopped as soon as any key press is detected and the Scanning is restarted if we need more inputs.

Interfacing with PIC Microcontroller

Matrix Keypad can also be interfaced with PIC Microcontroller using MikroC Libraries.

Circuit Diagram


Matrix Keypad interfacing with PIC MIcrocontroller

Matrix Keypad interfacing with PIC Microcontroller

Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.

Matrix Keypad is connected to the PORTB of the PIC Microcontroller. Each column of the Matrix Keypad is connected to RB0 – RB3 of the PIC Microcontroller, which are configured as output pins. While each row of the Matrix Keypad is connected to RB4 – RB7 of the PIC Microcontroller, which are configured as input pins.

Here I am using 4*4 matrix keypad, having characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, -, C, U, E, F. ‘B’ is replaced by ‘-‘ and ‘D’ is replaced by ‘U’ because Seven Segment Display is used for displaying characters. ‘B’ will be similar to ‘8’ and ‘D’ will be similar to ‘0’ when displayed in Seven Segment Display. For reading data for the Matrix Keypad, each column is made high and rows are scanned as I said above.

MikroC Programming

We use the function readKeyboard() to scan the Matrix Keypad and findKey() to find the pressed key. You can edit the function findKey() to change the character corresponds to each key of the Matrix Keypad.

Function to Scan Keypad

char readKeyboard()
 unsigned int i = 0;
  if(i == 0)
  PORTB = 1;
  else if(i == 1)
  PORTB = 2;
  else if(i == 2)
  PORTB = 4;
  else if(i == 3)
  PORTB = 8;

    return findKey(i,0);
   return findKey(i,1);
   return findKey(i,2);
   return findKey(i,3);
 return ' ';

This function initiates the keypad scanning and returns the character corresponds to the pressed key when a key press is detected. It uses the function findKey() to find the character corresponds to a particular row and column. In this function space (‘ ‘) is used as the null character, which is returned when no key is pressed, you may change this according to your needs.

Function to Find Keys

char findKey(unsigned short a, unsigned short b)
 if(b == 0)
   if(a == 3)
    return '0';
   else if(a == 2)
    return '1';
   else if(a == 1)
    return '2';
   else if(a == 0)
    return '3';
 else if(b == 1)
   if(a == 3)
    return '4';
   else if(a == 2)
    return '5';
   else if(a == 1)
    return '6';
   else if(a == 0)
    return '7';
 else if(b == 2)
   if(a == 3)
    return '8';
   else if(a == 2)
    return '9';
   else if(a == 1)
    return 'A';
   else if(a == 0)
    return '-';
 else if(b == 3)
   if(a == 3)
    return 'C';
   else if(a == 2)
    return 'U';
   else if(a == 1)
    return 'E';
   else if(a == 0)
    return 'F';

This function returns the character corresponding to a particular row and column. You may change characters corresponding to each key according to your need by editing this function.

Seven Segment Decoding Function

unsigned int sevenSegmentDecoder(char a)
   case '0': return 0x3F;
   case '1': return 0x06;
   case '2': return 0x5B;
   case '3': return 0x4F;
   case '4': return 0x66;
   case '5': return 0x6D;
   case '6': return 0x7D;
   case '7': return 0x07;
   case '8': return 0x7F;
   case '9': return 0x6F;
   case '0': return 0x3F;
   case 'A': return 0x77;
   case '-': return 0x40;
   case 'C': return 0x39;
   case 'U': return 0x3E;
   case 'E': return 0x79;
   case 'F': return 0x71;
   case ' ': return 0;

This function decodes the given character to display it in Seven Segment Display.

Reading Multiple Pressed Keys at a Time with PIC Microcontroller

You can download the MikroC Source Code, Proteus files etc here…

Matrix Keyboard PIC Microcontroller

Share this post

  • Mark Wallis says:

    or use a Texas Instruments TIL311 HEX LED display.

  • Temitayo Abel says:

    the function findkey(unigned short a, unsigned short b)
    is it the same by saying findkey(usigned short row, unsigned column) or probably findkey(usigned short column, unsigned row)???

  • Ligo George says:

    Yes, just turn on the led if it matches a particular key.

  • Mash says:

    How may I use this matrix key to make on and off the led.

  • Ligo George says:

    It is assembly language coding.

  • Gilbert Garcia says:

    is there anyone can code like this? HELP ME PLEASE ASAP!

  • Ligo George says:

    if(PORTB.F4) is same as if(PORTB.F4 == 1)

  • Iqbal Nur Fadillah says:

    In readKeyBoard function,there is state that { if(PORTB.F4) }..i dont understood the meaning of that.because that’s no condition..the condition is like this {if(PORTB.F4== 0)}…Please help me

  • Wendell Diniz says:

    Interesting tutorial. You could use b and d as minuscles, thus you can represent them in the 7-seg display, instead of using – and U. This will be more intuitive.

  • Ligo George says:

    Yes… by using analog to digital converter… search for it..

  • noufal says:

    Very helpful article. Is there any other way rather than continuous scanning?

  • Barath says:

    give me more explanation abt lcd commands

  • >