Keypad With Arduino Without Using Keypad Library

We learned in previous chapters how to connect LCD with arduino. In this we will learn how to interface keypad with arduino without using keypad library and to display keypad numbers/characters on LCD screen.

REQUIRED HARDWARE

Following Hardware will be required to perform this circuit.

S.No. Item Quantity
1 Arduino Uno 1
2 Breadboard 1
3 Alphanumeric LCD 16×2 1
4 Keypad 4×3 1
5 Resistor 10K ohm 1
6 Male to Male jumper wire approx 25

BUILDING CIRCUIT

Make the following connections with Arduino

Interfacing of Keypad with Arduino
Click to zoom

Real time Keypad 4×3 Pinout

keypad-pinout
Click to zoom

ARDUINO CODE FOR KEYPAD WITHOUT USE OF KEYPAD LIBRARY

ALSO READ: CODE USING KEYPAD LIBRARY

// Including LCD library
#include<LiquidCrystal.h>

// Setting (RS,E,D4,D5,D6,D7) for particular pin number
LiquidCrystal lcd(6, 5, 4, 3, 2, 1);

// Define variable c1,c2,c3,r1,r2,r3,r4 and del
int c1 = 11, c2 = 12, c3 = 13;
int r1 = 10, r2 = 9, r3 = 8, r4 = 7;
int del = 600;

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Hello !");
  delay(1000);
  lcd.clear();

  /* use input pull-up feature of arduino, so that input c1 to c3 remains HIGH in absence input
    signal
  */
  pinMode(c1, INPUT_PULLUP);
  pinMode(c2, INPUT_PULLUP);
  pinMode(c3, INPUT_PULLUP);

  // Set r1,r2,r3,r4 pins to OUTPUT mode
  pinMode(r1, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(r3, OUTPUT);
  pinMode(r4, OUTPUT);
}

void loop() {
  // Calling user defined function row1( ), row2( ), row3( ) and row4( ) next to each other
  row1();
  row2();
  row3();
  row4();
}

// Logic to function row1( )
void row1() {
  /* This the same condition we understand in topic 9.2 i.e. one row should be Low and others should be High to distinguish between other rows and to print row 1 data
  */
  digitalWrite(r1, LOW);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);
  digitalWrite(r4, HIGH);

  /* We know high signal becomes low when it gets a direct path to ground */
  /* Reading state of c1 and checking if it is LOW then print ‘1’ and if c2 is LOW print ‘2’ and if c3 is LOW print ‘3’
  */
  if (digitalRead(c1) == LOW) {
    lcd.print("1"); delay(del);
  }
  else if (digitalRead(c2) == LOW) {
    lcd.print("2"); delay(del);
  }
  else if (digitalRead(c3) == LOW) {
    lcd.print("3"); delay(del);
  }
  //delay(del);
  /*why do we not set single delay here instead three in if blocks? Because if we give delay in
     if block then delay will executed only if any key has been pressed but if we give delay
     here each time either key pressed or not delay will be executed which slows the
     programming process.
  */
}

/* Similarly as we set logic for row1 we can set logic for other rows.
   Keep row LOW which we want to set logic and others to be HIGH
   But this time we will print ‘4’, ‘5’, ‘6’ on column key pressed instead of ‘1’, ‘2’, ‘3’.
*/
void row2() {
  digitalWrite(r1, HIGH);
  digitalWrite(r2, LOW);
  digitalWrite(r3, HIGH);
  digitalWrite(r4, HIGH);

  if (digitalRead(c1) == LOW) {
    lcd.print("4"); delay(del);
  }
  else if (digitalRead(c2) == LOW) {
    lcd.print("5"); delay(del);
  }
  else if (digitalRead(c3) == LOW) {
    lcd.print("6"); delay(del);
  }
}

/* In this we will print ‘7’, ‘8’, ‘9’ on column key pressed.
*/
void row3() {
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, LOW);
  digitalWrite(r4, HIGH);

  if (digitalRead(c1) == LOW) {
    lcd.print("7"); delay(del);
  }
  else if (digitalRead(c2) == LOW) {
    lcd.print("8"); delay(del);
  }
  else if (digitalRead(c3) == LOW) {
    lcd.print("9"); delay(del);
  }
}
/* In this we will print ‘*’, ‘0’, ‘#’ on column key pressed.
*/
void row4() {
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);
  digitalWrite(r4, LOW);

  if (digitalRead(c1) == LOW) {
    lcd.print("*"); delay(del);
  }
  else if (digitalRead(c2) == LOW) {
    lcd.print("0"); delay(del);
  }
  else if (digitalRead(c3) == LOW) {
    lcd.print("#"); delay(del);
  }
}

ALSO READ: CODE USING KEYPAD LIBRARY

Total Visits: 2402
0 0 vote
Article Rating
Subscribe
Notify of
guest
7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Tallahassee zip codes
5 months ago

565314 153111I really enjoy reading on this website, it holds wonderful articles . 617788

brand new houses for sale
5 months ago

209921 608216Oh my goodness! an incredible article dude. Thank you Even so My business is experiencing issue with ur rss . Dont know why Unable to subscribe to it. Can there be anyone obtaining identical rss problem? Anybody who knows kindly respond. Thnkx 360471

custom ribbons online
5 months ago

505309 62892A great clear cut answer and a terrific concept. But how do I post any function on this site is another question. The Foureyed Poet. 647169

dentist in memphis
5 months ago

447611 650614Many thanks I ought say, impressed together with your website. I will post this to my facebook wall. 747345

youtu.be
5 months ago

968539 399536The place else could anyone get that type of info in such an ideal means of writing? 459782

LED Lighting Inc.
5 months ago

475395 950514Thanks for some other excellent post. Exactly where else could just anyone get that type of information in such an ideal means of writing? Ive a presentation next week, and Im at the search for such information. 379605

Dampeer
5 months ago

308018 231495I adore your wp web template, wherever would you obtain it by way of? 314385

Translate »
7
0
Would love your thoughts, please comment.x
()
x