Simple Calculator (with Arduino)

Athur Muharam
5 min readApr 1, 2019

--

Simple calculator

On this project, me and my team are building a simple calculator with 4 basic functions using keypad as input and LCD as output and can only compute up to 2 digits. As 4 functions that are going to be available on this project are:

  1. Addition
  2. Multiplication
  3. Subtraction
  4. Rounded down Division

Materials

Here are the materials and components that will be needed:

  1. 1 Arduino UNO board
  2. 1 variable resistor
  3. 1 4x4 keypad
  4. 1 breadboard
  5. 1 LCD Display
  6. several male to male jumper cables
  7. Solder and lead

Installation steps (with diagrams)

Firstly, we need to solder 16-pins male-male to LCD display (in order to firmly connect LCD with the breadboard). Here is a tutorial video for easy steps to solder LCD display.

After we confirm that all pins are set firmly, we will need to install all of the components based on the following diagrams, where power goes to LCD and variable resistor (and so does the ground) and some of the digital pins goes to LCD some goes to Keypad.

Arduino Calculator Circuit Diagram (cc: https://circuitdigest.com/microcontroller-projects/arduino-calculator-using-4x4-keypad)

After all hardware parts are set, we will need to configure and set up our arduino software. But before we go there, make sure all of the components are connected rightfully and with the right pin.

Arduino Code Setup

We will use keypad and LCD, thus we will need to include these devices library on our codes to make sure our device could run.

#include <LiquidCrystal.h> //Header file for LCD from https://www.arduino.cc/en/Reference/LiquidCrystal#include <Keypad.h> //Header file for Keypad from https://github.com/Chris--A/Keypad

Since we will use keypad, so we will need to setup the keypad first. What you need to know regarding keypad is that it is structured like matrix so every button needs to be saved on the elements of matrix. Next the pin that are connected between arduino and keypad are respectively ROWS and COLUMNS. Therefore we will need to declare and setup these digital pins.

// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};byte rowPins[ROWS] = { 0, 1, 2, 3 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 4, 5, 6, 7 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create the Keypad

As for LCD or Liquid Crystal Display you will need to declare all the digital pins that are connected to LCD and instantiate a LiquidCrystal object.

const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; //Pins to which LCD is connectedLiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Then you may setup all variables that you might need (as in this project only using 3 numeric var, 2 char var, and a result flag) and define your function. There are 4 function on our Arduino code:

  1. basic setup: for initiate display
  2. DetectButtons(): to display and save the right input from keypad
  3. CalculateResults(): To calculate based on the corresponding function (from inputted char)
  4. DisplayResult(): To display and show the result on LCD.

Following are the rest of our Arduino codes on this project,

//Variables
long Num1,Num2,Number;
char key,action;
boolean result = false;
//Basic Setup
void setup() {
lcd.begin(16, 2); //We are using a 16*2 LCD display
lcd.print("Mari Menghitung"); //Display a intro message
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
lcd.print("- AthurYociPhas"); //Display a intro message
delay(3000); //Wait for display to show info
lcd.clear(); //Then clean it
}
void loop() {

key = kpd.getKey(); //storing pressed key value in a char
if (key!=NO_KEY)
DetectButtons();
if (result==true)
CalculateResult();
DisplayResult();
}
void DetectButtons()
{
lcd.clear(); //Then clean it
if (key=='*') //If cancel Button is pressed
{Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}

if (key == '1') //If Button 1 is pressed
{Serial.println ("Button 1");
if (Number==0)
Number=1;
else
Number = (Number*10) + 1; //Pressed twice
}

if (key == '4') //If Button 4 is pressed
{Serial.println ("Button 4");
if (Number==0)
Number=4;
else
Number = (Number*10) + 4; //Pressed twice
}

if (key == '7') //If Button 7 is pressed
{Serial.println ("Button 7");
if (Number==0)
Number=7;
else
Number = (Number*10) + 7; //Pressed twice
}
if (key == '0')
{Serial.println ("Button 0"); //Button 0 is Pressed
if (Number==0)
Number=0;
else
Number = (Number*10) + 0; //Pressed twice
}

if (key == '2') //Button 2 is Pressed
{Serial.println ("Button 2");
if (Number==0)
Number=2;
else
Number = (Number*10) + 2; //Pressed twice
}

if (key == '5')
{Serial.println ("Button 5");
if (Number==0)
Number=5;
else
Number = (Number*10) + 5; //Pressed twice
}

if (key == '8')
{Serial.println ("Button 8");
if (Number==0)
Number=8;
else
Number = (Number*10) + 8; //Pressed twice
}
if (key == '#')
{Serial.println ("Button Equal");
Num2=Number;
result = true;
}

if (key == '3')
{Serial.println ("Button 3");
if (Number==0)
Number=3;
else
Number = (Number*10) + 3; //Pressed twice
}

if (key == '6')
{Serial.println ("Button 6");
if (Number==0)
Number=6;
else
Number = (Number*10) + 6; //Pressed twice
}

if (key == '9')
{Serial.println ("Button 9");
if (Number==0)
Number=9;
else
Number = (Number*10) + 9; //Pressed twice
}
if (key == 'A' || key == 'B' || key == 'C' || key == 'D') //Detecting Buttons on Column 4
{
Num1 = Number;
Number =0;
if (key == 'A')
{Serial.println ("Addition"); action = '+';}
if (key == 'B')
{Serial.println ("Subtraction"); action = '-'; }
if (key == 'C')
{Serial.println ("Multiplication"); action = '*';}
if (key == 'D')
{Serial.println ("Devesion"); action = '/';}
delay(100);
}

}
void CalculateResult()
{
if (action=='+')
Number = Num1+Num2;
if (action=='-')
Number = Num1-Num2;
if (action=='*')
Number = Num1*Num2;
if (action=='/')
(float)Number = (float)Num1/(float)Num2;
}
void DisplayResult()
{
lcd.setCursor(0, 0); // set the cursor to column 0, line 1
lcd.print(Num1); lcd.print(action); lcd.print(Num2);

if (result==true)
{lcd.print(" ="); lcd.print(Number);
} //Display the result

lcd.setCursor(0, 1); // set the cursor to column 0, line 1
lcd.print(Number); //Display the result
if ( (((Number) % 2) == 0) && ((Number) != 0))
{
lcd.setCursor(3, 1);
lcd.print("Genap");
}
else if ( (((Number) % 2) != 0) && ((Number) != 0))
{
lcd.setCursor(3, 1);
lcd.print("Ganjil");
}
}

The Results

Here are the results of these project

The hardware result of basic calculator

Credits

Our projects can be built without the helps of several authors, creators, and innovators. Therefore the highest appreciation for all of you guys.

  1. https://circuitdigest.com/microcontroller-projects/arduino-calculator-using-4x4-keypad
  2. https://www.arduino.cc/reference/en/language/variables/data-types/float/
  3. Yocia Meiko, for implementing and realizing this project.

--

--

Athur Muharam

Graduated from ITB on IS major. had experience in GrabKios, Hijup, and Indosat Ooredoo. Currently aspiring to be a "Good & Well Suit" Product Manager.