In this article, we will learn how perform math calculations in an Arduino program. We will look at the basic mathematical operators first, followed by a review of the order of operations. Then we will look at how to perform more advanced mathematical operations like trigonometry, squares, square roots, powers, and min/max.

Basic Mathematical Operators

The basic mathematical operators are the plus sign (+) for addition, the minus sign (-) for subraction, an asterisk (*) for multiplication, and a forward slash (/) for division.

When we write a math expression or declare a variable, we use what’s called an assignment operator. The assignment operator is the equals sign (=). The assignment operator is used to assign a value to a variable. For example, when we declare an integer variable called x and set it equal to one, like this:

int x = 1;

This tells the program to store the integer 1 in the variable x.

To perform addition, we use the addition operator (+):

int x = 2 + 1;

This will store the result of 2 + 1 (three), in the x variable.

Subtraction is performed with the subtraction operator (-):

int x = 2 - 1;

This will store the result of 2 - 1 (one) in the x variable.

Multiplication is performed with the multiplication operator (*):

int x = 2 * 4;

This will store the result of 2 * 4 (eight) in the x variable.

Division is performed with the division operator (/):

int x = 4 / 2;

This will store the result of 4 / 2 (two) in the x variable.

We can also do math with variables. For example, instead of using whole numbers in the examples above, we could use the variables a and b:

int x = a / b;

Now that we’re talking about division, there is an important quirk to be aware of when dividing numbers. Take a look at the code below, where we divide five by two:

void setup() {
  Serial.begin(9600);

  int x = 5 / 2;
  Serial.print(x);
}

The answer should be 2.5, but if you run the code a number two will be printed to the serial monitor. This happens because x was declared with the integer data type. Int only works with whole numbers so 2.5 is rounded down to 2.0.

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

float x = 5.0 / 2.0;

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

Order of Operations

When you use expressions with more than one operator like this:

int x = 5 + 6 * 2 - 1 / 3;

The order of operations becomes important because the result will come out differently depending on which operations are performed first. For example, if you add 5 and 6 first, the result is 21.7. But if you multiply 6 and 2 first, the result becomes 16.7.

So what is the correct order? It’s PEMDAS:

  1. Parentheses
  2. Exponents
  3. Multiplication
  4. Division
  5. Addition
  6. Subtraction

Operations inside parentheses are executed first. Then exponents. Then multiplication and division. Then addition and subtraction.

Since math inside parentheses is performed first, they can be used to control the order of operations.

Trigonometry Functions

The Arduino can also do more advanced math like trigonometry. The Arduino has built in functions for calculating the cosine, sine, and tangent of an angle. The syntax for each function looks like this:

float a = cos(b);
float a = sin(b);
float a = tan(b);

The trigonometry functions take a float value in radians and return a float value in radians.

The Square Function

The Arduino also has a function for calculating the square of a number. This calculates y squared (y2):

float x = sq(y);

The Square Root Function

To calculate the square root of a number, use the square root function. This calculates the square root of y (√y):

float x = sqrt(y);

The Power Function

The power function calculates the value of a base raised to the power of an exponent (yx). The syntax looks like this:

int x = pow(base, exponent);

For example, to find the result of 108, put 10 as the first argument and 8 as the second argument:

int x = pow(10, 8);

The Min and Max Functions

The Arduino also has functions that find the minimum value and maximum value from a pair of numbers.

The min() function calculates the minimum value of any two numbers:

int x = min(a, b);

The min() function returns the smaller of the two values. The variables a and b can be any data type.

The max() function calculates the maximum value of any two numbers:

int x = max(a, b);

The max() function returns the larger of the two values. The variables a and b can be any data type.

Min and max are useful for keeping values above or below a certain threshold. For example, say you want to make sure the reading from a temperature sensor never exceeds 100 degrees. You could use the min() function like this:

sensVal = min(temp, 100);

This will store the smaller of the two numbers in the variable sensVal. Even if temp exceeds 100, the min() function will still output 100.

The Modulus Operator

The Arduino has another operator that deals with division called modulus. The modulus operator is written with the percent sign:

int x = a % b;

The modulus operator performs division on an integer then returns the remainder. Say for example that a = 5 and b = 2. Two divides into five twice and we’re left with a remainder of one. So 5 % 2 would equal one.

The Absolute Value Function

Another useful Arduino function is the absolute value function. The absolute value function returns the positive value of a negative number. For example, |-x| = x. The abs() function below will return the absolute value of -10, which is 10:

int x = abs(-10);

More Advanced Math

There’s actually quite a bit more advanced math the Arduino is capable of. These advanced functions are available in a library called the math library. Visit this link for more information about the functions it has and how to install it.

That about covers the main mathematical operators used in Arduino programming. Feel free to leave a comment if you have a question or something to add!