arduino, keypad, lcd, wires (Small)

Display output keypad to LCD using arduino UNO

This tutorial is about how to interface keypad to arduino and display output of keypad to LCD. Sometime in your project you need to display output of keypad to LCD. Such as when you input a password, number or other project like calculator. So, this project is about how to display characters to LCD directly by keystrokes/pressing keypad.

Keypad used in this tutorial has 4 rows and 4 columns, and an LCD 20×4. You can use another type when you practicing by your self.

Parts needed to display keypad to LCD using arduino UNO

Parts needed is :

1. Arduino UNO

2. Keypad

3. LCD

4. Some wire to connect all parts

arduino, keypad, lcd, wires (Small)
arduino, keypad, lcd, wires

Steps to do

Connect keypad to arduino pin 9,8,7,6,5,4,3,2 (from left to right) like picture below :

arduino uno and keypad (Small)arduino uno and keypad

Then connect LCD to arduino to these pin :

LCD RS -> A1

LCD E -> A0

LCD D4 -> 13

LCD D5 -> 12

LCD D6 -> 11

LCD D7-> 10

And completed parts connected will be like this :

display keypad output to LCD (Small)
display keypad output to LCD

The code

Make sure you connect all wires and part correctly. Double check your wiring. If you already sure everything are correct, then now time to code !

First include all libarary you need, in this case you need keypad and liquidCrystal

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(A1, A0, 13, 12, 11, 10);

String pad;

Configure keypad pin and the characters. You can change according to your keypad and pin.

const byte numRows= 4;
const byte numCols= 4;

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};
byte colPins[numCols] = {5,4,3,2};

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad

In setup you need to initialize lcd

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20,4);
}

Make function to detect keystrokes

void bacaKeypad() {
  char keypressed = myKeypad.getKey(); //deteksi penekanan keypad
  String konv = String(keypressed);
  pad+=konv;
}

And the last, call all function in loop function

void loop() {
  // put your main code here, to run repeatedly:
  bacaKeypad();
  lcd.setCursor(0,0);
  lcd.print(pad);
  delay(100);
}

If you still unclear about all parts of code, here’s the complete code :

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(A1, A0, 13, 12, 11, 10);

String pad;
const byte numRows= 4;
const byte numCols= 4;

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};
byte colPins[numCols] = {5,4,3,2};

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20,4);
}

void loop() {
  // put your main code here, to run repeatedly:
  bacaKeypad();
  lcd.setCursor(0,0);
  lcd.print(pad);
  delay(100);
}

void bacaKeypad() {
  char keypressed = myKeypad.getKey(); //deteksi penekanan keypad
  String konv = String(keypressed);
  pad+=konv;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *