4×3 Keypad Interfacing with Arduino

Keypads allow users to input data while a program is running. In This tutorial we show how to connect a twelve-button keypad to an Arduino and how to use the library Keypad.h.

A keypad is often needed to provide input to an Arduino system, and membrane-type keypads are an economical solution for many applications. They are quite thin and can easily be mounted wherever they are needed.

In this tutorial, we demonstrate how to use a 12-button numeric keypad, similar to what you might find on a telephone. A 12-button keypad has three columns and four row. Pressing a button will short one of the row outputs to one of the column outputs. From this information, the Arduino can determine which button was pressed. For example, when key 1 is pressed, column 1 and row 1 are shorted. The Arduino will detect that and input a 1 to the program.

How the rows and column are arranged inside the keypad is shown in the figure below,

keypad
keypad

Components required for this tutorial:

Download Keypad Library here

Circuit Diagram

Arduino 4x3 Keypad Circuit Diagram
Arduino 4×3 Keypad Circuit Diagram

Code

/*
  Arduino 4x3 Keypad Tutorial.
  Created by - Alpha Electronz
  Date Created - 13-01-2020
  For more information visit : http://alphaelectronz.com/arduino-keypad
*/
#include <Keypad.h>
const byte Rows= 4; //number of rows on the keypad i.e. 4
const byte Cols= 3; //number of columns on the keypad i,e, 3
//we will definne the key map as on the key pad:
char keymap[Rows][Cols]=
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
//  a char array is defined as it can be seen on the above
//keypad connections to the arduino terminals is given as:
byte rPins[Rows]= {A6,A5,A4,A3}; //Rows 0 to 3
byte cPins[Cols]= {A2,A1,A0}; //Columns 0 to 2
// command for library forkeypad
//initializes an instance of the Keypad class
Keypad kpd= Keypad(makeKeymap(keymap), rPins, cPins, Rows, Cols);
void setup()
{
     Serial.begin(9600);  // initializing serail monitor
}
//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
void loop()
{
     char keypressed = kpd.getKey();
     if (keypressed != NO_KEY)
     { 
          Serial.println(keypressed);
     }
}

 

Leave a Reply

%d bloggers like this: