Thursday , March 30 2017
Home / Embedded Systems / Interface 4×4 Matrix Keypad With Microcontroller

Interface 4×4 Matrix Keypad With Microcontroller

In this post we will discuss logic and interface of a matrix keypad (4×4 for this post) with microcontroller to reduce the number of port pins required to read a certain number of inputs (digital). The same logic applies to any matrix keypad of order NxN. Where, N is the order of the matrix.

Why Matrix Keypad?

Typically one port pin is required to read a digital input into the controller. When there are a lot of digital input that has to be read, it is not feasible to allocate one pin for each of them. This is when a matrix keypad arrangement is used to reduce the pin count.

Therefore, the number of pins that are required to interface a given number of inputs decreases with increase in the order of the matrix.

4x4 matrix keypad featured image

Example: If the matrix is 2×2, you will need 2 pins for the rows and 2 pins for the columns in such a case there is no difference in the cost of reading that many inputs. But if you consider a 10×10 matrix you will just need 20 pins (10 for the rows and 10 for the columns) to read 100 digital inputs.

How is it wired up internally?

Here is how the matrix keypad is wired internally. matrix keypad Schematic

 

From the circuit you can see that of one of the 16 buttons are pressed, a pair of pins are connected together. We will used this feature to detect with button was pressed in the following sections.

Matrix Keypad Interface Logic

Initially all switches are assumed to be released. So there is no connection between the rows and columns. When any one of the switches are pressed, the corresponding rows and columns are connected (short circuited). This will drive that column pin (initially high) low. Using this logic, the button press can be detected. The colors red and black is for logic high and low respectively.  Here are the steps involved in determining the key that was pressed.

Step 1:

The first step involved in interfacing the matrix keypad is to write all logic 0’s to the rows and all logic 1’s to the columns. In the image, black line symbolizes logic 0 and red line symbolizes logic 1.

For now let us assume that, the circled key is pressed and see how the key press can be detected by a software routine.

Wiring diagram

Step 2:

Now the software has to scan the pins connected to columns of the keypad. If it detects a logic 0 in any one of the columns, then a key press was made in that column. This is because the event of the switch press shorts the C2 line with R2. Hence C2 is driven low.

Note: color of the lines indicate the logic values they return.

step 3

Step 3:

Once the column corresponding to the key pressed is located, the next thing that the software has to do is to start writing logic 1’s to the rows sequentially (one after the other) and check if C2 become high. The logic is that if a button in that row was pressed, then the value written to that row will be reflected in determined column (C2) as they are short circuited. Note: color of the lines indicate the logic values they return.

step 4

Step 4:

The procedure is followed till C2 goes high with logic high is written to a row. In this case, a logic high to the second row will be reflected in the second column.

Note: color of the lines indicate the logic values they return.

Step 5

We already know the key press happened at column 2. Now we have detected that the key is in row 2. So, the position of the key in the matrix is (2,2)

Once this is detected, its up to us to name it or provide it with a task in the event of the key press.

Implementation with C

Now lets see how the above logic can be implemented in embedded C. Here is the program I wrote to test it. This code is for PIC microcontrollers with c18 lite version compiler. I as usual, used a lot of macros so if you are an Arduino user you could easily make some alterations to the code and use it. The basic concept for keypad scan is inside the while(1) loop.

Here is a video demonstration for the interface of the 4×4 matrix keypad with the above code.

The above program is done with polling and utilizes the entire time of the controller to scan the keypad and display the data on the 7 segment displays. There is a cool feature on Microcontrollers called as the Interrupt on Change (IOC). As the name suggests, the controller will interrupt if it finds any change in a port. In PIC the whole of PORT B has this feature. By using the feature without any change in the hardware setup we can scan the keypad in the ISR and have more of the controllers time to do some thing useful.

Related Downloads

Here are links to download the related C files, HEX files and Project Folder for the above tutorial.

In my upcoming posts I will use the IOC feature to interface the keypad.

About Siddharth

Siddharth is a Firmware Engineer, techie, and a movie-buff. His interests include, Programming, Embedded Systems, Linux, Robotics, CV, Carpentry and a lot more. At times, you could see some of his sunday projects converge on release quality. You get to know him on the following social channels.

Check Also

c2000_lauchpad

Interfacing 16×2 LCD with C2000 launchpad

  The C2000 Launchpad is an inexpensive evaluation module based on Piccolo family microcontrollers from …

  • 560086

    can u plz explain what ur function call
    seg_wrt does??

    • Have you seen the video that I made to explain the POV concept?, the function seg_wrt is used implement it. It writes 2 or more digit data to the 7 segment display.

  • Abhishek

    I didn’t understand the logic of writing 1’s to rows sequentially(one after the other) and check whether C2 becomes high.You said logic is that if a button in that row was pressed, then the value written to that row will be reflected in determined column (C2) as they are short circuited.Can you please explain this logic briefly

    • Hi, I have updated the post with a new schematic. Hopefully it should clear any doubts that you may have regarding the matrix keypad working.

  • samuel

    Hi, how

  • samuel

    hi, how do you combine the keypad code with a 7 segment display on a MPLab IDE

  • Shubham Sharma

    Can you plz give a vhdl code for the same ?

Keep in touch with the current trends!
Did you like this article? Sign up and get our latest posts delivered to your inbox!
  We hate spam and never share your details.