Menu Close

Arduino Calculator

This tutorial of Robo India explains, how to make a calculator using Arduino.

1. Introduction:

Calculator is a device , which is used to perform simple Arithmatic Operations. In this tutorial, we have designed Simple Arithmetic Arduino Calculator using 4×4 Keypad Matrix.

1.1 Working:

In this project, 4×4 Keypad matrix is used as Calculator Keypad. On which, number buttons are used to write numbers and for arithmetic operator.

Button Description

A will be used as plus(+) operator, B as minus(-) operator, C as for Clear Screen(X), D as to get result(=), * button as multiple(*) operator and # button as divide(/) operator.

You can change these, according to you by changing in Arduino IDE Code.

2. Required Hardware

Following Hardware will be required to perform this circuit.

S.No.ItemQuantity
1 Arduino UNO 1
2 I2C Scanner 1
3 4X4 Keypad Matrix 1
4 Male to Female Jumper 4
5 Male to Male Jumper 8

3. Building Circuit

Make the following connections with Arduino-

4. Library File

Following library will be required to run this sketch. Download the zip file extract the same and copy this to your Arduino library folder.

This library file should be placed at the install folder of Arduino. I have a 64 bit Win7 OS and my arduino library folder address is located at

C:\Program Files (x86)\Arduino\libraries 

You may download library file from here.

5. Programming

You may download this Arduino Sketch from here.


#include <Keypad.h>                      //import keypad library

#include <Wire.h>  
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,20, 4);

const byte ROWS = 4;                    // four rows
const byte COLS = 4;                    // four columns
boolean valOnePresent = false;
boolean next = false;
boolean final = false;
String num1, num2;
int ans;
char op;


char keys [ROWS] [COLS] = {             // define he keypad
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', 'X'},
  {'*', '0', '/', '='}
};

byte rowPins[ROWS] = {13 ,12 ,11 ,10}; //connect keypad ROW1, ROW2, ROW3, ROW4 to these arduino pins
byte colPins[COLS] = {9, 8, 7, 6};     //connect keypad COL1, COL2, COL3, COL4 to these arduino pins

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

void setup()
{
  lcd.init();
  lcd.backlight();          // makes Backligh ON. 
  lcd.clear();              // Clears LCD
  lcd.setCursor(5, 0);
  lcd.print("Arduino");     //display on LCD after code upload
  lcd.setCursor(3, 1);
  lcd.print("Calculator");
  delay(2000);
  lcd.clear(); 
  
}

void loop()
{
  char key = myKeypad.getKey();
  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
  {
    if (valOnePresent != true)
    {
      num1 = num1 + key;
      int numLength = num1.length();
      lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
      lcd.print(num1);
    }
    else 
    {
      num2 = num2 + key;
      int numLength = num2.length();
      lcd.setCursor(15 - numLength, 1);
      lcd.print(num2);
      final = true;
    }
  }
  else if (valOnePresent == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+'))
  {
    if (valOnePresent == false)
    {
      valOnePresent = true;
      op = key;
      lcd.setCursor(15,0); //operator on right corner
      lcd.print(op);
    }
  }
  else if (final == true && key != NO_KEY && key == '=')
  {
    if (op == '+')
    {
      ans = num1.toInt() + num2.toInt();
    }
    else if (op == '-')
    {
      ans = num1.toInt() - num2.toInt();
    }
    else if (op == '*')
    {
      ans = num1.toInt() * num2.toInt();
    }
    else if (op == '/')
    {
      ans = num1.toInt() / num2.toInt();
    }    
    
    lcd.clear();
    lcd.setCursor(15,0);
    lcd.autoscroll();
    lcd.print(ans);
    lcd.noAutoscroll();
  }
  else if (key != NO_KEY && key == 'X')
  {
    lcd.clear();
    valOnePresent = false;
    final = false;
    num1 = "";
    num2 = "";
    ans = 0;
    op = ' ';
  }
}

6. Output

After uploading the code successfully, you can perform simple arithmetic calculation using Keypad matrix.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply