Arduino - Keypad

The keypad is widely used in many applications such as password input in the door lock, password and money amount input in ATM, key in calculator ...

In this tutorial, we will learn how to use keypad 3x4 and keypad 4x4 with Arduino. In-depth knowledge is also provided.

About Keypad

Keypad

The keypad is a set of buttons arranged in rows and columns (called matrix). Each button is called key

Keypad has various types. Two popular types for DIY projects are keypad 3x4 (12 keys) and keypad 4x4 (16 keys).

Pinout

Keypad pins are divided into two groups: row and column.

Keypad 3x4 has 7 pins: 4 row-pins (R1, R2, R3, R4) and 3 column-pin (C1, C2, C3).

Keypad 4x4 has 8 pins: 4 row-pins (R1, R2, R3, R4) and 4 column-pin (C1, C2, C3, C4).

Keypad Pinout

How It Works

This section is the in-depth knowledge. DON'T worry if you don't understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.

The process of detecting the key pressing is called scanning keypad.

It is called “scanning” because it checks one key by one key.

Row-pins are connected to Arduino's output pins

Column pins are connected to Arduino's input pins (INPUT_PULLUP, in this state, the value of the input pin is HIGH if the key is not pressed).

How Keypad Works

For each row:

  • Sets all row-pins is HIGH.
  • Sets only the current row-pin to LOW.
  • Reads the state of each column.
    • If a column-pin is HIGH ⇒ key at (row, column) is NOT pressed.
    • If a column-pin is LOW ⇒ key at (row, column) is pressed.
  • Repeats the above process for the next row-pins.

※ NOTE THAT:

The above is one of the methods to scan keypad. We can invert all HIGH to LOW and all LOW to HIGH to scan keypad.

Why does keypad is arranged and connected as a matrix? This makes the scanning process complicated. Why do not use each key as an independent button, then the state of the key is simply determined by reading the state of a button?

⇒ As we know, an independent button requires one Arduino's pin and GND. Let's take keypad 4x4 as an example. If we each key as an independent button, it requires 16 Arduino pin for 16 keys plus GND pin. If we arranged a connected key in matrix form, we just need to use 8 Arduino's pin, so we can save Arduino's pin. In short, the answer is: to save the Arduino pins.

Wiring Diagram

Arduino Keypad Wiring Diagram

Image is developed using Fritzing

How To Program

Thanks to Keypad library , using keypad with Arduino is a piece of cake, no matter whether you understand how the keypad works or not.

Arduino Code

Keypad 3x4

#include <Keypad.h> const int ROW_NUM = 4; //four rows const int COLUMN_NUM = 3; //three columns char keys[ROW_NUM][COLUMN_NUM] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); void setup(){ Serial.begin(9600); } void loop(){ char key = keypad.getKey(); if (key){ Serial.println(key); } }

Keypad 4x4

#include <Keypad.h> const int ROW_NUM = 4; //four rows const int COLUMN_NUM = 4; //four columns char keys[ROW_NUM][COLUMN_NUM] = { {'1','2','3', 'A'}, {'4','5','6', 'B'}, {'7','8','9', 'C'}, {'*','0','#', 'D'} }; byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); void setup(){ Serial.begin(9600); } void loop(){ char key = keypad.getKey(); if (key){ Serial.println(key); } }

Quick Steps

  • On Arduino IDE, Go to Sketch Include Library Manage Libraries
  • Arduino add library
  • Search “keypad”, then find the keypad library by Mark Stanley, Alexander Brevig
  • Click Install button to install keypad library.
  • Arduino keypad library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • Press some keys on keypad
  • See the result in Serial Monitor
  • COM6
    Send
    3 6 9 4 * #
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Video

incoming

Challenge Yourself

  • Display the pressed key of the keypad on LCD. Hint: Refer to Arduino - LCD
  • Make a door lock with password protection using the keypad.

Alternative Keypads

The above code also works with the following keypads:

Comments

If you have any questions or suggestions, please comment on our YouTube channel