Translate

Arduino Calculator using 4x4 Keypad and 16x2 LCD

 1. Introduction

Building a calculator using Arduino is an excellent beginner-to-intermediate project that combines input devices (keypad) and output devices (LCD display) with logical processing.

In this project, we design a fully functional calculator using:

  • A 4x4 matrix keypad for input
  • A 16x2 LCD display for output
  • Arduino Uno as the brain

This calculator supports:

  • Addition (+)
  • Subtraction (−)
  • Multiplication (×)
  • Division (÷)
  • Decimal numbers
  • Clear function (double press =)

2. Components

  • Arduino Uno
  • 16x2 LCD Display
  • 4x4 Matrix Keypad
  • Jumper Wires
  • Breadboard
  • USB Cable

3. Circuit and Connections

Circuit Diagram
Pinout Diagram of Arduino Uno R3


16X2 LCD Pinout Diagram
Pin-out Diagram 16X2 LCD

4X4 Keypad Pinout Diagram
4X4 Keypad Pin-out Diagram

🔌 LCD Connections

LCD PinArduino
RS12
EN11
D410
D59
D68
D77
VCC5V
GNDGND

🔌 Keypad Connections

Keypad PinArduino
R15
R24
R33
R42
C1A3
C2A2
C3A1
C4A0

4. Detailed Step By Step Circuit Working

  1. The keypad acts as the input device.
  2. Each key press is detected using row-column scanning.
  3. The Arduino reads the key and stores it in a variable.
  4. The LCD displays the entered number.
  5. When an operator is pressed:
    • First number is stored in memory
    • Operation is saved
  6. Second number is entered.
  7. On pressing =, Arduino:
    • Performs calculation
    • Displays result on LCD
  8. Double pressing = clears the screen.

5. Libraries to be Included

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

6. Code

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

/* LCD setup */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

/* Keypad setup */
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] = {5, 4, 3, 2};
byte colPins[COLS] = {A3, A2, A1, A0};

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

/* Variables */
String current = "";
String memory = "";
char operation = 0;

/* Splash screen */
void showSplashScreen() {
lcd.setCursor(0, 0);
lcd.print("Arduino Calc");
lcd.setCursor(2, 1);
lcd.print("Loading...");
delay(1500);
lcd.clear();
}

/* Calculate */
double calculate(char op, double a, double b) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/':
if (b == 0) {
lcd.clear();
lcd.print("Error: /0");
delay(1500);
lcd.clear();
return 0;
}
return a / b;
}
return 0;
}

/* Handle input */
void processKey(char key) {

static unsigned long lastPress = 0;

// CLEAR (double press '=')
if (key == '=' && millis() - lastPress < 500) {
current = "";
memory = "";
operation = 0;
lcd.clear();
lcd.setCursor(0,0);
return;
}

if (key == '=') lastPress = millis();

// Operator
if (key == '+' || key == '-' || key == '*' || key == '/') {
if (current != "") {
memory = current;
current = "";
operation = key;

lcd.setCursor(0,1);
lcd.print("Op: ");
lcd.print(operation);
}
return;
}

// Equal
if (key == '=') {
if (memory != "" && current != "" && operation) {
double result = calculate(operation, memory.toDouble(), current.toDouble());

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Result:");
lcd.setCursor(0,1);
lcd.print(result, 2);

current = String(result);
memory = "";
operation = 0;
}
return;
}

// Decimal
if (key == '.') {
if (current.indexOf('.') == -1) {
current += key;
lcd.print(key);
}
return;
}

// Numbers
if (isDigit(key)) {

lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);

current += key;
lcd.print(current);
}
}

/* Setup */
void setup() {
lcd.begin(16, 2);
showSplashScreen();
}

/* Loop */
void loop() {
char key = keypad.getKey();

if (key) {
processKey(key);
}
}

7. Detailed Step By Step Code Working

  • LiquidCrystal & Keypad libraries handle LCD and keypad communication
  • current → stores current number input
  • memory → stores first operand
  • operation → stores operator

Flow:

  1. User enters number → stored in current
  2. Press operator → moves currentmemory
  3. Enter next number
  4. Press = → calculation happens
  5. Result displayed
  6. Double = → clears everything

8. Tips

  • Use 10k potentiometer for LCD contrast
  • Avoid loose keypad connections
  • Use lcd.print(result, 2) for better readability
  • Keep wires short to avoid noise
  • Quick double click on "=" will clear LCD display

9. Uses

  • Educational projects
  • Arduino learning kits
  • Embedded system demonstrations
  • DIY calculator
  • Student lab experiments

10. Conclusion

This Arduino calculator project is a perfect combination of hardware interfacing and logical programming. It helps students understand real-time input processing, display control, and arithmetic operations.

With further improvements like memory functions and scientific operations, this can be upgraded into a fully featured calculator system.

à´¤ുà´Ÿà´•്à´•à´•്à´•ാർക്à´•ാà´¯ി ഇലക്à´Ÿ്à´°ോà´£ിà´•്à´¸് ലളിതമാà´¯ി പഠിà´•്à´•ാം.

Empowering students in Kerala with hands-on technical skills.