This tutorial is part of: Keypad
All keypad videos are listed here.
Arduino Code and Video: 4x4 Matrix Black Keypad
In this tutorial, we will explore how to use a 4x4 matrix keypad with Arduino to take actions based on key presses. The keypad allows you to input values, such as '6', 'A', 'B', 'D', and perform specific tasks when those keys are pressed. By following along, you will learn how to set up the keypad and write the necessary code to interact with it effectively. For further clarification, be sure to check out the video (in video at 00:00).

Hardware Explained
The 4x4 matrix keypad consists of 16 keys arranged in a 4x4 grid. Each key press connects specific rows and columns, allowing the microcontroller to determine which key was pressed. The keypad is connected to the Arduino using digital pins, which will be configured in the code to read key presses.
In this project, we'll be using the Keypad library, which simplifies the process of reading input from the keypad. The library handles the logic of scanning the rows and columns, making it easier for you to focus on the functionality of your project.
Datasheet Details
| Manufacturer | Generic |
|---|---|
| Part number | 4x4 Matrix Keypad |
| Logic/IO voltage | 5 V |
| Key lifespan | 1,000,000 presses |
| Dimensions | 68.5 mm x 9.8 mm |
| Weight | 23 g |
- Connect row pins to digital pins 2-5.
- Connect column pins to digital pins 6-9.
- Ensure proper wiring to avoid misreads.
- Use pull-up resistors if required for stability.
- Debounce key presses in software to avoid multiple readings.
Wiring Instructions
To wire the 4x4 matrix keypad to the Arduino, start by connecting the row pins. Connect the first row pin to digital pin 5, the second row pin to 4, the third row pin to 3, and the fourth row pin to 2. Next, connect the column pins, starting with the first column pin to digital pin 9, the second column pin to 8, the third column pin to 7, and the fourth column pin to 6. This setup follows a right-to-left wiring approach as described in the video (in video at 00:00).
Make sure to securely attach the wires to the keypad and Arduino to prevent any loose connections. You can use female headers to make the connections more manageable. If you're using a breadboard, ensure that all connections are firmly seated.
Code Examples & Walkthrough
The following code initializes the keypad and sets up the serial communication. This allows you to see which key is pressed in the serial monitor.
#include
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
In this excerpt, we define the number of rows and columns and create a character array keys that maps the keys on the keypad. This array will be used to identify which key is pressed.
void setup(){
Serial.begin(9600);
}
In the setup function, we initialize the serial communication at a baud rate of 9600. This allows us to print the key presses to the serial monitor for debugging and observation.
void loop(){
char key = keypad.getKey();
if (key){
Serial.print("Key ");
Serial.print(key);
Serial.println(" is pressed");
}
}
The loop function continuously checks for key presses using keypad.getKey(). If a key is pressed, it prints the key value to the serial monitor. This is where you'll see real-time feedback of your input.

Demonstration / What to Expect
Once the wiring and code are correctly set up, you should see the key presses displayed in the serial monitor. Pressing '4' will trigger a specific action, which you can customize in the code. This setup allows for various applications, such as controlling devices based on user input. Be cautious of reversed polarity and ensure all connections are secure to avoid issues (in video at 00:00).
Video Timestamps
- 00:00 Introduction
- 01:30 Wiring the keypad
- 02:45 Code explanation
- 04:00 Demonstration of key presses
This tutorial is part of: Keypad
- Using 4x3 Keypad with Arduino
- Arduino Code and Video for a Four-Key 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
- How to Use a 5x4 20-Key Keypad with Arduino to Detect Strings
- Lesson 67: Controlling an 8-Channel Relay with a Keypad Using Arduino
- 1602 LCD Keypad Shield: Basic Code
|||您可能需要的东西
-
亚马逊
-
亚马逊Purchase 4x4 keypad from Amazonamzn.to
资源与参考
尚无可用资源。