E029 How to use 4x4 Soft Keypad with Arduino

Video thumbnail for E029 How to use Arduino 4x4 keypad download the code RJT29

E029 How to use 4x4 Soft Keypad with Arduino

πŸ”’ Interfacing a 4x4 Matrix Keypad with Arduino

This tutorial demonstrates how to connect and use a 4x4 alphanumeric matrix keypad with an Arduino. This type of keypad includes 16 keys (0-9, A-D, *, #) and only uses 8 pins (4 rows + 4 columns), making it ideal for compact interfaces. With the help of the Keypad library, you can easily detect which button is pressed and trigger specific actions accordingly.

The keypad is connected to Arduino pins 2 through 9, and each press is detected in real-time and printed to the Serial Monitor. This can be adapted to control devices, input passwords, or navigate menus in embedded systems.

πŸ”Œ Wiring Explained

The 4x4 keypad consists of 4 row lines and 4 column lines:

Keypad Pin

Arduino Pin

Row 1

Pin 2

Row 2

Pin 3

Row 3

Pin 4

Row 4

Pin 5

Col 1

Pin 6

Col 2

Pin 7

Col 3

Pin 8

Col 4

Pin 9

This wiring ensures that each button press can be uniquely identified by scanning the row and column matrix.


πŸ’» Code Explanation

Here's the code used in the video:

cppCopyEdit#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5};  // Connect to the row pinouts
byte colPins[COLS] = {6, 7, 8, 9};  // Connect to the column pinouts

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    Serial.println(key);
  }

  if (key == '4') {
    Serial.println("Key 4 is pressed");
  }
}

Code Breakdown:

  • keys[][]: Defines the layout of the keypad.

  • rowPins[] and colPins[]: Maps the physical keypad pins to the Arduino.

  • keypad.getKey(): Checks if a key is pressed. If so, it's printed to the serial monitor.

  • The program also checks if '4' was pressed and performs an additional action (prints "Key 4 is pressed"), which can be extended to other keys as needed.

🎬 Video Chapters

  • 00:00 – Start

  • 00:45 – Introduction

  • 01:32 – Wiring Explained

  • 02:00 – Code Explained and Demonstration

This setup allows for flexible use of each keyβ€”for example, initiating actions, navigating menus, or controlling relays and lights.

πŸ“¦ Downloads


The Arduino code and required library files are available as a ZIP archive below this article. Let me know if


Downloads

Code Snippets

Comments will be displayed here.