Arithmetic operators in Arduino programming helps in determining the basic logic behind which a code runs. In this tutorial, we will learn what arithmetic operators are and how they can be used for mathematical calculations

Basic Arithmetic Operators

Arduino has the following five arithmetic operators for basic mathematical operations:

OperatorDescription

Example
Assignment Operator (=)It is used to assign the value to the right side of the equal sign to the variable on the left side.To assign the value 5 to B, write B=5.
Addition (+)Adds two operandsSuppose A=4 and B=6; then A+B will give 10.
Subtraction (-)Subtracts second operand from the firstSuppose A=20 and B=10; then A-B will give 10.
multiplication (*)Multiply both operandsSuppose A=5 and B=6; then A*B will give 30.
division (/)Divide numerator by denominatorSuppose A=10 and B=40; then B/A will give 4.
Modulus (%)Calculates the remainder of an integer divisionSuppose A=10 and B=5, then B%A will give 0.5.

Assignment Operator

When we write a maths expression or declare a variable, we use the assignment operator which is denoted by the symbol “=”. It is used to assign a value to a variable. For example, when we declare an integer variable called “a” and set it equal to two, it would look like this:

int a = 2;

Addition

The addition operator, represented by the symbol “+,” is used to add two or more values together. When writing code, numbers are declared first with integer data type. After that use the “+” operator for addition. 

Here is an example:

void setup() {
  Serial.begin(9600);
  int a = 14;
  int b = 3;
  int Sum;
  Sum = a + b;
  Serial.print(Sum);
}

void loop() {

}

Here, two variables are defined, namely a and b and a value is assigned to both of them as well. The third variable, i.e. Sum is defined and no value is assigned to it; hence, it contains a random number. This variable is used to store the sum of a and b, i.e., 17 will be printed to the serial monitor.

Subtraction

The subtraction operator, represented by the symbol “-“, is used to subtract one value from another. It is denoted by the symbol “-“. 

Here’s an example:

void setup() {
  Serial.begin(9600);
  int a = 10;
  int b = 5;
  int Sub = a - b;
  Serial.print(Sub);
}

void loop() {

}

In this example, we have two variables, a and b, with values 10 and 5, respectively. We then use the subtraction operator “-” to subtract a from b. The result, which is 5 (10 – 5), is stored in the variable “Sub” will be printed to the serial monitor.

Multiplication

The multiplication operator is represented by the symbol “*”. It is used to multiply two or more values together, enabling you to perform various mathematical calculations.

Let’s take a look at an example:

void setup() {
  Serial.begin(9600);
  int a = 7;
  int b = 3;
  int Mul = a * b;
  Serial.print(Mul);
}

void loop() {

}

 In this example, we have two variables, a and b, with values 7 and 3, respectively. We then use the subtraction operator “*” to subtract a from b. The result, which is 21 is stored in the variable “Mul” should be printed to the serial monitor.

Division

The division operator is denoted by the symbol “/”. It is used to perform division, dividing one value by another, and obtaining the quotient as the result.

Let’s go through an example:

void setup() {
  Serial.begin(9600);
  int a = 30;
  int b = 5;
  int Div = a / b;
  Serial.print(Div);
}

void loop() {

}

In this example, we have two variables, dividend and divisor, with values 30 and 5, respectively. We then use the division operator “/” to divide dividend by divisor. The result, which is 6 (30 / 5), is stored in the variable ‘Div’ will be printed on the serial monitor.

Now let us take another example:

void setup() {
  Serial.begin(9600);
  int a = 7;
  int b = 2;
  int Div = a / b;
  Serial.print(Div);
}

void loop() {

}

The answer should be 3.5, but if you run the code a number 3 will be printed to the serial monitor. This happens because a, b, Div variables are declared with the integer data type and int only works with whole numbers so 3.5 is rounded down to 3.0.

To get the correct answer, code should be like this: 

We need to declare ‘Div’ as float and either variable ‘a’ or variable ‘b’ should be float type of variable.

If we declare a as a float and add decimals to the whole numbers like this:

void setup() {
  Serial.begin(9600);
  float a = 7;
  int b = 2;
  float Div = a / b;
  Serial.print(Div);
}

void loop() {

}

The correct answer (3.5) should be printed to the serial monitor.

Modulo

In Arduino IDE, the modulo operator is denoted by the symbol “%”. It is also referred to as the remainder operator. The modulo operator calculates the remainder of a division operation between two numbers.

Here is an example:

void setup() {
  Serial.begin(9600);
  int a = 11;
  int b = 5;
  int Rem = a % b;
  Serial.print(Rem);
}

void loop() {

}

Here, we have two variables, a and b, with values 11 and 5, respectively. We then use the modulo operator “%” to calculate the remainder. The result, which is 1, is stored in the variable Rem will be printed to the serial monitor.

Code

The following sketch is for the combined code of the arithmetic operators explained above:

void setup() {
  int a = 3;
  int b = 2;
  int result;
  float result_fl;
  Serial.begin(9600);
  Serial.print("Addition of (a + b): ");
  result = a + b;
  Serial.println(result);

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

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

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

  Serial.print("Float Division (3.0 / 2.0): ");
  result_fl = 3.0 / 2.0;
  Serial.println(result_fl);
  Serial.print("Remainder (10 % 4): ");
  result = 10 % 4;
  Serial.println(result);
}

void loop() {

}

Output:

Addition of (a + b): 5

Subtraction (a – b): 1

Multiplication (a * b): 6

Division (a / b): 1

Float Division (3.0 / 2.0): 1.50

Remainder (10 % 4): 2