Arduino IDE: Variables

Why we need variables?

In the previous topic (Analog Output), you must have noticed that we have used number 3 (pin number) and 500 (delay value) a lot. But what if we have to change the pin number or the delay in improving the code? 

You have to change the value in each of them. This is very tedious and time-consuming. But there is an easy way, using variables.

A variable is used to store a value or an information so that we can refer or/and manipulate it at a later stage during the life of the Arduino sketch. Memory is set aside for storing the variable and the variable is given a name which allows us to access it in the sketch.

So, we have to make two variables for pin number and delay. This is how it looks once you modify the code:

Let us learn more about variables.

Types of variables

Below are some variable types that are frequently used in Arduino sketches:

DatatypeSize in BytesDescription
char1 ByteIt stores 8 bit numerical ASCII value of characters like alphabets, symbols etc. It can also store a signed number that is in range of -128 to 127. Character literals are written in single quotes like 'a', '#' etc and their ASCII numerical is stored at corresponding variable location.
unsigned char1 ByteIt can store 8 bit numerical ASCII values of characters, symbols etc and can also store any unsigned number in range of 0 to 255. Character literals are written in single quotes like 'a', '#' etc and their ASCII numerical is stored at corresponding variable location.
int2 BytesStores a 2 byte(16 bits) signed integer value that is in range of -32,768 to 32,767.
unsigned int2 BytesStores an unsigned integer value of 2 bytes(16 bits) that is in range of 0 to 65,536.
long 4 BytesStores a 4 byte (32 bit) signed integer value that is in range of -2,147,483,648 to 2,147,483,647.
unsigned long 4 BytesStores an unsigned 4 byte(32 bit) integer that is in range of 0 to 4,294,967,295 (2^32 - 1).
float 4 BytesStores a signed 4 byte(32-bit) value that is integer or a value with decimal point (say 12.15) that is in range of -3.4028235E+38 to 3.4028235E+38.
double4 BytesSame as float for evive.
evive Explore
There are other types of variables as well in Arduino. Click here to explore more.

Defining a Variable

The standard form of variable definition is:

Variable_Datatype Varible_Name;

Variable_Datatype can be int or float depending on the type of variable you want. Variable_Name is the name of variable. Variable is referenced or used by its name later in the program.

By giving the variable a type and name, space is made available in memory for this variable.

E.g. if you want to count the number of times the main loop is executed, you must first define a variable count as shown below:

int count;

How to name a Variable

You can give a variable any name as long as it sticks to the rules set out below. It is best to give variables meaningful names that help you and others understand the sketch better.

  1. Variables can consist of both uppercase (A-Z) and lowercase(a-z) letters.
  2. Variables can contain numbers 0 to 9, but cannot start with a number.
  3. Variables may not have the same names as Arduino language keywords, e.g. you cannot have a variable named float.
  4. Variables must have unique names, i.e. you cannot have two variables with the same name.
  5. Variable names are case sensitive, so Count and count are two different variables.
  6. Variables may not contain any special characters, except the underscore (_).

Assigning Value to a Variable

You can assign a value to a variable with the following statement:

Variable_Name = Expression;

Expression will give a valid number that will be assigned to the variable. Let’s count how many times the loop is executed. Below is the Arduino sketch:

Arduino Loop Example

Let’s make a new sketch, where we’ll add two float variable to a new variable. Below is the code:

Arduino Precision

When the value of the variable is sent to the serial monitor window,  println() automatically rounds the number off to two decimal places, i.e. number of digits after the decimal.

The second time that println() is used to send the value of the variable to the serial monitor window, the number of decimal places is specified as 5. This is done by passing a second parameter value of 5 to the println() function.