A modern technology banner image

DIY Arduino Calculator

OBJECTIVE

To design and simulate a basic calculator using Arduino that performs arithmetic operations through a keypad and displays results on an LCD..

MODULES REQUIRED

  • Arduino uno
  • Lcd 16X 2
  • Keypad
  • Resistors

SCHEMATIC DIAGRAM

Schematic diagram showing an Arduino connected to an LED and a push button.

SCHEMATIC CONNECTION

Connect Lcd:

  • Connect the VS pin of the Lcd to the Gnd pin on the Arduino .
  • Connect the VDD pin of the Lcd to the 5v pin on the Arduino .
  • Connect the RS pin of the Lcd to the D12 pin on the Arduino.
  • Connect the RW pin of the Lcd to the Gnd pin on the Arduino .
  • Connect the E pin of the Lcd to the D11 pin on the Arduino .
  • Connect the D4 pin of the Lcd to the D10 pin on the Arduino .
  • Connect the D5 pin on the Lcd to the D9 pin on the Arduino.
  • Connect the D6 pin of the Lcd to the D8 pin on the Arduino .
  • Connect the D7 pin of the Lcd to the D7 pin on the Arduino
  • connect the A pin to one end of the resistor and the other end of the resistor to the Arduino's 5V pin.
  • Connect Keypad :

  • Connect the R1 to R4 pin of the keypad to the D5 to D2 pin on the Arduino .
  • Connect the C1 to C4 pin of the keypad to the A3 to A0 pin on the Arduino.
  • To begin your project, click this template link:

    Simulate on Wokwi

    ARDUINO CODE

    
    #include (LiquidCrystal.h)
    #include (Keypad.h)
    #include (Servo.h)
    
    /* Display */
    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', '*'},
      {'.', '0', '=', '/'}
    };
    
    Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
    
    uint64_t value = 0;
    
    void showSpalshScreen() {
      lcd.print("GoodArduinoCode");
      lcd.setCursor(3, 1);
      String message = "Calculator";
      for (byte i = 0; i < message.length(); i++) {
        lcd.print(message[i]);
        delay(50);
      }
      delay(500);
    }
    
    void updateCursor() {
      if (millis() / 250 % 2 == 0 ) {
        lcd.cursor();
      } else {
        lcd.noCursor();
      }
    }
    
    void setup() {
      Serial.begin(115200);
      lcd.begin(16, 2);
    
      showSpalshScreen();
      lcd.clear();
      lcd.cursor();
    
      lcd.setCursor(1, 0);
    }
    
    char operation = 0;
    String memory = "";
    String current = "";
    uint64_t currentDecimal;
    bool decimalPoint = false;
    
    double calculate(char operation, double left, double right) {
      switch (operation) {
        case '+': return left + right;
        case '-': return left - right;
        case '*': return left * right;
        case '/': return left / right;
      }
    }
    
    void processInput(char key) {
      if ('-' == key && current == "") {
        current = "-";
        lcd.print("-");
        return;
      }
    
      switch (key) {
        case '+':
        case '-':
        case '*':
        case '/':
          if (!operation) {
            memory = current;
            current = "";
          }
          operation = key;
          lcd.setCursor(0, 1);
          lcd.print(key);
          lcd.setCursor(current.length() + 1, 1);
          return;
    
        case '=':
          float leftNum = memory.toDouble();
          float rightNum = current.toDouble();
          memory = String(calculate(operation, leftNum, rightNum));
          current = "";
          lcd.clear();
          lcd.setCursor(1, 0);
          lcd.print(memory);
          lcd.setCursor(0, 1);
          lcd.print(operation);
          return;
    
      }
    
      if ('.' == key && current.indexOf('.') >= 0) {
        return;
      }
    
      if ('.' != key && current == "0") {
        current = String(key);
      } else if (key) {
        current += String(key);
      }
    
      lcd.print(key);
    }
    
    void loop() {
      updateCursor();
    
      char key = keypad.getKey();
      if (key) {
        processInput(key);
      }
    }
    
    
    
    
    
    

    INSTRUCTIONS

  • Assemble the circuit by connecting all components as shown in the schematic diagram.
  • On the left side of the screen, open sketch.ino and write your Arduino code.
  • After completing the circuit and writing the code, click the green Play button to start the simulation.
  • To perform calculations, enter two numbers and select the operator (addition, subtraction, multiplication, or division) to get the result.
  • WORKING PRINCIPLE

  • The DIY Arduino calculator takes user input from a keypad, processes the numbers and selected arithmetic operation (addition, subtraction, multiplication, division), and displays the result on an LCD screen.

  • Project Link

    for your reference.

    `