Arduino Calculator Using 4X4 Keypad

21K1518

Intro: Arduino Calculator Using 4X4 Keypad

In this tutorial we will build our own calculator with Arduino. The values can be sent in through a keypad (4×4 keypad) and result can be viewed on a LCD screen. This calculator could perform simple operations like Addition, Subtraction, Multiplication and Division with whole numbers. But once you understand the concept you can implement even scientific functions with Arduino’s built in functions.

STEP 1: Schematics

STEP 2: Installation of Library :

As told earlier we are going to interface a LCD and keypad with Arduino using libraries. So let’s add them to our Arduino IDE first. The library for LCD is already included in your Arduino by default so we need not worry about it. For Keypad library ( click on the link to download it from Github). You will get a ZIP file, then add this lib to Arduino by Sketch -> Include Library -> Add .ZIP file and point the location to this downloaded file. Once done we are all set for programming.

STEP 3: Source Code :

/*

© Techtronic Harsh */

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

LiquidCrystal lcd(0, 1, 2, 3, 4, 5); 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] = {13,12,11,10}; byte colPins[COLS] = {9,8,7,6};

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

boolean presentValue = false; boolean next = false; boolean final = false; String num1, num2; int answer; char op;

void setup() { lcd.begin(16,2); lcd.setCursor(0,0); lcd.print("Techtronic Harsh"); lcd.setCursor(0,1); lcd.print(" Calculator"); delay(3000); lcd.clear(); lcd.setCursor(0,0); lcd.print(" Like And"); lcd.setCursor(0,1); lcd.print(" Subscribe Us" ); delay(3000); 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 (presentValue != 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 (presentValue == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')) { if (presentValue == false) { presentValue = true; op = key; lcd.setCursor(15,0); lcd.print(op); } }

else if (final == true && key != NO_KEY && key == '='){ if (op == '+'){ answer = num1.toInt() + num2.toInt(); } else if (op == '-'){ answer = num1.toInt() - num2.toInt(); } else if (op == '*'){ answer = num1.toInt() * num2.toInt(); } else if (op == '/'){ answer = num1.toInt() / num2.toInt(); } lcd.clear(); lcd.setCursor(15,0); lcd.autoscroll(); lcd.print(answer); lcd.noAutoscroll(); } else if (key != NO_KEY && key == 'C'){ lcd.clear(); presentValue = false; final = false; num1 = ""; num2 = ""; answer = 0; op = ' '; } }

/*

© Techtronic Harsh */

STEP 4: Working :


Make the connections as per circuit diagram and upload the code. If it shows error make sure you have added the library as per the instruction given above.

Character on Keypad and Assumption :

  • “A” - Addition (+)
  • “B” - Subtraction (-)
  • “C” - Multiplication (*)
  • “D” - Division (/)
  • “*” - Clear (C)
  • “#” - Equals (=)


9 Comments

Hi! I really thought your project is cool. I am trying to build a calculator as well but my original plan was to make the calculator speak the answer using speakers? Would it be possible to do this as I am having difficulty right now.
If I do not have a 10K potentiometer, could I use 2 5k Ohm resistors?
This was fun and cool! I edited it a bit. Now, instead of the clear button, you have the decimal point button. When you press equal, and you start typing new numbers, it will automatically clear. Also, it can solve bigger numbers, and if you have a number thats too big, it will say OVF. (overflow) Script is below: (please delete your whole script and paste this one in. Enjoy!

#include
#include
LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
byte rowPins[ROWS] = {13,12,11,10};
byte colPins[COLS] = {9,8,7,6};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
boolean presentValue = false;
boolean next = false;
boolean final = false;
String num1, num2;
float answer;
char op;
boolean answerprinted = false;
void setup()
{
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Calculator");
lcd.setCursor(0,1);
lcd.print("Calculator");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Calculator ");
lcd.setCursor(0,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'||key == '.'))
{
if (answerprinted == true)
{
lcd.clear();
lcd.setCursor(15,0);
answerprinted = false;
}
if (presentValue != 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 (presentValue == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+'))
{
if (presentValue == false)
{
presentValue = true;
op = key;
lcd.setCursor(15,0);
lcd.print(op);
}
}
else if (final == true && key != NO_KEY && key == '='){
if (op == '+'){
answer = num1.toFloat() + num2.toFloat();
}
else if (op == '-'){
answer = num1.toFloat() - num2.toFloat();
}
else if (op == '*'){
answer = num1.toFloat() * num2.toFloat();
}
else if (op == '/'){
answer = num1.toFloat() / num2.toFloat();
}
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
lcd.print(answer);
lcd.noAutoscroll();
answerprinted = true;
num1 = '\0';
num2 = '\0';
final = false;
presentValue = false;
}
}
Great! Although, to prevent the user from entering numbers a second time without clearing, I made a boolean called, "ansPresent", and whenever the answer was shown on the lcd, the if statements would not execute. For example, this was my first if statement:
if (ansPresent != true && key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
{
The ansPresent != true will prevent them from messing up calculator, and when the answer is present, just make ansPresent = true to prevent the user. Again, great project.
This is the final code:
boolean ansPresent = false;
boolean opSelected = false;
boolean final = false;
String num1, num2;
int answer;
char op;
void setup() {
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Arduino");
lcd.setCursor(0,1);
lcd.print("Calculator");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
}
void loop(){
char key = myKeypad.getKey();
if (ansPresent == false && key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')) {
if (opSelected == false) {
num1 = num1 + key;
lcd.setCursor(0, 0);;
lcd.print(num1);
}
else {
num2 = num2 + key;
lcd.setCursor(0, 1);
lcd.print(num2);
final = true;
}
}
else if (ansPresent == false && opSelected == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')) {
opSelected = true;
op = key;
lcd.setCursor(15, 0);
lcd.print(op);
lcd.setCursor(15, 1);
lcd.print("=");
}
else if (ansPresent == false && final == true && key != NO_KEY && key == '=') {
if (op == '+'){
answer = num1.toInt() + num2.toInt();
}
else if (op == '-'){
answer = num1.toInt() - num2.toInt();
}
else if (op == '*'){
answer = num1.toInt() * num2.toInt();
}
else if (op == '/'){
answer = num1.toInt() / num2.toInt();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(answer);
ansPresent = true;
}
else if (key != NO_KEY && key == 'C'){
lcd.clear();
ansPresent = false;
opSelected = false;
final = false;
num1 = "";
num2 = "";
answer = 0;
op = ' ';
}
}