Arduino Calculator Circuit

1,558 views

Introduction

The Arduino Calculator program is a simple and effective way to use an Arduino microcontroller for performing basic arithmetic operations. This program allows users to input numerical values and arithmetic operations through a keypad or set of buttons and calculates the result using the math.h library. The result can be displayed on an output device such as an LCD display or the Serial Monitor. With the flexibility and versatility of the Arduino platform, users can easily customize the program to suit their specific requirements and even extend it to perform more advanced calculations.

This program is an excellent example of the practical applications of Arduino microcontrollers and is widely used in educational, scientific, and engineering settings to demonstrate the power of this technology.

Arduino Calculator Code

  1. Include the necessary library for mathematical operations. In this case, we need math.h library.
#include <math.h>
  1. Declare the necessary variables for the mathematical operations. In this case, we need two floating-point variables a and b to store the values of 500 and 105.33, respectively. We also need four more floating-point variables add, sub, mul, and divide to store the results of the mathematical operations.
float a = 500;
float b = 105.33;
float add;
float sub;
float mul;
float divide;
  1. In the setup() function, initialize the serial communication with a baud rate of 9600. Then, print a message to the serial monitor to indicate that the program is a simple Arduino calculator. Print the initial values of a and b to the serial monitor.
void setup()
{
  Serial.begin(9600);
  Serial.println("Simple Arduino Calculator:");
  Serial.println("\n");
  Serial.print("a = ");
  Serial.println(a);
  Serial.print("b = ");
  Serial.println(b);
  Serial.println("\n");
}
  1. Calculate the result of addition of a and b and store it in the add variable. Print the result to the serial monitor.
add = a + b;
Serial.print("Addition: ");
Serial.print("a + b = ");
Serial.println(add);
  1. Calculate the result of multiplication of a and b and store it in the mul variable. Print the result to the serial monitor.
mul = a * b;
Serial.print("Multiplication: ");
Serial.print("a * b = ");
Serial.println(mul);
  1. Calculate the result of division of a by b and store it in the divide variable. Print the result to the serial monitor.
divide = a / b;
Serial.print("Division: ");
Serial.print("a / b = ");
Serial.println(divide);
  1. Calculate the result of subtraction of b from a and store it in the sub variable. Print the result to the serial monitor.
sub = a - b;
Serial.print("Subtraction: ");
Serial.print("a - b = ");
Serial.println(sub);
  1. In the loop() function, do nothing because we don’t need to perform any action continuously.
void loop()
{
  // do nothing
}

Full Code

Now copy the following code and upload it to Arduino IDE Software.

#include <math.h>

float a = 500;
float b = 105.33;
float add;
float sub;
float mul;
float divide;

void setup()
{
  Serial.begin(9600);
  Serial.println("Simple Arduino Calculator:");
  Serial.println("\n");
  Serial.print("a = ");
  Serial.println(a);
  Serial.print("b = ");
  Serial.println(b);
  Serial.println("\n");

  add = a + b;
  Serial.print("Addition: ");
  Serial.print("a + b = ");
  Serial.println(add);

  mul = a * b;
  Serial.print("Multiplication: ");
  Serial.print("a * b = ");
  Serial.println(mul);

  divide = a / b;
  Serial.print("Division: ");
  Serial.print("a / b = ");
  Serial.println(divide);

  sub = a - b;
  Serial.print("Subtraction: ");
  Serial.print("a - b = ");
  Serial.println(sub);
}

void loop()
{
  // do nothing
}

Working Explanation

The program initializes Serial communication and prints the initial values of a and b to the Serial Monitor. The program performs four arithmetic operations (addition, subtraction, multiplication, and division) on a and b and prints the results to the Serial Monitor.

The loop() function is empty as we do not need to perform any continuous action in this program. When the program is uploaded to the Arduino board and run, the results of the arithmetic operations are printed to the Serial Monitor for debugging and testing purposes.

Applications

  • Education
  • Engineering
  • Science
  • Technology

Conclusion

We hope you have found this Arduino Calculator Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.