In this project, we’ll see how to make a calculator with Arduino. It will be able to perform addition, subtraction, multiplication, and division. To do this, we’ll use a keypad to enter numbers and an LCD screen to display the result.

Required materials

Now let’s look at the materials needed for the project:

  • An Arduino Uno board

  • A 16×2 Liquid Crystal LCD screen

  • A keypad

  • A 220-ohm resistor

  • Jumper wires (around fifteen!)

How does the calculator work?

To perform a calculation, you must first enter a number, then choose from several operations:

Button A: Addition
Button B: Subtraction
Button C: Multiplication
Button D: Division
Button #: To display the result
Button *: To reset the calculation

```cpp
#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // Initialize the LCD screen
// Variables to store the two numbers and the calculation result
long first_number = 0; 
long second_number = 0;
double total_result = 0;
// Define the keypad size
char key_pressed;
const byte ROWS = 4;
const byte COLS = 4;
// Define the keys on the keypad
char keys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','=','/'}
};
byte rowPins[ROWS] = {7,6,5,4};    // Connect to the row pins of the keypad
byte colPins[COLS] = {3,2,1,0};    // Connect to the column pins of the keypad

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

void setup() {
  lcd.begin(16, 2);               // Start the LCD
  for(int i = 0; i <= 3; i++);
  lcd.setCursor(0,0);             // Move cursor to the beginning of the screen
  lcd.print("Calculator");
  lcd.setCursor(0,1);
  lcd.print("Arduino Factory");
  delay(4000);
  lcd.clear();                   // Clear the LCD screen
  lcd.setCursor(0, 0);
}

void loop() {
  key_pressed = customKeypad.getKey();
  switch(key_pressed) {
    case '0' ... '9': // Keep building the first number until an operator is pressed
      lcd.setCursor(0,0);
      first_number = first_number * 10 + (key_pressed - '0');
      lcd.print(first_number);
      break;

    case '+': // Addition
      first_number = (total_result != 0 ? total_result : first_number);
      lcd.setCursor(0,1);
      lcd.print("+");
      second_number = getSecondNumber(); // Get the second number
      total_result = first_number + second_number;
      lcd.setCursor(0,3);
      lcd.print(total_result);
      first_number = 0; second_number = 0; // Reset for next calculation
      break;

    case '-': // Subtraction
      first_number = (total_result != 0 ? total_result : first_number);
      lcd.setCursor(0,1);
      lcd.print("-");
      second_number = getSecondNumber(); // Get the second number
      total_result = first_number - second_number;
      lcd.setCursor(0,3);
      lcd.print(total_result);
      first_number = 0; second_number = 0; // Reset for next calculation
      break;

    case '*': // Multiplication
      first_number = (total_result != 0 ? total_result : first_number);
      lcd.setCursor(0,1);
      lcd.print("*");
      second_number = getSecondNumber(); // Get the second number
      total_result = first_number * second_number;
      lcd.setCursor(0,3);
      lcd.print(total_result);
      first_number = 0; second_number = 0; // Reset for next calculation
      break;

    case '/': // Division
      first_number = (total_result != 0 ? total_result : first_number);
      lcd.setCursor(0,1);
      lcd.print("/");
      second_number = getSecondNumber(); // Get the second number
      lcd.setCursor(0,3);

      if (second_number == 0) {
        lcd.print("Invalid");
      } else {
        total_result = (float)first_number / (float)second_number;
        lcd.print(total_result);
      }
      first_number = 0; second_number = 0; // Reset for next calculation
      break;

    case 'C': // Reset the calculator
      total_result = 0;
      lcd.clear();
      break;
  }
}

long getSecondNumber() { // Function to capture the second number input
  while (1) {
    key_pressed = customKeypad.getKey();
    if (key_pressed >= '0' && key_pressed <= '9') {
      second_number = second_number * 10 + (key_pressed - '0');
      lcd.setCursor(0,2);
      lcd.print(second_number);
    }

    if (key_pressed == '=') break; // Finish entering second number
  }
  return second_number;
}
```