The 4x4 Matrix Membrane Keypad, or 4x4 keypad, is a set of buttons arranged in rows and colums which includes a numeric keypad, four character keys and two symbol keys. So as every Arduino Basics, I´m just showing you how to connect it and set it up to show keys characters on a display.
First af all, you need to know that the total number of pins you have to connect exceeds the pins avilable in some models of Arduino. And for that reason you might need to use an I2C LCD1602 module in order to reduce the number of pins used for the display.
With this in mind, here is the connection diagram:
If you have doughts about the I2C module, you can check out this post for more information: Arduino Basics: The I2C module (yurrder.blogspot.com).
Now, the keypad requires a special library which you can easily install from the Arduino IDE Library manager.
Note: Both Key.h and Keypad.h are part of the installation and the same way are important for keypad functionality.
//Keypad libraries
#include <Key.h>
#include <Keypad.h>
//LCD libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Initialize the keypad
const byte rows=4;
const byte cols=4;
char keys[rows][cols]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[rows]={9, 8, 7, 6}; //Change pins if you connected different.
byte colPins[cols]={5, 4, 3, 2}; //Change pins if you connected different.
Keypad keypad=Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
//Initialize the LCD
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
lcd.init(); lcd.backlight(); //Set up display interface.
}
void loop() {
char key=keypad.getKey(); //Get the key pressed.
if(key!=NO_KEY){ //If a key is pressed, display it on the LCD.
lcd.clear();
lcd.print(key);
delay(300);
lcd.clear();
}
}
Follow me on social media as @yurrder to stay up to date with all the news about the blog and other projects.
Gracias, fue de mucha utilidad
ReplyDelete