Make Arduino Calculate

Math is hard, let's try programming!

We've played around with printing out phrases, but it turns out we can also print out numbers pretty easily too. Try out this sketch on your Arduino:

We've seen that if you use a quoted line of text as argument to println function, it will display the quoted text. If you don't include the quotes your Arduino sees the argument as a variable and the println will look up what the variable contains and print its value instead.


So in this case we would not see in the Serial Monitor, but instead we would see 3.

Note that we're using 2 different serial functions here, the original println and now also print. The print function is just like println except it does not print out a "carriage return" at the end, starting a new line.

  Serial.print("a = ");

So, a = would print in the Serial Monitor and then be followed by 3 on the same line. You can experiment with changing the print's to println's and looking at the Serial Monitor to see how it looks different.

It turns out that the Arduino is smart enough to also do math when asked:

  Serial.println(a + b);

In this case, the Arduino looks at what the argument to println is, and finds it's actually a calculation. It looks up what a is (3) and what b is (10) and then adds them together (+). It then uses that as the value to send to println.

  Serial.print("a * c = ");       // multiply
  Serial.println(a * c);

You may have noticed that for multiplication we used an asterisk instead of an x. Since x is a letter computers would never know when you want the letter and when you intend to multiply so instead we use an askerisk as our multiplication symbol.

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

When we look at the results for b / a in Serial Monitor we see the result isn't very precise. So far we've only been working with integers, which if you recall, are whole numbers. If the variables we are calculating with are integers then our answer will be an integer as well. If there are decimals they will simply be dropped from the result.

We can fix our decimal problem by changing our variable types when we declare them. We can use float instead of int. Floats can have decimals. You may be wondering why we don't use float all the time. We will save that discussion for a little later. For now change the variable declaration portion of your sketch to be:

float a = 3;
float b = 10;
float c = 20;

When you upload and run your sketch you'll notice that everything has decimals now and we can get an actual answer to "b / a".

Pythagorean Theorem

We can use our Arduino as a simple calculator, but let's get a little more sophisticated. Let's calculate the length of the hypotenuse of a right triangle. If you remember from math class, if you have a right-triangle, the hypotenuse c can be calculated from the lengths of the two legs, a and b.

a2 + b2 = c2
c = √(a2 + b2)

Try this code out:

The first thing that's new here is this line at the very beginning of the sketch:

#include "math.h"               // include the Math Library

Which basically says "We'd like to use the math functions, which are in a library that requires us to include the file math.h where the sqrt function lives". This gives us more than sqrt, but we don't need to know that right now.

The second thing that's different here is that when we create the variable c we don't assign it a value.

float c;

It turns out that this is totally OK, it just means that we don't know what c is going to store yet, because we're going to calculate it later. Since it's not assigned to a value upon creation, the Arduino just creates the box, the stuff inside is whatever was in left over in memory.

Default values
If you don't assign a value to a variable, then it will be given a default value of zero.

Later on in the sketch, we assign it the value.

  c = sqrt( a*a + b*b );

In this line, we square a and b and then add them together, then we call the sqrt() function (which does exactly what you may think), to take the square root. Then we assign that to the variable c. Whatever was in c before is lost, replaced by the new value.

The text and code examples above were copied and slightly modified from Arduino tutorials by Limor Fried at under as such, this page is subject to the same license.