Overview

In today’s lesson, we will learn how to connect the keyboard to the Arduino. In embedded devices, one of the essential parts is the keyboard, which is used to interact with embedded devices, the keyboard is an input device used to give commands to other devices, from the calculator to the computer; keyboard input, in this tutorial you will learn how to connect the keyboard to the Arduino, Arduino is a mini-computer that is easy to use. We will see how to connect the keyboard to the Arduino board and write a program to communicate between the Arduino and the keyboard.

Components to be Used:

  1. 4×4 keypad
  2. Arduino UNO
  3. Connecting Wires

Installing the keyboard library

We need to continuously scan rows and columns to determine which key was pressed. Fortunately, Keypad.h was written to hide this unnecessary complexity so that we could issue simple commands to know which key was pressed.

To install a library, go to Sketch > Include Library > Manage Libraries… Wait for the library manager to download the library index and update the list of installed libraries.

How does the keyboard work and how do I scan them?

The principle of operation is very simple. Pressing the button closes one of the rows in the row in one of the rows in the column, allowing current to flow between them. For example, when the “4” key is pressed, column 1 and row 2 are closed.

The microcontroller can scan these lines at the touch of a button. To do this, follow the procedure below.

  • 1. The microcontroller configures all rows of columns and rows for input.
  • 2. Then select a string and set it to HIGH.
  • 3. Then check the column rows one by one.
  • 4. If the column connection remains LOW, the row button has not been pressed.
  • 5. If it is HIGH, the microcontroller knows which row is HIGH and which column is HIGH during the test.
  • 6. Finally, you know which button was pressed, which corresponds to the detected row and column.

Installing the keyboard library

The following sketches will give you a complete understanding of how to detect keystrokes with a 4×4 membrane keyboard and can serve as a basis for more practical experiments and projects.

Source Code For 4 * 3

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns

char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3}; //connect to the column pinouts of the keypad


// Create a keyboard object
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  Serial.print("MeviHub.com ");

}
  
void loop(){
  char key = keypad.getKey();// Read the key
  
// Print if the key is pressed
  if (key){
    Serial.print("Key pressed: ");
    Serial.println(key);
  }
}


Final Code For 4 * 4

#include <Keypad.h>

const byte __ROWS = 4; //four __ROWS
const byte __COLS = 4; //four columns

char hexaKeys[__ROWS][__COLS] = {
                             {'1','2','3','A'},
                             {'4','5','6','B'},
                             {'7','8','9','C'},
                             {'*','0','#','D'}
                                              };


byte __RowPins[__ROWS] = {52, 53, 50, 51}; //connect to the row pinouts of the keypad
byte __ColPins[__COLS] = {48, 49, 46,47}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad __CustomKeypad = Keypad( makeKeymap(hexaKeys), __RowPins, __ColPins, __ROWS, __COLS); 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("MeviHub.com ");
}

void loop() {
  // put your main code here, to run repeatedly:
  char __CustomKey = __CustomKeypad.getKey();
  
  if (__CustomKey) 
  {
     Serial.print("Key pressed: ");
    Serial.println(__CustomKey);
 
  }
}

By Devilal

Leave a Reply

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