یہ ٹیوٹوریل کا حصہ ہے: کی پیڈز
تمام کی پیڈ ویڈیوز یہاں درج ہیں۔
How to Use a 5x4 20-Key Keypad with Arduino to Detect Strings
In this tutorial, we will learn how to use a 5x4 20-key keypad with an Arduino to detect key presses, including keys that have more than one character, such as "F1", "UP", "DOWN", "ESC", and "ENTER". Standard keypad libraries often struggle with multi-character keys, but this guide provides a simple solution to overcome that limitation. This project is perfect for a wide range of applications, including:

- Creating a security lock system where you enter a code and press "ENTER" to unlock (in video at 01:50).
- Building a menu navigation system for an LCD display, using the arrow keys to move a cursor (in video at 01:36).
- Developing a data entry terminal for industrial control systems.
- Making a custom remote control for a robot or other device.
- Designing an access control system for a door or a piece of equipment.
Hardware/Components
To build this project, you will need the following components:
- Arduino board (Uno, Nano, Mega, etc.)
- 5x4 20-key keypad with multi-character keys (like the one shown in the video)
- Jumper wires
- Breadboard (optional, for easier prototyping)
The keypad used in this tutorial has a flexible cable with 9 wires, corresponding to the 5 rows and 4 columns of the key matrix (in video at 02:25). The keypad also has an adhesive backing, allowing you to mount it securely to a surface (in video at 02:39). The dimensions of the keypad are 85mm x 73.4mm, with a cable length of 96mm (in video at 03:19).
Wiring Guide
Wiring the keypad to the Arduino is straightforward. When you hold the keypad with the keys facing you and the pins at the top, the wires are arranged in order from left to right. Connect these wires to the Arduino pins as follows (in video at 03:39):
- Connect the leftmost wire (pin 1) to Arduino pin 2.
- Connect the next wire (pin 2) to Arduino pin 3.
- Connect the next wire (pin 3) to Arduino pin 4.
- Connect the next wire (pin 4) to Arduino pin 5.
- Connect the next wire (pin 5) to Arduino pin 6.
- Connect the next wire (pin 6) to Arduino pin 10.
- Connect the next wire (pin 7) to Arduino pin 9.
- Connect the next wire (pin 8) to Arduino pin 8.
- Connect the rightmost wire (pin 9) to Arduino pin 7.
Note that the column pins are connected in reverse order (10, 9, 8, 7), which is important for the key mapping to work correctly (in video at 06:25).

Code Explanation
The code for this project uses the Keypad.h library. If you haven't already, you will need to install this library. You can download it from the link provided in the video description and install it via Sketch > Include Library > Add .ZIP Library (in video at 04:24).
Here are the key parts of the code that you can customize for your own projects:
1. Defining the Keypad Layout
This is where you define the actual text labels for each key on your keypad. You can change these to match your specific keypad or your application's needs (in video at 05:25).
char* specialKeys[] ={
"F1", "F2", "#", "*",
"1", "2", "3", "UP",
"4", "5", "6", "DOWN",
"7", "8", "9", "ESC",
"LEFT", "0", "RIGHT", "ENT"
};
2. Mapping Single Characters to Keys
Since the Keypad library works with single characters, we need a way to represent each key with a unique character. This array maps each key to a single character ID. Do not change these values unless you also update the keys array and the getKey() function accordingly (in video at 06:14).
char specialKeysID[] = {
'A', 'B', '#', '*',
'1', '2', '3', 'C',
'4', '5', '6', 'D',
'7', '8', '9', 'E',
'F', '0', 'G', 'H'
};
3. Configuring Arduino Pins
Here, you define which Arduino pins are connected to the keypad's rows and columns. Adjust these numbers to match your actual wiring (in video at 06:17).
byte rowPins[ROWS] = {2,3,4,5,6}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {10,9,8,7}; //connect to the column pinouts of the kpd
4. Using the getKey() Function
The getKey() function is a custom function that takes the single character returned by the keypad library and converts it back into the full string label. This is what allows you to detect multi-character keys. You can use this in your loop() to perform actions based on which key is pressed. For example, to check if the "ENTER" key was pressed, you would use:
if(getKey(key) =="ENTER")
{
Serial.println("ENTER is pressed");
}
Live Project/Demonstration
Once you have uploaded the code to your Arduino and opened the Serial Monitor (set to 9600 baud), you can test the keypad. When you press a key, the corresponding string will be printed to the Serial Monitor (in video at 07:32). For example, pressing "1", "2", "3" will print "1", "2", "3". Pressing the "UP" arrow will print "UP", and pressing "ESC" will print "ESC". The video demonstrates that all keys, including the multi-character ones, respond perfectly (in video at 08:32).
You can modify the code to add a delay between key presses if you want to prevent accidental multiple presses, but the default code has no delay for immediate response (in video at 09:37).
Video Chapters
- [00:00] Introduction to the 5x4 Keypad Project
- [00:48] Keypad Features and Applications
- [02:21] Keypad Physical Overview and Dimensions
- [03:39] Wiring the Keypad to Arduino
- [04:24] Installing the Keypad Library and Code Explanation
- [07:32] Live Demonstration of the Keypad
- [09:13] Conclusion and Final Thoughts
یہ ٹیوٹوریل کا حصہ ہے: کی پیڈز
- Using 4x3 Keypad with Arduino
- Arduino Code and Video for a Four-Key Keypad
- Arduino Code and Video: 4x4 Matrix Black Keypad
- Build a Simple Electronic Lock with Keypad and LCD Using Arduino
- Controlling an 8-channel relay with a 4x3 keypad
- How to use 4x4 Soft Keypad with Arduino
- Lesson 67: Controlling an 8-Channel Relay with a Keypad Using Arduino
- 1602 LCD Keypad Shield: Basic Code
/*
*
* Original code from keypad.h library: MultiKey.ino (@author Mark Stanley)
* Written and updated by Ahmad Shamshiri (Robojax.com) on May 06, 2020 at 19:37 in Ajax, Ontario, Canada
*
* This code is for a 5x4 keypad with text (more than 1 character);
* The code was written to allow any text for a key.
* The keypad has the following keys:
"F1", "F2", "#", "*",
"1", "2", "3", "UP",
"4", "5", "6", "DOWN",
"7", "8", "9", "ESC",
"LEFT", "0", "RIGHT", "ENT"
* Video code:
*
* Watch the video for details: https://youtu.be/sbQ3D1_x_tg
*
* See the wiring diagram and full explanation of the code: https://robojax.com/tutorial_view.php?id=310
If you found this tutorial helpful, please support me so I can continue creating contents like this. (PayPal):
🔥https://paypal.me/robojaxTV
* Code is available at: http://robojax.com/learn/arduino
* This code is provided "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Keypad.h>
const byte ROWS = 5; //5 rows
const byte COLS = 4; //4 columns
char* specialKeys[] ={
"F1", "F2", "#", "*",
"1", "2", "3", "UP",
"4", "5", "6", "DOWN",
"7", "8", "9", "ESC",
"LEFT", "0", "RIGHT", "ENT"
};
char specialKeysID[] = {
'A', 'B', '#', '*',
'1', '2', '3', 'C',
'4', '5', '6', 'D',
'7', '8', '9', 'E',
'F', '0', 'G', 'H'
};
char keys[ROWS][COLS] = {
{specialKeysID[0], specialKeysID[1], specialKeysID[2], specialKeysID[3]},
{specialKeysID[4], specialKeysID[5], specialKeysID[6], specialKeysID[7]},
{specialKeysID[8], specialKeysID[9], specialKeysID[10], specialKeysID[11]},
{specialKeysID[12], specialKeysID[13], specialKeysID[14], specialKeysID[15]},
{specialKeysID[16], specialKeysID[17], specialKeysID[18], specialKeysID[19]}
};
byte rowPins[ROWS] = {2,3,4,5,6}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {10,9,8,7}; //connect to the column pinouts of the kpd
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
unsigned long loopCount;
unsigned long startTime;
String msg;
void setup() {
// Robojax 5x4 keypad test
Serial.begin(9600);
Serial.println("Robojax 5x4 keypad");
}
void loop() {
// Robojax 5x4 keypad test
char key = keypad.getKey();
// just print the pressed key
if (key){
Serial.print("Key: ");
Serial.println(getKey(key));
}
if(getKey(key) =="ENTER")
{
Serial.println("ENTER is pressed");
}
//Robojax.com 5x4 keypad detecting string value
} // End loop
/*
* getKey()
* @brief gets the actual key value from single character
* @param k is character
* @return returns actual key value
* Written by Ahmad Shamshiri for robojax.com
* on May 06, 2020 at 19:37 in Ajax, Ontario, Canada
*/
char* getKey(char k)
{
//Robojax.com 5x4 keypad detecting string value
for(int i=0; i<20; i++)
{
if(specialKeysID[i] ==k) return specialKeys[i];
}
}//getKey
آپ کو جن چیزوں کی ضرورت ہو سکتی ہے
-
ایمیزون
-
ایمیزونPurchase 4x3 keypad from Amazonamzn.to
-
ای بے
-
علی ایکسپریس5x4 20 Kay kaypad keypad from AliExpresss.click.aliexpress.com
وسائل اور حوالہ جات
-
اندرونی
-
اندرونی
-
اندرونی
-
اندرونی
-
اندرونی
فائلیں📁
کوئی فائلیں دستیاب نہیں ہیں۔