Header Ads

One single analog pin keypad control using Arduino

Overview

In this tutorial, we will learn how to interfacing 4*4 keypad with Arduino by a single analog pin. The use of a lot of pins can be reduced by following this procedure. It helps to use rest of pins for another purpose. For this tutorial, we will use a single analog pin to interfacing keypad with Arduino.

You can watch the following video below:-

Components Required

The required components list for this tutorial given below:-
  • Arduino Uno.
  • 4*4 keypad.
  • Three 4.7k resistors.
  • Four 1k resistors.
  • One 220ohm resistor.
  • LCD display.
  • 10k potentiometer.
  • Some jumper wire.
    Fig:1.1

Keypad Pinout

The following below figure shows 4*4 keypad pinout. 
Fig 2.1: 4*4 Keypad Pinout

Circuit Schematic

The given below schematic is single wire keypad with Arduino:- 
Fig 4.2: One wire keypad with LCD using Arduino

Circuit Description

One single analog pin keypad control based on basic voltage divider role. The voltage increase gradually from left to right (Fig: 4.1) in the row and up to down (Fig: 4.1in the column. The voltage is minimum at the first-row first column and the maximum voltage is at point row 4 and column 4. Three 4.7k ohm resistors will gradually connect between row 1 to  2, 2 to 3, 3 to 4. Similarly, three 1k ohm resistors will connect between column 1 to 2, 2 to 3, and 3 to 4 respectively. The remaining one 1k ohm resistor will go to ground from column 1. Next +5V will connect with row 1 and analog input A0 pin from Arduino will connect with column 1. 
Fig: 4.1
The first pin of LCD from left is Gnd pin which will connect with Arduino ground pin. Next LCD pin R/W 5  and cathode pin16 will also go to ground. The second pin of LCD is Vcc which connect with Arduino +5V. The anode pin 16 of LCD operated on 5V, a 220-ohm resistor should be connected in series to this pin. Next comes to the VEE (Pin 3) which is LCD contrast adjustment pin that will be connected a 10k potentiometer to +5V and ground, with its wiper (output) pin. The LCD rs and enable pin 4 and 6 will connect with Arduino digital pin 2 and 3 respectively. Next LCD pin D3 to D7 gradually connect with Arduino digital pin 4 to 7.
Fig 4.2: One wire keypad with LCD using Arduino.

Source Code

One wire keypad with Arduino source code is given below:-
 #include <OnewireKeypad.h>  
 //#include <LiquidCrystal.h>  
 //LiquidCrystal lcd(2,3,4,5,6,7);  
    
 char KEYS[] = {  
  '1', '2', '3', 'A',  
  '4', '5', '6', 'B',  
  '7', '8', '9', 'C',  
  '*', '0', '#', 'D'  
 };  
    
 OnewireKeypad <Print, 16 > KP2(Serial, KEYS, 4, 4, A0, 4700, 1000, ExtremePrec );  
    
 void setup () {  
 // lcd.begin(16, 2);  
  Serial.begin(9600);  
  KP2.SetKeypadVoltage(5.0);  
 }  
 void loop()  
 {  
  char Key;  
  byte KState = KP2.Key_State();  
    
  if (KState == PRESSED)  
  {  
   if ( Key = KP2.Getkey() )  
   {  
    Serial << "Key: " << Key << "\n";  
 //   lcd.setCursor(0, 0);  
 //   lcd.clear();  
 //   lcd.print(Key);  
   }  
  }  
 }  
One wire keypad with LCD using Arduino source code is given below:-
 #include <OnewireKeypad.h>  
 #include <LiquidCrystal.h>  
 LiquidCrystal lcd(2,3,4,5,6,7);  
    
 char KEYS[] = {  
  '1', '2', '3', 'A',  
  '4', '5', '6', 'B',  
  '7', '8', '9', 'C',  
  '*', '0', '#', 'D'  
 };  
    
 OnewireKeypad <Print, 16 > KP2(Serial, KEYS, 4, 4, A0, 4700, 1000, ExtremePrec );  
    
 void setup () {  
  lcd.begin(16, 2);  
  Serial.begin(9600);  
  KP2.SetKeypadVoltage(5.0);  
 }  
 void loop()  
 {  
  char Key;  
  byte KState = KP2.Key_State();  
    
  if (KState == PRESSED)  
  {  
   if ( Key = KP2.Getkey() )  
   {  
    Serial << "Key: " << Key << "\n";  
    lcd.setCursor(0, 0);  
    lcd.clear();  
    lcd.print(Key);  
   }  
  }  
 }  
You can download the library here: https://github.com/AndrewMascolo/OnewireKeypad

No comments

Powered by Blogger.