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
| LCD Pin | Arduino |
|---|---|
| RS | 12 |
| EN | 11 |
| D4 | 10 |
| D5 | 9 |
| D6 | 8 |
| D7 | 7 |
| VCC | 5V |
| GND | GND |
🔌 Keypad Connections
| Keypad Pin | Arduino |
|---|---|
| R1 | 5 |
| R2 | 4 |
| R3 | 3 |
| R4 | 2 |
| C1 | A3 |
| C2 | A2 |
| C3 | A1 |
| C4 | A0 |
4. Detailed Step By Step Circuit Working
- The keypad acts as the input device.
- Each key press is detected using row-column scanning.
- The Arduino reads the key and stores it in a variable.
- The LCD displays the entered number.
- When an operator is pressed:
- First number is stored in memory
- Operation is saved
- Second number is entered.
- On pressing
=, Arduino:- Performs calculation
- Displays result on LCD
- 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 inputmemory→ stores first operandoperation→ stores operator
Flow:
- User enters number → stored in
current - Press operator → moves
current→memory - Enter next number
- Press
=→ calculation happens - Result displayed
- 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.