Thursday, 8 August 2013

How to Interface 4x4 Keypad with PIC24, DsPIC33, DsPIC30 (16bit PIC microcontroller) Code + Proteus Simulation.

This post would provide a working example of how to interface keypad (4x4) with 16bit PIC microcontroller. There are three methods to Interface keypad which I know. 1) Scanning method. 2) Logic change interrupt method. 3) External Interrupt using IC. All of them depending upon design requirements and Pin resources available for interfacing. The first method requires 8 pins for interfacing. The second require 4 pins with logic change interrupt. And third required an external IC and 5pins in which one must be External interrupt pin.

In this particular post I will go with first scanning method. You may download code and Proteus Simulation from the download section at the bottom of this post. I am using MPLAB X IDE, C30 Compiler and Proteus Simulation on v8.0. This code is written in C Language and will work on PIC24, DsPIC33 and DsPIC30 (16bit PIC microcontroller) by slightly changing the code.

In this post I supposed that you know following things if not then please go through following posts before proceeding with this.

·         How to Blink an LED. (Click here)
·         How to interface an LCD. (Click here)
·         How to get input from push button. (Click here)

Proteus Simulation:

            In the following figure 1. Shows the scanning method circuit in which I enabled internal pullup resistors for ROWs (Pins RB8 – RB11) only which are configured as input pins.  Now columns are configured as output pins (Pins RB12 – RB15) and write 1to them. I connected LCD with lower 8 pins of PortB (Pins RB0 – RB7) whereas LCD RS (Register Select) and E (Enable) pins are connected to pins RA6 and RA7.

Figure 1. Proteus Circuit

In the following figure 2. Shows the recently pressed button.

Figure 2. Button Pressed.


In the following figure 3. This is main function in which I first initialize LCD, then I initialize keypad. In while loop I called GetKey() function which is responsible for get key from keypad this function return 0 if no key is pressed otherwise return the key. Which is then displayed on LCD.

Figure 3. Main Function.

In the following figure 4. We have InitilizeKeyPad() function which initialize the keypad rows as input and enabled their corresponding pullup resistors. Then it configure columns pins as output.

Figure 4. Initilize Keypad Function.

 In the following figure 5. We have GetKey() function which returns a value of key by calling another function named ScanKeyPad() which actually scans the keypad.

Figure 5. GetKey Function.

In the following figure 6. We have ScanKeyPad() which actually scans the keypad. Now this is the point how microcontroller scans the keypad for which key is actually pressed. Microcontroller writes zero to column pins one by one at a time and checks all the rows for becomes zero. If one column is zero and button is pressed, which actually shorts the corresponding row pins and it becomes zero too which means that this key has been pressed. Otherwise if key is not pressed the corresponding row pin is high though pullup resistors. I called one another function which is RowDebouncing() which actually waits for the key to be released in order to avoid bouncing effect of push button.

Figure 6. Scanning Keypad Function.

            In the following figure 6. Shows how the corresponding pin becomes zero if button is pressed (left) green track shows Zero and red track shows high voltage whereas arrows shows the direction of current.

Figure 7. Button Pressed.

Reading Suggestions:


You can download Code (MPLAB X and C30 compiler) and Proteus (v8) Simulation. Click here

For all topics Click here:

That’s all for this post hope you will learn please comment if you have any questions for upcoming posts please subscribe or follow.

All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2013 IbrahimLabs. All rights reserved.

Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...