Keypad control by Arduino
Hello guys, Welcome back to my SriTu Hobby blog. Today we are going to talk about a topic that is very important to us. It’s about how to connect a keypad with an Arduino board. By that can get digital values to the Arduino board. The digital input details post link is below. Click me
What is the keypad?
When we say a keypad, we mean something that contains letters, numbers, and characters. This is mainly seen in the security system and phones. There is a special keypad in the market that we can use with the Arduino. We can get them as 4 * 4, 4 * 3 etc. Today we will be using a 4*4 keypad.
This keypad has 16 keys. We can use 8 digital pins to access these 16 key inputs. These keypads are connected to the matrix way. Look at the photo below.
The rows and columns of these keypads are connected together. It uses 8 digital pins to control this. We can use this component to enter the passwords or phone numbers in the Arduino. Let’s talk about those topics in our next articles. We will first learn the Arduino basics.
Okay, let’s see how to use this keypad with Arduino Nano board. The required components are as follows.
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Step 2
Connect this keypad to the Arduino board as shown below.
Step 3
Ok, let’s look at this code.
/*keypad tutorial.
Serial monitor readings.
created by the SriTu Tech team.
Read the code below and use it for any of your creation
*/
#include <Keypad.h>//Include library(library link in my blog)
const byte rows[4] = {2, 3, 4, 5};//connect to the row pinouts of the keypad
const byte cols[4] = {6, 7, 8, 9};//connect to the column pinouts of the keypad
char keys[4][4] = { //create 2D arry for keys
{'D', 'C', 'B', 'A'},
{'#', '9', '6', '3'},
{'0', '8', '5', '2'},
{'*', '7', '4', '1'},
};
Keypad mykeypad = Keypad(makeKeymap(keys), rows, cols, 4, 4);
void setup() {
Serial.begin(9600);//enable serial monitor
}
void loop() {
char myKey = mykeypad.getKey();//get key and put in to the veriable
if (myKey) { //check condition
Serial.println(myKey);//print key
}
}
So, the first time we need the keypad library for working on this code. Use this link to download it. Then, insert it into your Arduino IDE. Follow the steps below.
OK, done. Now we must include the rows and columns pins we are using. For that, using two arrays. That’s are called ‘rows’ and ‘cols’.
const byte rows[4] = {2, 3, 4, 5};//connect to the row pinouts of the keypad
const byte cols[4] = {6, 7, 8, 9};//connect to the column pinouts of the keypad
After, we must create a char map using keypad characters. It uses a 2D array.
char keys[4][4] = { //create 2D arry for keys
{‘D’, ‘C’, ‘B’, ‘A’},
{‘#’, ‘9’, ‘6’, ‘3’},
{‘0’, ‘8’, ‘5’, ‘2’},
{‘*’, ‘7’, ‘4’, ‘1’},
};
After that, we want an object for the keypad library. In this code, it is called “mykeypad”. It notifies the Arduino board about the pins and keys used.
Keypad mykeypad = Keypad(makeKeymap(keys), rows, cols, 4, 4);
The serial monitor is enabled to read key values.
void setup() {
Serial.begin(9600);//enable serial monitor
}
Then create a char variable and take the keypad values.
char myKey = mykeypad.getKey();//get key and put in to the veriable
Then, those values are checked using a condition and printed on a serial monitor.
if (myKey) { //check condition
Serial.println(myKey);//print key
}
Step 4
Now, select the board and port. After, click the upload button.
Okay, it’s all over. Please press the keys to view the values on the serial monitor. You can then see the key value on the serial monitor.
The full video guide is below. Please watch it and try to use this knowledge in your own creations. So, we will meet in the next post. Have a good day.