Arduino Calculator with LCD and 4x4 Keypad project with code

Project Description: Arduino Calculator with LCD and Keypad

This project is about making a simple yet functional calculator with an Arduino microcontroller. It has a 16x2 LCD display and a 4x4 matrix keypad. Users can input numbers and operations like (+, -, *, /) on the keypad. The LCD shows the input and results, and it gives feedback for calculations.

This project is great for learning about Arduino programming, LCD interfacing, and matrix keypad handling.

Key Features

  1. User-Friendly Interface:

    • Uses a 4x4 keypad for input and a 16x2 LCD for display.
    • Displays both the entered expression and the computed result.
  2. Core Functionality:

    • Supports addition, subtraction, multiplication, and division.
    • Handles errors, such as division by zero.
  3. Customization:

    • Easily expandable to include advanced mathematical operations.
    • Compatible with most Arduino boards.
  4. Educational Value:

    • Teaches interfacing of hardware components like keypads and LCDs with Arduino.
    • Demonstrates practical use of programming concepts like string manipulation and input parsing.

Components Needed

  • Arduino Uno or compatible microcontroller.
  • 16x2 LCD Display (HD44780 compatible).
  • 4x4 Keypad (16-key matrix).
  • Resistors, jumper wires, and a breadboard for connections.

How It Works

  1. Input: The user enters a mathematical expression using the keypad.
  2. Processing: The Arduino parses the input, identifies the numbers and the operator, and performs the required calculation.
  3. Output: The LCD displays the result or an error message (e.g., division by zero).

Applications

  • Learning Arduino: Ideal for beginners to understand input-output handling.
  • DIY Projects: Can be integrated into other electronic systems.
  • Teaching Tool: Demonstrates basic computational logic and Arduino capabilities.

Schematic:

Schematic of the calculator

Arduino code: 
#include <LiquidCrystal.h>
#include <Keypad.h>

/* Display Setup */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

/* Keypad Setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'C', '0', '=', '/'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);

String inputBuffer = ""; // Store user input

void setup() {
  lcd.begin(16, 2);
  lcd.print("Calculator");
  delay(2000);
  lcd.clear();
  lcd.print("Enter:");
}

void loop() {
  char key = keypad.getKey();
  if (key) {
    if (key == 'C') {
      // Clear input
      inputBuffer = "";
      lcd.clear();
      lcd.print("Enter:");
    } else if (key == '=') {
      // Calculate result
      lcd.clear();
      double result = calculate(inputBuffer);
      lcd.setCursor(0, 0);
      lcd.print("Result:");
      lcd.setCursor(0, 1);
      lcd.print(result);
      delay(5000);
      inputBuffer = "";
      lcd.clear();
      lcd.print("Enter:");
    } else {
      // Append input
      inputBuffer += key;
      lcd.setCursor(0, 1);
      lcd.print(inputBuffer);
    }
  }
}

// Function to calculate the result of the expression
double calculate(String input) {
  char operation = 0;
  double num1 = 0, num2 = 0;
  bool parsingNum2 = false;
  String tempNum = "";

  // Parse the input
  for (int i = 0; i < input.length(); i++) {
    char c = input[i];
    if (isdigit(c) || c == '.') {
      tempNum += c;
    } else {
      if (!parsingNum2) {
        num1 = tempNum.toDouble();
        tempNum = "";
        operation = c;
        parsingNum2 = true;
      } else {
        // Error: Multiple operations
        return 0;
      }
    }
  }
  if (tempNum != "") {
    num2 = tempNum.toDouble();
  }

  // Perform the operation
  switch (operation) {
    case '+': return num1 + num2;
    case '-': return num1 - num2;
    case '*': return num1 * num2;
    case '/': return (num2 != 0) ? num1 / num2 : 0; // Avoid division by zero
    default: return 0; // Invalid operation
  }
}


Json code:
{
  "version": 1,
  "author": "https://www.youtube.com/@EasyElectronics2",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": 200, "left": 20, "attrs": {} },
    { "type": "wokwi-membrane-keypad", "id": "keypad", "top": 140, "left": 360, "attrs": {} },
    { "type": "wokwi-lcd1602", "id": "lcd", "top": 8, "left": 20, "attrs": {} },
    { "type": "wokwi-resistor", "id": "r1", "top": 140, "left": 220, "attrs": { "value": "220" } }
  ],
  "connections": [
    [ "uno:GND.1", "lcd:VSS", "black", [ "v-51", "*", "h0", "v18" ] ],
    [ "uno:GND.1", "lcd:K", "black", [ "v-51", "*", "h0", "v18" ] ],
    [ "uno:GND.1", "lcd:RW", "black", [ "v-51", "*", "h0", "v18" ] ],
    [ "uno:5V", "lcd:VDD", "red", [ "v16", "h-16" ] ],
    [ "uno:5V", "r1:2", "red", [ "v16", "h-118", "v-244", "h50" ] ],
    [ "r1:1", "lcd:A", "pink", [] ],
    [ "uno:12", "lcd:RS", "blue", [ "v-16", "*", "h0", "v20" ] ],
    [ "uno:11", "lcd:E", "purple", [ "v-20", "*", "h0", "v20" ] ],
    [ "uno:10", "lcd:D4", "green", [ "v-24", "*", "h0", "v20" ] ],
    [ "uno:9", "lcd:D5", "brown", [ "v-28", "*", "h0", "v20" ] ],
    [ "uno:8", "lcd:D6", "gold", [ "v-32", "*", "h0", "v20" ] ],
    [ "uno:7", "lcd:D7", "gray", [ "v-36", "*", "h0", "v20" ] ],
    [ "uno:A3", "keypad:C1", "brown", [ "v116", "*", "h0", "v0" ] ],
    [ "uno:A2", "keypad:C2", "gray", [ "v120", "*", "h0", "v0" ] ],
    [ "uno:A1", "keypad:C3", "orange", [ "v124", "*", "h0", "v0" ] ],
    [ "uno:A0", "keypad:C4", "pink", [ "v128", "*", "h0", "v0" ] ],
    [ "uno:5", "keypad:R1", "blue", [ "v-34", "h96", "*", "v12" ] ],
    [ "uno:4", "keypad:R2", "green", [ "v-30", "h80", "*", "v16" ] ],
    [ "uno:3", "keypad:R3", "purple", [ "v-26", "h64", "*", "v20" ] ],
    [ "uno:2", "keypad:R4", "gold", [ "v-22", "h48", "*", "v24" ] ]
  ],
  "dependencies": {}
}


  • Arduino Calculator Project
  • DIY Arduino Calculator
  • Arduino LCD Keypad Calculator
  • Arduino 4x4 Keypad Tutorial
  • Arduino Math Project
  • Simple Arduino Calculator
  • Arduino Microcontroller Projects
  • Arduino Educational Projects
  • Arduino Programming for Beginners
  • Arduino LCD Display Projects
  • Matrix Keypad Arduino Example
  • Arduino LCD Calculator Code



  • Comments