Introduction: How to Use Keypad & LCD With Arduino to Make Arduino Calculator.

About: I am a Programmer, Hacker, Maker and Technology enthusiast.

In this tutorial I will be sharing how you can use 4x4 matrix keypad and 16x2 LCD with Arduino and use it to make a simple Arduino Calculator.

So lets get started...

Step 1: Things You Will Require :-

Hardware Requirements :-

  1. Arduino UNO.
  2. 4x4 keypad. (You can use 4x3 keypad).
  3. 16x2 LCD.
  4. Breadboard.
  5. 10k potentiometer.
  6. some wires to solder to the keypad.

Software Requirements :-

  1. Arduino IDE.

That's all you gonna need for this project.

Step 2: Understanding the Keypad :-

So to use keypads first you have to understand how the keypad works.

Keypad is nothing but a buttoned matrix with nxn number of rows and columns. The Rows are horizontal and Columns are vertical.

In 4x4 matrix there are 4 Rows and 4 columns and in 4x3 there are 4 Rows and 3 Columns.

Each button in a row is connected to all the other buttons in the same row. Same with columns.

Pressing a button closes the switch between a column and a row trace, allowing current to flow between a Column pin and a Row pin. This is how arduino finds which button is pressed.

I don't want to dive deep into it and make the tutorial boring so if you wish to learn the working of keypad in depth you can check out this post.

Let's move on to the next step...

Step 3: Connections :-

1. Solder wires to the keypad. Solder header pins to another end.

2. Refer to the diagram to and make connections as follows :-

  • R1 = D2
  • R2 = D3
  • R3 = D4
  • R4 = D5
  • C1 = D6
  • C2 = D7
  • C3 = D8
  • C4 = D9

3. LCD connections are fairly simple too.

  • First connect LCD on breadboard.
  • Now connect pins RW, LED cathode and Vss or GND to the GND rail of breadboard.
  • Connect the Vcc to the +ve rail of breadboard. Also connect the LED anode pin (Right next to cathode) to +ve rail through a 220 ohm resistor.
  • Connect the contrast pin labeled as V0 to middle terminal of the potentiometer. Connect other two terminals of the pot to +ve and GND.
  • Now connect the following pin in order:
  • D4 = D13
  • D5 = D12
  • D6 = D11
  • D7 = D10

where, D2, D3,.....,D13 are Digital i/o pins of arduino.

Once the connections are made. We can move on to the coding step...

Step 4: Keypad Code :-

Before you can start coding you have to install a library to us the keypad and LCD.
To download the library, open IDE and goto :-

  • Sketch >> Include Library >> Manage Libraries.
  • In the search bar type "Keypad.h" and scroll down to find "Keypad library by Mark Stanley version 3.1.1"
  • Also check if LiquidCrystal library is installed. If not, you can find it using same method.
  • Install the libraries and restart the IDE.

Now copy the code below and paste it in the IDE. Upload it to arduino. (Code for 4x3 can be downloaded from below) :-

This code will help you check the working of Keypad, It shows the button pressed on Serial monitor.

/*Code for 4x4 keypad*/

#include<keypad.h>

const byte ROWS = 4; 
const byte COLS = 4;

char keys[ROWS][COLS] = 
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {9, 8, 7, 6};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(9600);
}
  
void loop()
{
  char key = keypad.getKey();
  
  if (key)
  {
    Serial.println(key);
  }
}

With this you can get started with keypad with arduino, The code for calculator is in next step..

Step 5: Arduino Calculator Code :-

Once you have tested the keypad, and it works fine. you can move on to making a simple calculator.

You can download the code from the file given below.

To use the calculator simply upload the code, The Alphabets are used as follows :-

A = + (Addition)

B = - (Subtraction)

C = * (Multiplication)

D = / (Division)

Symbol * and # are used as 'Cancel' and 'Equals to' Respectively.

That's all for this tutorial. Hope you like it.

Thank you.