Send characters and numbers to your Arduino using a keypad ⌨️

In your daily life, you deal with a lot of devices that have a keypad, such as the ATM.


In this tutorial, we will learn more about the keypad, how it works, and how you can use it to send data to your Arduino.


A keypad is a set of keys that are used to input data into a computer or electronic device. Most keypads consist of numeric, symbol, and navigation keys.


Overview


There are many different types of keypads, which vary in function and size. Keypads are useful for entering text, navigating menus, and even controlling games.


We will use the 4x4 keypad, which has four rows and four columns of keys organized in a grid. Each key represents a different number, character or symbol.


In this tutorial, we will send data from the keypad to the Arduino and display them on your computer!


Let's see how simple it is!


Getting the items


For this project you will need the following components: 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat 16 Key Keypad
9 QAR
Sale Off
Voltaat Jumper Wires - Male to Male (40 Pack)
10 QAR

Wiring it up


To set up the keypad in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the keypad and the Arduino. Once the keypad and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.


Connections from the keypad to Arduino:

Take a close look at the keypad's pins; they are numbered from 1 to 8 (from right to left)


• Keypad pin 1 → Arduino pin 2

• Keypad pin 2 → Arduino pin 3

• Keypad pin 3 → Arduino pin 4

• Keypad pin 4 → Arduino pin 5

• Keypad pin 5 → Arduino pin 6

• Keypad pin 6 → Arduino pin 7

• Keypad pin 7 → Arduino pin 8

• Keypad pin 8 → Arduino pin 9


Coding


The function of this sketch is to print data from the keypad to your computer through the serial monitor. You can use this code to make more complex projects, such as a system with a secure password.


In order for the code to work correctly, you need to download the keypad library. Libraries are files that you can download and copy to the Arduino IDE software files so the Arduino can recognize different sensors and components. You can download the library files from the resources section and then install it by following this tutorial.


/*
Voltaat learn (https://www.voltaat.com)
Link for full tutorial: https://bit.ly/3ySLDVt
Link for libraries: https://bit.ly/3D7djse

Tutorial: Send characters and numbers to your Arduino using a keypad

The function of this sketch is to print data from the keypad to your computer

Connections from the keypad to Arduino:
Pins on the keypad are numbered from 1 to 8
• Keypad pin 1 → Arduino pin 2
• Keypad pin 2 → Arduino pin 3
• Keypad pin 3 → Arduino pin 4
• Keypad pin 4 → Arduino pin 5
• Keypad pin 5 → Arduino pin 6
• Keypad pin 6 → Arduino pin 7
• Keypad pin 7 → Arduino pin 8
• Keypad pin 8 → Arduino pin 9

*/


// This library allows you to connect the keypad with arduino
#include< Keypad.h >
//Define variables (the four rows)
const int ROW_NUM =4;
//Define variables (the four columns)
const int COLUMN_NUM =4;

//Instantiates a Keypad object that uses pins 5, 4, 3, 2 as row pins, and 8, 7, 6 as column pins.
//This keypad has 4 rows and 3 columns, resulting in 12 keys.
char keys[ROW_NUM][COLUMN_NUM]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

//Connect to the row pinouts of the keypad
byte pin_rows[ROW_NUM]={9,8,7,6};
//Connect to the column pinouts of the keypad
byte pin_column[COLUMN_NUM]={5,4,3,2};
Keypad keypad =Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

//Commands inside void setup run once
void setup(){
// Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);
}

//Commands inside void loop run forever
void loop(){

//Returns the key that is pressed
char key = keypad.getKey();

//If key is pressed
if(key){
Serial.print(key);
Serial.println(" Is pressed on your keypad");
}
}

Testing it out




Now you must have correctly wired the keypad to the Arduino as we explained in the wiring section, as well as uploaded the code to your Arduino board.


You may now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.



The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It can allow us to send and receive different commands as well as view information directly from the Arduino.


Now, as we can see in the following image, the serial monitor displays the input data from our keypad in single lines.

You should also make sure you have chosen the right baud rate (9600) as specified in the code.


Resources 


Arduino Code

Keypad Library

Fritzing Wiring file

Related Tutorials


A servo motor is just a simple DC motor with certain modifications. When you look at it, you'll notice that it has several gears and an electronic circuit.


Display data to LCD using Arduino 🖥️

LCDs are used in a range of everyday applications, including the automobile radio and the house air conditioning remote. They display data and let you control it through menus.


Control a relay using the Arduino

Most of us are familiar with the switches used in household devices. A relay is a type of switch that can be connected to an Arduino or any other microcontroller.