Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Understanding global and local variables for Arduino

in Arduino Basics

In the previous tutorial, we made our first sketch with an external LED. We declared one constant variable at the top of the program for the output pin. This tutorial will give you insight into how variables are used. We can distinguish two kinds of variables, the global variables, and the local variables. When we create a variable, it is called “declaring.” If we give the variable a name, it is called “naming.” When we assign a value to the variable, we call that “initializing.”

Firstly, a global variable is one that can be seen by every function in the program. Secondly, local variables are only visible to the function in which they are declared. In the Arduino environment, any variable declared outside of a function (void setup(), loop(), void(), etc. ), is a global variable. 

When programs start to get larger and more complex, local variables are useful to ensure that only one function can access its own variables. This prevents programming errors when one function modifies variables used by another function.

Arduino has different types of variables to represent values efficiently. You want to know how to select and use these Arduino data types.

Before they are used, all variables have to be declared. Declaring a variable means defining its type, and optionally, setting an initial value (initializing the variable). Variables do not have to be initialized (assigned a value) when they are declared, but it is often useful to do so.

Declaring and Initializing

Declaring the variable: As stated above, we need to declare a variable and name a variable to make use of variables. Declaring a variable means allocating a place in the Arduino’s memory to store information. You will need to indicate the data type as well as the name of the variable. The name of the variable should be unique in your sketch. The following is a simple declaration of a variable where the data type is int (you can read more about integer variables below) and the name myValue.

int myVariable;

The data type determines the sort of data that can be stored in a variable and what you can do with it. Choosing the right data type is very important since it decides what we can do with the variable. After declaring a variable, must give the variable a unique name.

Naming the variable: One of the most important parts of the variable is the “naming”. Even though you can name your variable whatever you want, it is wise to follow some rules.

Firstly, your variable should be descriptive of what information the variable holds. For example, in the tutorial: 2.1 Blink a LED , we declared an variable called pinLed.

int pinLed = 10;

We could call it “pinWater”. However, that would not make any sense. It is more logical to declare the variable with a name that makes sense. In that way, anyone who reads the variable understands the variable held by the variable.

Notice that the first letter in the second word in “pinLed” is capitalized. This is not necessary, but it will be easier to distinguish the two words.

Initializing the variable: As mentioned in earlier lessons, we need to have a semicolon at the end of every statement. Once we have declared the variable, it can hold information. To put a value to the variable, we are going to use the Assignment Operator. This is just an equal sign that gives our variable to value given after the equal sign.

As mentioned before, when we are declaring a variable, we need the data type followed by the name.  Then, at the end of that statement, we want a semicolon.

Initializing a variable: The first time we assign a value to the variable, it is called initializing the variable.  In the above example, we declared and initialized the variable in the same line of code: int pinLed = 10;

Since pinLed  is a global variable, we can change the value later in the program. The only thing we need to do is to use the variable name and assignment operator again.

In other words, type in the variable name, use the equal sign, and add value. Remember to end the statement with a semicolon. In the example below, this is added. If you would run this program and look at the serial monitor, you will notice that the first time it will print the value 5, and if the sketch reaches the loop part, it will contain the value 10.

Global and Local variables: In Arduino, if a variable is declared at the top of the program, before the void setup, all parts of the program can use that variable. Hence, it is called a Global variable. On the other hand, if the variable is declared between a set of curly brackets, it is only recognized within that scope. It will only be recognized and can only be used between that set of curly brackets.

For example, if a variable is declared in the void setup(), it will not be recognized and can not be used in the void loop(), because the void loop is within its own set of curly brackets.

Since pinLed  is a global variable, we can change the value later in the program. The only thing we need to do is to use the variable name and assignment operator again.

In other words, type in the variable name, use the equal sign, and add value. Remember to end the statement with a semicolon. In the example below, this is added. If you would run this program and look at the serial monitor, you will notice that the first time it will print the value 5, and if the sketch reaches the loop part, it will contain the value 10.

Data Types

there are many data types available to Arduino. Let us start with the numeric types that either uses whole numbers or floating-point numbers. There are two types of numeric datatypes, signed and unsigned. Unsigned data types will always contain positive values where signed data types can hold negative values. By default, most data types are signed.

The integer data type is the primary data type for storing numbers: it can store numeric values between -32,768 and 32,767. The values should be whole numbers. An integer variable cannot store decimal points. You can declare an integer variable by using the int keyword.

In contrast to the integer data type, floating-point numbers can contain a number with a decimal point. To declare a floating variable, you can use the float keyword. The following are two examples of float declarations:

float pi = 3.14159; 
float circle = 360.0;

Byte is also a data type that can store values between 0 – 255. Unlike integers and floats, bytes are unsigned. Therefore when a byte exceeds 255, it rolls over to 0.

Another data type is the boolean data type. You can use this to store two values – false or true.

Table 1 shows some of the various data types available to the Arduino platform, some of which we will cover in later tutorials.

Numeric TypeBytesRangeUsage
int2-32768 to 32767For positive and negative integer values
unsigned int20 to 65535for only positive numbers
long4–2147483648 to 2147483647 For a very large range of positive and negative values
unsigned long44294967295For a very large range of positive values
float43.4028235E+38 to –
3.4028235E+38
for numbers with fractions.
boolean1 false (0) or true (1)represent true and false values.
char1-128 to 127for a single character.
byte10 to 255simular to char, but for unsigned values.

Constant variables:

As discussed above, variables behave on the properties of their data type. The constant/const variable makes the value of a variable read-only. Once a value is assigned to a constant variable, the variable cannot change in the sketch.

Int pinLED = 10; // the variable can be changed in the sketch

Const pinLED = 10; // the variable cannot be changed in the sketch

The keyword const is used at the beginning of the variable declaration of define a variable as a constant value.

This can be useful in a couple of situations. One of them is that a compiler error is caused when we want to assign a new value to the constant variable.

It is also useful to assign a constant variable to reduce the amount of used memory on the board.

We now move on to a  lesson that gives us insight into using the variables in a sketch with operators.

Constant Variables

As discussed above, variables behave on the properties of their data type. The constant/const variable makes the value of a variable read-only. Once a value is assigned to a constant variable, the variable cannot change in the sketch.

Int pinLED = 10; // the variable can be changed in the sketch

Const pinLED = 10; // the variable cannot be changed in the sketch

The keyword const is used at the beginning of the variable declaration of define a variable as a constant value.

This can be useful in a couple of situations. One of them is that a compiler error is caused when we want to assign a new value to the constant variable.

It is also useful to assign a constant variable to reduce the amount of used memory on the board.

We now move on to a  lesson that gives us insight into using the variables in a sketch with operators.

Previous Post
Blink a LED with Arduino
Next Post
Understanding strings in Arduino
You must be logged in to post a comment.
Menu