Arduino Mega – Making a Calculator (Serial Port)

In this tutorial, I am going to show you how to make a calculator using Arduino Mega 2560 but you can use any Arduino board. This calculator uses only UART module, to communicate with the computer. You don´t need any extra parts.


After opening the serial monitor, you should ensure that the 9600 and “newline” are selected, in the options in the bottom bar, as you can see in image below:


As you can see in the code below, there is a function called “serialEvent ()”, this function is recognized by the native libraries of Arduino and therefore the name of this function can not be changed, otherwise, it will not be recognized and the system will not work.

Copy the code below to your Arduino IDE project and save it. Then open the serial monitor of IDE or any other and insert the numbers and operator.

//variable declaration
int value1;
int value2;
char operation = "";
float result = 0;

int aux = 0;

String inputString = ""; // a String to hold incoming data
boolean nextInsertion = false;

void setup() {
  // init Serial Port with baudrate = 9600
  // reserve 100 bytes for the inputString:
  Serial.println("Enter the first number!");

void loop() {
  if (nextInsertion && aux == 0){
    nextInsertion = false;
    aux++; // increment var
    value1 = inputString.toInt();
    inputString = ""; // clear inputString
    Serial.print("value1: ");
    Serial.println("Enter the operator (+, -, / or *)!");
  if (nextInsertion && aux == 1){
    nextInsertion = false;
    operation = inputString[0]; //only first char
    inputString = "";    
    Serial.print("operation: ");
    Serial.println("Enter the second number!");
  if (nextInsertion && aux == 2){
    nextInsertion = false;
    value2 = inputString.toInt();
    inputString = "";
    Serial.print("value2: ");

  if (aux == 3){
    /* compute */
    aux = 0;
    switch( operation )
      case '*':
        result = value1 * value2;
      case '/':
        result = value1 / value2;
      case '+':
        result = value1 + value2;
      case '%':
        result = value1 % value2;
      case '-':
        result = value1 - value2;
        Serial.println( "Erro! Operacao indefinida!\r" );
  Serial.print("result: ");

SerialEvent occurs whenever a new data comes in the hardware serial Port. This
routine is independent of the loop(), but using delay inside loop can
delay response.
void serialEvent()
  while( Serial.available() ) {
    // get a new byte:
    char inputByte = (char);
    // add byte to the String
    inputString += inputByte;
    if( inputByte == '\n' || inputByte == '\r' )
    { //next insertion
      nextInsertion = true;

1 thought on “Arduino Mega – Making a Calculator (Serial Port)

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!

Hello! We have noticed you are using an ad blocker. Our website is funded by advertising which allows you to access all our content for free. By disabling your ad blocker, you are contributing to the sustainability of our project and ensuring we continue to provide high-quality, useful tutorials. We appreciate your support!