A keypad allows you to interact with a microcontroller. You can salvage these keypads from old telephones or you can purchase them from most electronics store for less than $2.
They come in wide variety of shapes and sizes. The most commons sizes are 3×4 and 4×4 and you can get keypads with with words, letters and numbers written on the keys.
You can even create your own keypad from scratch.
If you follow this tutorial you can control any keypad.
Description
These keypads very popular among the Arduino tinkerers. They are very cheap and you can use them with any microcontroller (MCU).
Where to buy?
You can purchase these modules for just a few dollars. Click here to compare the membrane keypad on several stores and find the best price.
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
How it Works?
A membrane keypad is a matrix consisting of rows and columns. Each key is assigned to a certain row and column (see the picture below).
On a 12 button keypad you have 4 rows and 3 columns. The first key would make a link between Row 1 and Column 1 (R1C1). 2 would be R1C2, 3 R1C3, * R4C1, 9 R3C3 and so on.
Arduino with Membrane Keypad
You need the following components to make this circuit:
Schematics
Follow the next schematics. If your keypad is different from the one below, try to search for the datasheet online.
Library Download
Here’s the library you need for this project:
- Download the Keypad library
- Unzip the Keypad library
- Install the Keypad library in your Arduino IDE by moving the unzipped folder to: Arduino\Libraries
- Restart your Arduino IDE
Uploading the Code
If your keypad doesn’t work with code below you might have to change the connections from the previous schematics.
Search the web or go to the store that sold you the keypad to find the datasheet for your keypad.
Note: If your keypad has more keys you can change lines 3 and 4 to add the right number of rows and columns. Then in line 5 you can change the array to match your keypad keys.
#include "Keypad.h"
const byte ROWS = 4; // number of rows
const byte COLS = 3; // number of columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; // row pinouts of the keypad R1 = D8, R2 = D7, R3 = D6, R4 = D5
byte colPins[COLS] = {4, 3, 2}; // column pinouts of the keypad C1 = D4, C2 = D3, C3 = D2
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
Serial.begin(9600);
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
Serial.println(key);
}
Demonstration
In this project when you press a key, it’s displayed the value in your serial montior. Here’s what you should see in your Arduino IDE serial monitor when you start pressing the keypad keys.
Conclusion
Now you can create an interface for your Arduino using a keypad. You could also add an LCD to this project.
I hope you found this guide useful.
Share this post with a friend that also likes electronics!
You can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.
Because these keypads consume 7 or 8 io pins, it may be better in some projects to use them in conjunction with an IIC port expander. You can find some modules on aliexpress (and probably ebay too) that have 8 digital ports, based on the 8574 chip, for under $5. On aliexpress search for things like “IIC 8574 I/O” and you can find modules with header pins ready to go. Using IIC you only use up 2 analog pins on your arduino, plus you can still daisy chain other IIC devices like a 1602 LCD if desired.
Hi Steve,
Yes, that’s a great tip. In some projects you’ll need to use an IC to save some digital pins in your Arduino
How can I make such a keyboard and 7 buttons with i2c? schematics and code details.
thanks
How can I make such a keyboard and 7 buttons with i2c? schematics and code details. Let’s say a 15 * 2 lcd i2c still on :))
thanks
A keyboard with only 7 buttons? or you mean 7 rows? Here’s documentation on that subject: playground.arduino.cc/Main/I2CPortExpanderAndKeypads
Great what you have done !
Thank you!
mencata traqbajar con al duino
I’m sorry I only speak Portuguese and English…
Can you please update this tutorial to show how to use ISR interrupt routines, as keypads become unresponsive in more complex projects?
Can you redo this with a port expander and I2C Please? I have tried other tutorials and they dont work with the latest library versions. I know if you guys do it you will do it properly 🙂
Hi Santos,
Can I change line 12 and 13 to match the pins of the keypad?
This is such simple and great tutorial!
Hi Santos,
How do I display the keypad that has been pressed to the ESP webserver, apart from displaying the keypad output on the serial monitor?
void loop(){
char key = keypad.getKey(); // Create a variable named key of type char to store the characters pressed
if (key){ // If the key variable contains characters
Serial.print(“Key pressed: “);
Serial.println(key)
if (key >= ‘0’ && key <= ‘9’) { // act on numeric keypress
inputString += key;
}
else if (key == ‘#’) { // press # to enter
if (inputString.length() > 0) {
Serial.println(inputString);
}
}
else if (key == ‘*’) {
inputString = “”; // clear input
}
}
}
With the code snippet above, when I press buttons 1 2 3 4 then I press #, the inputString will display “1234” on the serial monitor on the Arduino. When I press 0 0 0 and #, the inputString wil display “1234000” on the serial monitor.
But I also want these characters to appear as text on the webserver, how do I do that?