Arduino uno + 4×4 Keypad Matrix + LCD i2c screen
Hello everyone, this is a quick tutorial on how to use the keypad with Arduino and a LCD i²c screen.
The Keypad I use is 4×4, it’s very useful and permits you to enter values or codes to you projects, I used it in many locks, alarm clocks, values input… It has 8 pins wired with Arduino Digital pins and the Matrix should be declared in the code, you’ll find examples in the Arduino Keypad built-in library.
The LCD I use is a i²c it has only 4 pins and easier than the standard version.
Wiring:
Keypad pins (from 8 to 1) it’s written on the kaypad, are wired with the Arduino UNO digital pins from (9 to 2).
LCD display is wired on 5v, GND, SDA with Analog4 and SCL with Analog5
Don’t forget to download the libraries if you don’t have them already, go to Arduino IDE, Sketch, Include libraries, Manage libraries, then search for the name of the library as in the code and install it.
The code I provided is just an example to get you started with both modules, but the possibilities are a lot bigger, you can check my other projects using the modules.
Library
You can download LCD i2c library here I use the New_Liquid_Crystal one.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
//Hello, This is the code to use a 4x4 keypad matrix with and Arduino and show the result on an LCD screen //You should wire you keypad from 8to1 (keypad pins) to 9to2 Arduino digital pins //SurtrTech #include<Keypad.h> //The keypad and LCD i2c libraries #include<Wire.h> #include<LCD.h> #include<LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 //defining the LCD pins #define BACKLIGHT_PIN 3 #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); const byte numRows= 4; //number of rows on the keypad const byte numCols= 4; //number of columns on the keypad //keymap defines the key pressed according to the row and columns just as appears on the keypad char keymap[numRows][numCols]= { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3 //if you modify your pins you should modify this too byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3 //initializes an instance of the Keypad class Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); void setup() { Serial.begin(9600); lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); lcd.home (); lcd.print("Press a key"); delay(2000); } void loop() { char keypressed = myKeypad.getKey(); if (keypressed != NO_KEY) { lcd.clear(); lcd.print(keypressed); //Serial.println(keypressed); //Uncomment this to show it on the serial monitor too } } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
your video was helpful thank you sir
the code is not work arrrrr i wish i never trusted you
Lol, can you explain you problem, or error ?
im having trouble finding the keypad.h library.