Build Your Own Arduino Calculator

13 May 2023 Balmiki Mandal 0 µC - µP

How to Use an Arduino Calculator

Building your own calculator using an Arduino is a great introduction to the world of microcontrollers. With just a few components, you can build a fully-functional calculator that can perform basic mathematical operations. In this guide we will be showing you how to use an Arduino to build a simple calculator with push buttons.

What You Will Need

  • Arduino Uno
  • 9 Pushbuttons
  • Breadboard
  • Jumper Wires
  • LED Display

Setting Up the Circuit

Before you can begin writing code for your calculator, you will need to build the circuit first. Start by connecting the pushbuttons to the breadboard in a grid pattern. Then connect each of the pushbuttons to one of the Arduino’s digital pins. Finally, connect the LED display to the Arduino using jumper wires.

Writing the Code

Next, you will need to write the code for your calculator. Begin by initializing the pins used for the pushbuttons and the LED display. Then create variables to store the first and second numbers and the operator. After that, set up a loop to wait for the user to input their numbers and choose an operator. Finally, write the code for the calculator to perform the operation.

Here is a sample code that can be used to create a simple calculator using Arduino:

#include <Keypad.h>

const byte ROWS = 4; // Define number of rows
const byte COLS = 4; // Define number of columns

// Define keypad layout
char keys[ROWS][COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'C', '0', '=', '/'}
};

// Define keypad pins
byte rowPins[ROWS] = { 9, 8, 7, 6 };
byte colPins[COLS] = { 5, 4, 3, 2 };

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

float num1, num2, result; // Variables to store numbers and result
char op; // Variable to store operator

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey(); // Read keypad input

  if (key != NO_KEY) {
    Serial.print(key); // Print key to serial monitor
    
    // Perform action based on key pressed
    switch (key) {
      case 'C':
        num1 = 0;
        num2 = 0;
        result = 0;
        op = '\0';
        break;
        
      case '+':
      case '-':
      case '*':
      case '/':
        op = key;
        break;
        
      case '=':
        switch (op) {
          case '+':
            result = num1 + num2;
            break;
            
          case '-':
            result = num1 - num2;
            break;
            
          case '*':
            result = num1 * num2;
            break;
            
          case '/':
            result = num1 / num2;
            break;
        }
        
        Serial.print(result); // Print result to serial monitor
        num1 = result;
        num2 = 0;
        op = '\0';
        break;
        
      default:
        if (op == '\0') {
          num1 = num1 * 10 + (key - '0');
        } else {
          num2 = num2 * 10 + (key - '0');
        }
        break;
    }
  }
}

In this code, we are using a 4x4 keypad to input numbers and operators. The keys array defines the layout of the keypad, and the rowPins and colPins arrays define the pins used to connect the keypad to the Arduino.

In the setup() function, we initialize the serial communication using the Serial.begin() function.

In the loop() function, we read the keypad input using the getKey() function and store it in the key variable. We then print the key to the serial monitor using the Serial.print() function.

We use a switch statement to perform an action based on the key pressed. If the key is a number, we store it in either num1 or num2 depending on whether an operator has been pressed. If the key is an operator, we store it in the op variable. If the key is the equals sign, we perform the operation based on the operator stored in op, store the result in num1, and print it to the serial monitor. If the key is the clear button, we reset all variables.

Finally, we use the delay() function to

Testing the Calculator

Now that you have finished setting up the circuit and writing the code, it’s time to test your calculator. Connect the Arduino to your computer and upload the code. Then press the pushbuttons to input the numbers and operator. If everything is working correctly, the LED display should show the result of the calculation. Congrats! You have successfully built a calculator.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.