The objective of this post is to explain how to use a 4×4 matrix keypad with the ESP8266. For simplicity, we will assume the use of the ESP8266 integrated in a NodeMCU board.
Introduction
The objective of this post is to explain how to use a 4×4 matrix keypad with the ESP8266. For simplicity, we will assume the use of the ESP8266 integrated in a NodeMCU board.
For this tutorial, I’ve used a membrane keypad similar to the one shown in figure 1. This is a very simple keypad that can be bought at eBay for less than 1 euro.
Figure 1 – Membrane 4×4 matrix keypad. Adapted from [1]
We will be using the Arduino Keypad library that can be found here. If you prefer, you can also find it at GitHub here. The easiest way to install the library is via Arduino IDE library manager, as shown bellow in figure 2.
Figure 2 – Installation of the library via library manager.
Although there is no explicit mention for the support in the ESP8266, it works fine with it as we will see bellow.
The hardware
As we can see from figure 1, a 4×4 matrix only has 8 interface pins, which correspond to the columns and rows of the matrix. Explaining how we can use only 8 pins to check which of the 16 keys is pressed is outside the scope of this post, but I strongly recommend you to read this article which explains the working principle.
Although the library we are going to use hides the implementation details from us, if you want to check how it works the mentioned article provides a useful help.
So, to interface with the matrix keypad, we simply need to connect both its columns and rows to the pins of the NodeMCU. This is shown in figure 3.
Figure 3 – Connection diagram for the NodeMCU and keypad matrix.
Note that we don’t need to connect any power supply to the keypad because half of the digital pins will work as outputs and the other half as inputs, as can be seen in this private method of the library, which is responsible for scanning for pressed keys. Again, this is just an implementation detail that we don’t need to worry about when using the library.
The code
This code is very similar to the one in the “CustomKeypad” example of the library. The examples are very simple and easy to use, so I encourage you to check them.
We will start by including the previously installed library, so we can access all the functionality needed to interact with the keypad.
#include <Keypad.h>
Next, we will declare some global variables. Since we are using a 4×4 keypad, we will declare a variable to store the number of lines and another for the number of columns. Note that this library works with keypads with sizes different from 4×4.
const byte n_rows = 4; const byte n_cols = 4;
Then, we will declare a char matrix with the same size of our keyboard (4 lines and 4 columns). We will initialize this matrix with the same values as the keys of our keypad. So, when a key is pressed, we will receive a value that matches the one in our keypad, which is simpler to interpret than having to convert, for example, integers in our keys.
For our keypad, shown in figure 1, we have the following mappings:
char keys[n_rows][n_cols] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} };
Then, we declare two byte arrays, which will contain the pins of the NodeMCU that are connected to the rows and the columns of our keypad.
byte colPins[n_rows] = {D3, D2, D1, D0}; byte rowPins[n_cols] = {D7, D6, D5, D4};
Note that we are using the constants defined here so we don’t need to worry about mapping the pins of the NodeMCU board to the pins of the ESP8266.
Finally, we instantiate an object of class Keypad and pass to the constructor our matrix of keys, the arrays containing the numbers of the digital pins of the NodeMCU connected to the rows and columns of the matrix, and the number of rows and columns.
Note that the matrix of keys needs to be passed to a macro that will cast it to a char array. But this is just a detail, we can just think of it as passing the matrix to a regular function.
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols);
In the setup function, we simply initialize a serial connection, so we can output the result of our code.
void setup(){ Serial.begin(115200); }
Now, in the main loop, we can call the getKey method of the class Keypad to get a char corresponding to the key pressed. This char will correspond to the one defined in the previously mentioned matrix, for the button pressed.
If there’s no key being pressed, the function will return NULL.
char myKey = myKeypad.getKey();
The full code is shown bellow, already including the printing of the pressed key to the serial port.
#include <Keypad.h> const byte n_rows = 4; const byte n_cols = 4; char keys[n_rows][n_cols] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; byte colPins[n_rows] = {D3, D2, D1, D0}; byte rowPins[n_cols] = {D7, D6, D5, D4}; Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols); void setup(){ Serial.begin(115200); } void loop(){ char myKey = myKeypad.getKey(); if (myKey != NULL){ Serial.print("Key pressed: "); Serial.println(myKey); } }
To test the program, just upload the code to the ESP8266, open the Arduino IDE serial monitor and press some keys. You can check the expected output in figure 4.
Figure 4 – Output of the program.
References
[1] https://www.parallax.com/product/27899
Technical details
- ESP8266 libraries: v2.3.0
- Keypad library: v3.1.1
Can it be done using lua and nodemcu
Hello,
As for the NodeMCU, that’s precisely the board I’ve used for this tutorial, so it works fine.
I haven’t played with lua yet, so I don’t know the libraries / functions available. Although this library will most likely not be available for lua, there may be an equivalent module/library.
If not, the implementation of the scanning of a keypad matrix is relatively simple, as you can check in this article:
http://embedjournal.com/interface-4×4-matrix-keypad-with-microcontroller/
So, if you can do digital writes and digital reads, in principle you will be able to get the pressed key from the keypad.
Just as a curiosity, there are other more complex approaches to reading keys pressed on a keyboard, from a microcontroller:
http://playground.arduino.cc/Code/OneWireKeyPad
Hope it helps
Can it be done using lua and nodemcu
Hello,
As for the NodeMCU, that’s precisely the board I’ve used for this tutorial, so it works fine.
I haven’t played with lua yet, so I don’t know the libraries / functions available. Although this library will most likely not be available for lua, there may be an equivalent module/library.
If not, the implementation of the scanning of a keypad matrix is relatively simple, as you can check in this article:
http://embedjournal.com/interface-4×4-matrix-keypad-with-microcontroller/
So, if you can do digital writes and digital reads, in principle you will be able to get the pressed key from the keypad.
Just as a curiosity, there are other more complex approaches to reading keys pressed on a keyboard, from a microcontroller:
http://playground.arduino.cc/Code/OneWireKeyPad
Hope it helps
I am using a ESP8266 ESP-01 not the NodeMCU how can i apply the wiring ?
Hi!
Sorry, unfortunately the ESP-01 doesn’t expose enough IO pins to allow the connection to the keypad using the configuration in this tutorial.
I’ve seen a couple of wirings to reduce the number of pins needed, such as the one shown in the link below, but I’ve never tested them:
https://playground.arduino.cc/Code/OneWireKeyPad
Hope this helps getting you in the right track.
Best regards,
Nuno Santos
I am using a ESP8266 ESP-01 not the NodeMCU how can i apply the wiring ?
Hi!
Sorry, unfortunately the ESP-01 doesn’t expose enough IO pins to allow the connection to the keypad using the configuration in this tutorial.
I’ve seen a couple of wirings to reduce the number of pins needed, such as the one shown in the link below, but I’ve never tested them:
https://playground.arduino.cc/Code/OneWireKeyPad
Hope this helps getting you in the right track.
Best regards,
Nuno Santos
How can i interface keypad with esp32 while using rfid and oled?
Hi!
That is a very generic question that I will not be able to answer properly in a comment 🙂
Basically you will need to connect everything correctly to your ESP8266, which will depend on your RFID, keypad and OLED modules.
Then, you will need to write your program in a way that allows to interact with the 3 peripherals.
Again, this will depend on the devices you are using (they may have or may not have libraries to make developing easier and the APIs may differ with the peripherals models).
On top of that, it pretty much depends on what you want to do with your program.
Why are you using the 3 peripherals? What is going to be displayed in the oled? Why the need for a keypad and rfid? What happens when the user clicks a button of the keypad?
Those are the things you need to take in consideration and develop your code accordingly 🙂
Depending on your previous experience with electronics, the ESP8266 and programming, this may be a simple task or a very hard one. Do you have prior experience with these?
Best regards,
Nuno Santos
How can i interface keypad with esp32 while using rfid and oled?
Hi!
That is a very generic question that I will not be able to answer properly in a comment 🙂
Basically you will need to connect everything correctly to your ESP8266, which will depend on your RFID, keypad and OLED modules.
Then, you will need to write your program in a way that allows to interact with the 3 peripherals.
Again, this will depend on the devices you are using (they may have or may not have libraries to make developing easier and the APIs may differ with the peripherals models).
On top of that, it pretty much depends on what you want to do with your program.
Why are you using the 3 peripherals? What is going to be displayed in the oled? Why the need for a keypad and rfid? What happens when the user clicks a button of the keypad?
Those are the things you need to take in consideration and develop your code accordingly 🙂
Depending on your previous experience with electronics, the ESP8266 and programming, this may be a simple task or a very hard one. Do you have prior experience with these?
Best regards,
Nuno Santos
how to make it on ESP32, i need help from you
Hi!
Although I haven’t had the chance to test it, the library should work in the ESP32 also.
You just need to connect it to the pins of your ESP32 board and correctly declare the rowPins and colPins.
Let us know if it works 🙂
Best regards,
Nuno Santos
how to make it on ESP32, i need help from you
Hi!
Although I haven’t had the chance to test it, the library should work in the ESP32 also.
You just need to connect it to the pins of your ESP32 board and correctly declare the rowPins and colPins.
Let us know if it works 🙂
Best regards,
Nuno Santos
Excellent tutorial! Thank you so much for sharing your knowledge with us.
I need help to connect my wemos D1 R1 to the Keypad,it doesn’t work like in your tutorial only apears en mi serial (???????????)