Variables in Arduino programming (Simplified Tutorial)

So far in our tutorials, we have reasonably covered topics that introduced us to learning Arduino, now; we are delving deeper into the learning.

In this tutorial; Variables in Arduino programming , we shall learn the following:

  • Variable as a value container
  • The nature of variables
  • Variable data types
  • Variable declaration and initialization
  • Variable naming identification restrictions
  • Variable naming conventions “CamelCase”
  • Variable data types definitions
  • Variable scope

VARIABLE AS VALUE-CONTAINER

In English language, a variable refers to something that can change, at the other hand; a constant is something that does not change. In Arduino programming, a variable is a “named-value container” where values can be stored. These values may vary over time which is why it is called a variable, but the name of the entity that contains the variable does not change.

At the other hand, we can see the variable as a bucket; water as an item can be pured into the bucket and removed from the bucket. Also, this bucket has the capacity to contain different types of items, one can decide to fill stones into the bucket, or anything one feels like putting into the bucket, as long as the bucket has the capacity to contain what is in it.

Variables are used to carry data from one part of the program to another part of the program. The contents of a variable can change; however, the identity or name of the variable remains the same in programming.

A variable as a material container
Figure 1: A variable as a material container

THE NATURE OF VARIABLES

A variable should have a name and be of a data type. When the variable is just an integer variable, it means that the value that will be held in the variable is an integer.

We can have other data types like: float, char, double, Boolean, etc. We shall discuss them in the future, but just know that a variable holds or saves data of a certain type. For example let’s say we have designed and programmed a circuit to measure the distance of a robotic car as it approaches an obstacle, you may decide to integrate a display to show the distance the car is from the obstacle, one would observe that the values displayed vary as the robotic car moves closer or away from the obstacle.

Whosoever that programmed the robotic car must have created a memory space in the microcontroller as a variable to hold the data that is being measured, since these data will have to vary as the robotic car moves, they will be computed and recorded as variables. This data that is recorded have be recorded as a specific data type. If the data type that was programmed is an integer, then the result that will be displayed on the screen will be integer values, but if it was programmed to display float or double data type, the result will display decimal point in the result as well.

VARIABLE DATA TYPES

While programming arduino, you have to specify the data type of any variable you use in the program, and whenever you wish to change the content of the variable, you must make sure you stick to the specific data type you declared for the variable at the beginning. In other to understand the concept of data types very well let’s look at the figure below.

Various containers symbolizing various variable data types
Figure 2: Various containers symbolizing various variable data types

As stated earlier, there are various data types, they include integer, char, string, Boolean, float, double, etc. the figure above gives an intuitive understanding of what data types look like. We have various containers of various shapes so designed to contain items of specific features, the first figure is a cuboid shaped container so designed to contain cuboidal items. If this container (variable) is an integer, it will only accept integer data (cuboids), in our analogy, the container is designed to specifically store cuboid shapes, putting any other type of data into this unique integer variable will be erroneous, same thing is applicable to the other containers.

VARIABLE DECLARATION AND INITIALISATION

To declare a variable in arduino programming requires just two simple steps.

Step 1: State the variable data type

Step 2: State the variable name

See image below.

Simple variable declarations in arduino programming
Figure 3: Simple variable declarations in arduino programming

In the figure above, code lines 1 to 6 are variable declaration code lines, you can see that the data types appear in a different colour, while the variable names appear in another colour. The arduino IDE was designed to display data types with such colour, to differentiate them from other words used in writing the program. You can pass data to the variable immediately after declaring the variable, this process of passing data to a variable immediately it is declared is called variable initialization, see figure 4 below.

Data passed to variables
Figure 4: Data passed to variables

VARIABLE NAMING IDENTIFICATION RESTRICTIONS

You can name a variable arbitrarily; however, the following restrictions apply.

  1. A variable name cannot have space(s) or special characters in them; however, you can use underscore. E.g. “my_Num” can serve as a variable name, but not my#name.
  2. You can have a number in the name of a variable, e.g. my8num, but a number cannot start the name of a variable, e.g. 9myNum is unacceptable, and you cannot have just a number for the name of a variable.
  3. A variable name cannot be the name of an arduino keyword, e.g. void, int, delay, loop, etc.

VARIABLE NAMING CONVENTIONS CAMELCASE

The variable name should be descriptive and self-explanatory; it should be a word that another person who reads the sketch would understand its meaning. It should be a word that you can still remember its use even after a year has passed.

For example, if you wish to name a digital pin (which can be stored in a variable holder) where you plugged LED in your arduino board, you can use any word you like, as long as it does not violate the exceptional rules listed above, however, choosing a name like “cool” as a digital pin variable name is not a problem, but, if you check back the sketch after a year, can you easily remember that the variable“cool” in your sketch refers to a digital pin where you plugged LED in your design??? I doubt you easily can!

On the other hand if you named the variable “ledPin” even after 10 years, you can still remember that it refers to a pin where an LED is connected. Therefore, using descriptive variable identifiers is very necessary in variable declaration.

NB: variable identifier means variable name

Also, adding comments to a variable to explain its meaning is an added bonus. You can check the tutorial on how to add comments to arduino sketch.

There is a convention used in naming variables in programming, it is called CamelCase (also “camel case” or “dromedary case”). It is a naming convention in which the first letter of each word in a compound word is capitalized. Examples include ledPin, pushButton, buttonState, switchPin, etc. this makes the name easily readable.

When we specify data type, we are telling the compiler to set aside a certain amount of memory to contain the data we are sending to it, so, if along the line you ask the compiler to hold a data that is not meant for the data type it can hold, that becomes a problem, that is why we have to strictly maintain format when changing variable contents, we have to do that with respect to the data type of the variable.

VARIABLE DATA TYPES DEFINITIONS

The Table below shows various data types and their definitions

NAME BRIEF DEFINITION
voidUsed only in function declarations- it means returns nothing
booleanHolds either 1 or 0, true or false, LOW or HIGH
char-128 to 127 and character literals
unsigned char0-225
byte0-225
int-32,768 to 32,768
unsigned int65,555
word65,555
long-2,147,483,648 to 2,147,483,647
unsigned long0 to 4,294,967,295
float-3.4028235E+38to3.4028235E+38
double-3.4028235E+38to3.4028235E+38
stringArrayofcharacters-defined as char strName[5];
StringAStringObject
Variable declaration sample and explanation
Figure 5: Variable declaration sample and explanation

VARIABLE SCOPE

Arduino programming uses C++ programming language, and C++ has a property called variable scope. There are two types of variable scopes they are:

  • Global variable
  • Local variable

WHAT IS A GLOBAL VARIABLE?

A global variable is simple a variable that can be seen by every function in the program.

WHAT IS A LOCAL VARIABLE?

Local variables at the other hand are only visible to the functions in which they are declared. In arduino development environment, any variable declared outside of a function like setup(), loop(), etc. is a global variable.

When programs start to get larger and complex, local variables are a useful tool to ensure that only one function has access to its own variable. This helps to minimize program error when one function unknowingly modifies variables used by another function.

Global and local variables
Figure 6: Global and local variables

Code lines 1 to 6 are global variables, while code lines 15 and 16 are local variables.

Having read and understood this tutorial now open some of the sketch examples in the arduino IDE and check how variables are used. Check out for the next tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *