Arduino Programming: Variables

Objectives and Overview

This lesson introduces variables, a core concept of programming. Included are explanations of different types of variables, how to declare them, and why they’re useful.

Lesson Objectives

  • Understand the purpose of variables in programming and explain the advantages of using them.
  • Explain the concept of scope and identify the differences between local and global scope.

What are Variables?

Variables are one of the cornerstones of programming and development. At the most basic level, variables are containers. On their own, a variable is just an empty placeholder waiting to store a value. Variables have a typename, and value. Once data is stored inside a variable, it can be used and manipulated in your scripts.

Declaring Variables

Variables must be declared before they’re used. When coding for the Arduino, variable declarations must include the name and type. Setting the initial value is optional. Here are two examples, both of which are correct:

int ledPin1; // this declaration includes the name (ledPin1) and the type (int) int ledPin2 = 12; // this declaration also includes the initial value
Code language: JavaScript (javascript)

Variable Types

The above examples specify the variable type. Understanding variable types is an important aspect of programming, especially when coding for Arduino because you must correctly specify the type when declaring the variable. This isn’t the case in other languages such as JavaScript, so if you’re coming from a background with “weakly typed” variables you’ll need to make sure that you’re properly specifying the variable types when working with Arduino.

Variable Type Examples

Here are some of the most common variable types you’ll encounter. There are more types, but they’re rarer. You’ll encounter these types in most Arduino code:

Note: The initial value is optional, whereas the type and name are required.

  1. Integers (int): Integers are primarily what you’ll encounter for number storage. These are only integers with no decimal values that are between -32,768 and 32,768. The above examples are type int. These are declared as int varName = 10;
  2. Characters (char): Characters are either single or multiple characters (strings). These are declared like this: char exChar = ‘J’; for single characters and char exStr = “Jonathan”; for strings. Strings use double quotes whereas single characters use single quotes.
  3. Floating Points (float): Floating points are numbers that have a decimal point. These variables are common in Arduino projects. These are declared like this: float zVar = 2.0; When doing any math operations on floating points, make sure to include the decimal such as 2.0 instead of 2. If you don’t include the decimal, it will be interpreted as an integer instead of a float.
  4. Long (long): These are long values that don’t include decimals. These are the values between -2,147,483,648 and 2,147,483,647.
  5. Booleans (bool): These are Boolean values (true or false).
  6. Constants (const): This isn’t a variable type. It’s a qualifier that changes the behavior of a variable so that it can be read/accessed throughout the code but is unable to be modified. It remains constant throughout. Here’s an example: const int ledPin = 13; Const makes the value of the variable ledPin can be accessed in the program but never changed.

Arduino Variable Example

Now that you have an overview of variables, let’s take a look at an example. In the “Blink LED” project the LED is in pin 13 in the Arduino, and the Arduino code corresponds to this with a pinMode value of 13.

However, you’ve likely noticed that when you wanted to change the LED to pin 12 in the Arduino, you’ve also needed to change it in several spots in the code. You’d need to update pinMode and digitalWrite  with the new pin number. This isn’t too difficult when you don’t have many lines of code, but it becomes overwhelming when working in larger projects.

When you find yourself needing to do the same task repeatedly while programming, you should ask yourself if there is a more effective method. In this particular situation, using variables is the optimal method!

Let’s get started!

You’re going to create a variable that stores the value of the intended ledPin: int ledPin = 12;

The variable in this example has an initial value of 12. Note that if you were to change the pin that the LED is in you’ll need to change this variable.

Let’s take a look at this updated version of the Blink code that now uses variables:

Arduino IDE with the "Blink" sketch using variables and annotations pointing out when the variable is created and used

Notice that our variable ledPin is being passed as a parameter for pinMode and digitalWrite. Since variables are containers, the variable named ledPin is currently storing an the number 12, an integer. When you want to change the pin that the LED is in on the Arduino you only need to update the Arduino code in one place: the variable declaration at the top of the code.

Variable Scope

The Arduino programming language is based on the Processing language, which itself is based on C. Variables in C have a property called scope, and therefore variables in Arduino also have this property. The two scopes that variables have are global and local:

Global variables can be seen and used by every function in your Arduino code.

Local variables are only seen by the function that they’re declared in.

Here’s a non-programming parallel:

Your full given birth name is on your birth certificate and is seen/accessed by anyone in any of your circles, as long as you reveal it. This is your “global name.” A nickname that is known to only your inner circle of friends and only applied to you when in their company is your “local name.” There is no record of it anywhere else other than that particular local circle, so if someone called you by this particular nickname in an official capacity, it wouldn’t be recognized.

Remember that void setup() and void loop() are both functions. This means that any variables declared within those functions will be local to it. Any global variables must be declared outside of these functions.

Here’s an example:

Arduino IDE showing the "Blink" sketch with variables and an annotation pointing out the variable's scope

Notice that int ledPin = 12; is declared before the setup function begins. This means that ledPin is a global variable that can be seen/accessed by all functions in the program. A quick way to tell if a variable is global is to check to see that it is declared outside of a function.

The ledPin variable is accessed in  void loop() later in the example. If ledPin is declared within the void setup() function, it wouldn’t have been accessible by the methods in the loop. If it had been declared within void loop(), it wouldn’t be accessible during void setup() or anything else.

Test It! Go ahead and move the variable declaration into the void setup() function and test your code. You’ll get an undefined error.

Additional Resources

Arduino Reference: This is the Arduino Reference page for variables. There is lots of useful content as well as more examples.