How to Interface Keypad with PIC Microcontroller

Interface Keypad with PIC Microcontroller

Interface Keypad with PIC Microcontroller

The keyboard is another commonly used input device. Likely a keypad is arranged as an array of switches that can be mechanical, membrane, capacity or Hall-Effect in construction.In mechanical switches, two metal contacts are brought together to complete an electrical circuit. In membrane switch, a plastic or rubber membrane presses one conductor onto another, this type of switch can be made very thin. The capacitive switch internally comprises two plates of a parallel plate capacitor; pressing the key cap effectively increases the capacitance between the two plates. Special circuitry is needed to detect this change in capacitance. Mechanical keypads are most popular because of their low cost and strength in construction. However, mechanical switches have a common problem called contact bounces. Instead of producing a single, clean pulse output, pressing a mechanical switch generates a series of pulses because the switch contacts do not come to rest immediately. This phenomenon is illustrated.This article discusses about how to interface keypad with PIC microcontroller

Interface 4×4 Matrix Keypad With PIC Microcontrol

The logic and interface of a matrix keypad (4×4) with microcontroller only to read the number of port pins inputs i.e. digital. The same principle applies to any matrix keypad of order NxN with a microcontroller. Here, N is the order of the matrix.

Why Matrix Keypad?

When there is one port pin, it will be easy to read a digital input into the controller, whereas when there are lots of pins that is digital, it won’t be feasible to allocate one pin for each of them in this kind of situation where matrix keypad arrangement is used to reduce the pin count. By this it is concluded that with the increase in the order of the matrix there will be decreased in number of pins that are required to interface with a given number of inputs.

Matrix Keypad

Matrix Keypad

For example, if matrix 4×4 is needed, then 4 pins for columns and 4 pins for rows are required in such case there will be no difference in the cost. However, if we consider a 10×10 matrix it requires 20 pins, i.e. 10 rows and 10 columns it reads 100 digital inputs.

Internal Wiring

Internal Wiring

Matrix Keypad Interface Logic

All switches are assumed to be released initially. There will be no connections between columns and rows. When any of the switches are pressed, then corresponding columns and rows are connected i.e short circuited. It will drive the column to low initially. By using this logic, button press can be detected. The colors of black and red are for the logic low and high.

The steps in determining the key that was pressed are:

Step 1

Interfacing the matrix keypad and writing all logic as 1 to the columns and all logic 0 for rows.Assume that circled key is pressed and witness the how key is pressed can be detected by a software routine.

Step 2

This is the important stage where software has to scan the pins which are connected to the columns of the keypad. The key press in the column is known if it detects the logic as 0. This happens when the switch press shorts the C2 line with R2. Finally C2 is driven low.

Step 3

To start writing logic 1’s to row, and then the column of the corresponding key pressed to be located. Now check C2 become high or not. The logic is if the button in that row is pressed, then the value written in that row will determine columns (C2), since they are short circuited.

Step 4

This is followed till C2 goes higher with the logic that was written in a row. In this case logic to the second row will reflected in the second column.It is known that at column 2 is pressed, that means the detected key is in column 2. So, the position of the key in the matrix will be (2,2)

It is a user friendly. When it comes to designing a certain applications such as telephone, calculator, etc., then matrix keypad will arrange the push button switches in columns and rows. For suppose to interface a 4*4 which is a 16 key matrix pad with a microcontroller in a straightforward way, 16 pins on a microcontroller is needed. But, with a simple technique, it can be reduced to 8 pins. These are connected in a special manner.

4×4- Matrix Keypad

By scanning pressed key can be detected. Now, let’s suppose that all rows Row 1 – Row 4 are connected to output pins and all columns Co 11 – Co 14 are connected to the input pins.In the normal case we witness all the columns will be in a LOW state. For scanning keypad

  • A high signal is given to Co 11 inputs
  • Scanning for each row 1 – row 4
  • If any of the keys in the first column is pressed, then logic high signal will pass from Co 11.
  • To detect multiple keys this process can be repeated for all columns.

Interfacing with the PIC Microcontroller

  • In the above PIC microcontroller VDD and VSS are not shown. VSS is connected to ground and VDD is connected to +5V
  • To the port B of the PIC microcontroller matrix keypad is connected. Columns of matrix keypad are connected to RB0 – RB3 of PIC microcontroller which are configured as output pins. Every row of matrix keypad is connected to RB 4 – RB 7 of microcontroller, these are configured as input pins.
  • Here in 4×4 matrix keypad, which is having characters 0, 1, . . . 9, -, C, U, E, F.
  • Where ‘ B ‘ is replaced by ‘ – ‘ and ‘ D ‘ is replaced by ‘U’. Seven segment displays are used for displaying characters.
  • When displayed in seven segment display, D will be similar to 0 and B will be similar to U. TO read the data from the matrix keypad rows to be scanned and columns are made high.
Circuit Diagram of Matrix interfacing

Circuit Diagram of Matrix interfacing

Function to Scan the Keyboard

It initiates the keypad scanning and will return the character corresponds to the pressed key when a key is pressed it detected. The function used for finding key () to find the character corresponds to a particular column and row.

In this function Space (‘ ‘) is used as the null character, returns when no key is pressed. This can be changed accordingly. This function returns the character corresponding to a particular column and row.

Seven Segment Decoding Function

By using mikroC library interfacing matrix keypad with the PIC microcontroller.

The mikroC pro for PIC microcontroller the library providing for 4*4 keypad. This is used for 4*1, 4*2, 4*3 keypads.

The following functions provide while interfacing with matrix keypad

  • Keypad_Key_Click
  • Keypad_Init
  • Keypad_Key_Press

Keypad Key Click

The prototype for a keypad key click is a char keypad key click (void);

When a function is called, it will wait until a key is pressed and released. After it is released, it returns the number corresponding to the pressed key (1-16). It will return to zero if no key is pressed. When more than one key is pressed, then the function will wait till all the pressed keys are released and the return number corresponding to the first key. Before calling this function port need to be initialized.

Keypad Press

The prototype for keypad press is char keypad key press (void);This is the function reads keys when a key is pressed and will return number corresponding to the pressed key (1-16). It will return to zero when no key is pressed.

Keypad Init

The prototype for Keypad Init is void keypad Init (void);It works with the initializing with a particular keypad. Keypad port is a global variable port must be defined before suing this function. The initializing of port should be made before calling this function.

Thus, this is all about how to interface keypad with the PIC microcontroller.We hope that you have got a better understanding of this concept or to implement microcontroller based projects, please give your feedback by commenting in the comment section below.Here is a question for you, what is the function of a microcontroller?

Add Comment

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