Project 17 – Interface with 4×4 keypad and 2×16 LCD

In this tutorial, you will learn on how to display the character on LCD by pressing the 4x4 keypad button.


INTERFACE WITH 4×4 keypad and 2×16 LCD

Back to Project 16                                                                                         Go to SK40C Tutorial Page

This 16-button keypad provides a useful human interface component for micro controller projects. Convenient adhesive backing provides a simple way to mount the keypad in a variety of applications. Matrix keypads use a combination of four rows and four columns to provide button states to the host device, typically a micro-controller. Underneath each key is a push button, with one end connected to one row, and the other end connected to one column.

In order for the micro-ocontroller to determine which button is pressed, it first needs to pull each of the four column either low or high one at a time, and then poll the states of the four rows. Depending on the states of the rows, the micro controller can tell which button is pressed. You can download the Keypad 4×4user manual from here.

In this tutorial, you will learn on how to display the character on LCD by pressing  the 4×4 keypad button.




lcd280x3731 x LCD (2×16)- Blue Backlight

keypad-4x4-3158-280x3734×4 Keypad

Jumper(male-male)1 x  Male to Male Jumper Wires



Connection in between SK40C and 4×4 keypad are only required 9 wires which are 5V(VDD) and Port C0-C7. Additional 4 of 10k ohm resistors were used to pull out the 4 columns of the keypad..

a) Circuit connection


                                                               b) Actual circuit connection

c) Schematic



4×4 Keypad pin can directly connect to microcontroller or keypad decoder IC for decode purpose. However, DIY scanning of keypad consumes a lot of understanding in programming and more program space. The better way to interface a keypad to the PIC is to use a keypad encoder in between the keypad and the microcontroller. One of the Keypad Encoder is theMMC74C922. Simply hook up all the row and the column pins directly to the encoder and it will output a HIGH pulse on the Data Available pin whenever a key is pressed. After that, the encoder will output 4 bits of data to the PIC.


  •  Ultra-thin design
  •  Adhesive backing
  •  Excellent price/performance ratio
  •  Easy interface to any microcontroller
  •  Example programs provided for the BASIC

Key Specifications

  •  Maximum Rating: 24 VDC, 30 mA
  •  Interface: 8-pin access to 4×4 matrix
  •  Operating temperature: 32 to 122 °F(0 to 50°C)
  •  Dimensions:64x62x8 mm

Application Ideas

  • Security systems
  •  Menu selection
  •  Data entry for embedded systems




In this part of the source code, the pins which were connected to the columns and the rows of the 4×4 keypad were initialized at here.


This part of source code will determined which 4×4 keypad button being pressed.


KP_R1 = 0;
KP_R2 = 1;
KP_R3 = 1;
KP_R4 = 1;

It will test whether the 1st row of the keypad being pressed or not. If not, it will goto to 2nd row to check for the button pressed. If the button of the 1st row was being pressed, it will test which column is the location.

if (KP_C1 == 0)  return ‘1’;
if (KP_C2 == 0) return ‘2’;
if (KP_C3 == 0) return ‘3’;
if (KP_C4 == 0) return ‘A’;

If the 1st column, it will return 1 in ASCII form. If  it was 2nd column, 2 in ASCII form being return. Same go to column 3 and 4.

return 0xFF;

If the keypad does not being pressed, this function will return 0xFF to the function which will used it.

unsigned char keypad_wait();

This function will test whether the keypad button being pressed or not.

do{c_pressed_key = keypad_read();}while (c_pressed_key == 0xFF);

If the keypad_read() function does not return anything(return 0xFF only) to the c_pressed_key, it will wait at here until it accept one of the 16 characters on the keypad from keypad_read().

return c_pressed_key;

Return the accepted data(character in ASCII form) back to main function.


unsigned char key_get;

It is an unsigned char variable used to store the data get from the 4×4 keypad.

TRISC = 0b00001111;

Initialize the PORTC<7:4> as output, PORTC<3:0> as input


Please refer here for the SK40C with lcd tutorial.


While loop used in here in order the micro-controller can always read the data from the keypad

LED1 = 1;

LED2 = 0;

Set  the LED1 and LED2 light up and off at first respectively.

lcd_putstr(”   Enter Word:”);

Display Enter Word on the first row of the 16×2 lcd


Go to 2nd row of the 16×2 lcd

for(unsigned int i = 0; i < 17; i++)

This loop used in order for the LCD to displayed 16 characters

key_get = keypad_wait();

The variable key_get will store the data from keypad_wait() function.

LED1  ^= 1;

LED2  ^= 1;

The led will be toggle each time for the button of the keypad being pressed. Either LED1 on and LED2 off or vice versa.


Clear the LCD





The LCD will display the character of the keypad as you pressed the button. It will showed 16 characters and if you pressed the keypad further more, the 16   characters will be clear and started again from the first column of the 2nd row on the16x2 LCD.

Back to Project 16                                                                                        Go to SK40C Tutorial Page



1. P17_SK40C_4x4keypad_user manual.pdf


18 thoughts on “Project 17 – Interface with 4×4 keypad and 2×16 LCD”

  1. Can you share me the proteus circuit? Done compiling but i am not confident with my connection as it does not produce display through the lcd

  2. hi kindly help im give a task to design a system that will read apassword from a 3X4 KEYPAD use the last digits of your student number the system must flash 2LEDs if the combination is wrong or not entered

  3. I have noticed you don’t monetize your website,
    don’t waste your traffic, you can earn additional cash
    every month because you’ve got high quality content.
    If you want to know how to make extra $$$, search for:
    Boorfe’s tips best adsense alternative

  4. hy ,,,i want to ask u how the connection and coding (c++) for keypad 4×4 and uart with 2 different lcd 16×2???

  5. Great post. I used to be checking constantly this blog and I’m impressed!

    Extremely helpful information specially the final section 🙂 I care for such info a lot.
    I used to be looking for this certain information for a very lengthy time.
    Thanks and good luck.

  6. Hi, I’m trying to use this with a 18f4520. My hardware layout is identical. I’m just wondering is the system.h section a seperate header file and does it require an #include “system.h” call. Also, the file keypad.c, is that seperate to the main program or can it be included?
    Thanks in advance

  7. Resistors need only be .25W (1/4 W) as they are only pullups. No need to buy the 5W HUGE resistors currently linked to in the post.

  8. Hi guy,

    I still can’t display the output at LCD as shown on your tutorial above .
    Error pop pup in output shown like below this text;

    //*** Error 12″keypad.c” Line 29(10,11); Underlined identifier RC6

    require your help to resolve .

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Control Servo with Maker Pi Pico
Remote Jetson Nano using SSH with Visual Studio Code
3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C
Program Maker Pi Pico and ESP01 with Blynk IoT App using Arduino IDE
Keypad Solenoid Door Lock Using Arduino
Scroll to Top