Arduino Based Calculator using Keypad & LCD for Calculation

Arduino Based Calculator using Keypad & LCD (Last Updated On: December 5, 2018)

Arduino Based Calculator using Keypad & LCD:

A simple Arduino Based Calculator using Keypad & LCD for Solving Mathematical Calculations can be easily implemented using Arduino, LCD and Keypad. A simple mathematical calculation like Addition, Subtraction, Multiplication and Division can easily done using this project.

The 16*2 LCD is capable of solving mathematical problem up to 16 digits without a delay and fast processing. Even the Arduino program is easy and is almost like simple C program, merely based on simple formula for addition, subtraction, multiplication and division. So lets implement this simple Project.


Circuit Diagram and Requirements:

The circuit diagram is so easy as it can made either by assembling the circuit directly on Arduino board or by making the assembled circuit in pcb. The requirements for the circuit are as follows.

1. Arduino Uno Board
2. 16*2 LCD
3. 4*4 Membrane Matrix Keypad
4. 1 kilo ohm resister (2)
5. Reset Switch
6. On/Off Switch
7. 1 kilo ohm Potentiometer
8. 9 volt Battery
10. Bread Board & Connecting Wires

Arduino Based Calculator using Keypad & LCD


Program & Source Code:

The programming is done in Arduino language taking Arduino Uno as reference. You need to add an extra Arduino library file for keypad as #include <Keypad.h >. So download this from adafruit library or just update the library file. After assembling, just upload the source code in Arduino Uno board and enjoy your Arduino Based Calculator


#include <Keypad.h>
#include <LiquidCrystal.h>

// 1. LCD Pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// 2. Keypad Pins
const byte Rows = 4;
const byte Cols = 4;
char keys[Rows][Cols] =
{
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'C', '0', '=', '/'}
};
byte rowPins[Rows] = {A2, A3, A4, A5};
byte colPins[Cols] = {2, 3, 4, 5};
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, Rows, Cols);

// 3. Dot Button
int dot = A0;
int dotFlag = 0;
int dotButton = 0;

// 4. Calculator Operators
float num1, num2, fraction;
float total;
char operation, button;

// 5. Loading Setup
char input[16];
int n = 1750;

void setup()
{
// Initialize dot button as input to Arduino
pinMode(dot, INPUT);

// Initialize LCD Size
lcd.begin(16, 2);

// LCD Loading Setup Begin
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("LOADING...");
for (int i = 0; i < 16; i++)
{
lcd.setCursor(i, 1);
lcd.write(255);
delay(50);
}
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Simple Arduino");
lcd.setCursor(3, 1);
lcd.print("Calculator");
delay(n);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Designed By");
lcd.setCursor(2, 1);
lcd.print("Alex Newton");
delay(n);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Alex Newton");
lcd.setCursor(2, 1);
lcd.print("how2electronics.com");
delay(n);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Alex Newton");
lcd.setCursor(2, 1);
lcd.print("how2electronics.com");
delay(n);
lcd.clear();
// LCD Loading Setup End
}
void loop()
{
// First while loop for num1.
while (1)
{
dotButton = digitalRead(dot);
button = customKeypad.getKey();
if (button == 'C')
{
dotFlag = 0;
num1 = 0;
num2 = 0;
fraction = 0;
total = 0;
operation = 0;
lcd.clear();
}
else if (dotButton == LOW)
{
dotFlag = 1;
}
else if (button >= '0' && button <= '9')
{
if (dotFlag == 0)
{
num1 = num1 * 10 + (button - '0');
lcd.setCursor(0, 0);
lcd.print(num1);
}
else if (dotFlag == 1)
{
fraction = (button - '0');
num1 = num1 + (fraction / 10);
lcd.setCursor(0, 0);
lcd.print(num1);
dotFlag++;
}
else if (dotFlag == 2)
{
fraction = (button - '0');
num1 = num1 + (fraction / 100);
lcd.setCursor(0, 0);
lcd.print(num1);
dotFlag++;
}
}
else if (button == '-' || button == '+' || button == '*' || button == '/')
{
operation = button;
dotFlag = 0;
lcd.setCursor(0, 1);
lcd.print(operation);
break;
}
}
// Second while loop for num2.
while (1)
{
dotButton = digitalRead(dot);
button = customKeypad.getKey();
if (button == 'C')
{
dotFlag = 0;
num1 = 0;
num2 = 0;
fraction = 0;
total = 0;
operation = 0;
lcd.clear();
break;
}
else if (dotButton == LOW)
{
dotFlag = 1;
}
else if (button >= '0' && button <= '9')
{
if (dotFlag == 0)
{
num2 = num2 * 10 + (button - '0');
lcd.setCursor(1, 1);
lcd.print(num2);
}
else if (dotFlag == 1)
{
fraction = (button - '0');
num2 = num2 + (fraction / 10);
lcd.setCursor(1, 1);
lcd.print(num2);
dotFlag++;
}
else if (dotFlag == 2)
{
fraction = (button - '0');
num2 = num2 + (fraction / 100);
lcd.setCursor(1, 1);
lcd.print(num2);
dotFlag++;
}
}
if (button == '=')
{
domath();
break;
}
}
// Third while loop for ensuring C button is executed after while loop 2.
while (1)
{
button = customKeypad.getKey();
if (button == 'C')
{
dotFlag = 0;
num1 = 0;
num2 = 0;
fraction = 0;
total = 0;
operation = 0;
lcd.clear();
break;
}
}
}
void domath()
{
switch (operation)
{
case '+':
total = num1 + num2;
break;
case '-':
total = num1 - num2;
break;
case '/':
total = num1 / num2;
break;
case '*':
total = num1 * num2;
break;
}
lcd.print('=');
if (operation == '/' && num2 == 0)
{
lcd.print("ERROR 0 DIV");
}
else
{
lcd.print(total);
}
}



Recommended For You

Ask A Question .......

Close