Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Interfacing Matrix Keypad with PIC Microcontroller – MikroC

Interfacing Matrix Keypad with PIC Microcontroller – MikroC

A 3x4 Matrix Keypad

Figure 1: A 3×4 Matrix Keypad

Keypads are small keyboards that are used to enter numeric, alphanumeric or select configuration data to microcontroller systems. Keypads are available in a variety of sizes. The common sizes are 3×3, 4×3 and 4×4 keypads.

Keypads are widely used input devices with lots of application in our everyday life, devices like telephone, ATM, electronic lock, Calculator, timers just to name a few, all have some sort of a keypad.

A matrix keypad is basically a combination of push-buttons in a way to form rows and columns. In this way the number of input/output pins necessary for their connection to a microcontroller is reduced. A 4×3 keypad requires 7 input/output pins instead of 12 and a 4×4 will require 8 input/output pins instead of 16 pins. In the matrix keypad switches are connected in a special manner as shown in figure 2 below.

4x4 Matrix Keypad structure

Figure 2: 4×4 Matrix Keypad structure         

The values of each key could be mapped according to ones specific project application. Figure 1, figure 3 and figure 4 show a typical keys mapping for a 3×4 and 4×4 Matrix keypads.


     Figure 3 Keys mapping with letters         Figure 4 Calculator Keys mapping

Detecting a pressed Key

A 3x4 Keypad connected to PORTB of a PIC Microcontroller

Figure 5: A 3×4 Keypad connected to PORTB

Assuming that the keypad is connected to PORTB as shown on figure 5, the steps to determine which key is pressed are as follows:

  1. A logic 1 is applied to the first column via RB0.
  2. Row pins RB4 – RB7 are read. If RB4 is 1, then it means key 1 is pressed, if RB5 is 1, key 4 is pressed and RB6 is 1, key 7 is pressed and if RB7 is 1, key * is pressed.
  3. A logic 1 is applied to the second column via RB1.
  4. The rows of pins RB4 – RB7 are read again. If RB4 is 1, key 2 is pressed, if RB5 is 1, key 5 and so on.
  5. The above process is repeated for all the three columns continuously.

 MikroC Pro for PIC Library      

The MikroC PRO for PIC provides a library for working with 4×4 keypad. The library routines can also be used with 4×1, 4×2, or 4×3 keypad. Below is a quick descriptions of the Keypad library, for more information, please visit online the mikroC pro for PIC Keypad Library page.

NB: This  following variable must be defined in all projects using Keypad Library:   


This function initializes the port for working with keypad. 

The global variable keypadPort must be defined first before using this function.



This function reads the key from keypad when key gets pressed. The code of a pressed key (1..16). If no key is pressed, returns 0.

The keypad has to be initialized first before calling this function.



This function waits until some key is pressed and released (this is a blocking call). When released, the function returns 1 to 16 (note it’s 1 to 16 and not 0 to 15), depending on the key. If more than one key is pressed simultaneously the function will wait until all pressed keys are released. After that the function will return the code of the first pressed key. The keypad has to be initialized first before calling this function.



This is a simple example of using the Keypad Library. It can support keypads with 1..4 rows and 1..4 columns. The code being returned by Keypad_Key_Click() function is in range from 1..16. In this example, the code returned is transformed into ASCII codes [0..9,*#] and displayed on Lcd. Figure 5 shows the circuit diagram of the example, a 3×4 keypad is used.

You can download the full project files (MikroC source code and Proteus Schematic design) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).

MikroC Source code: Interfacing Keypad with MikroC

Proteus Schematic: Keypad Proteus Schematic design

Share this post

Select your currency
EUR Euro
USD United States (US) dollar