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.
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 type, name, and value. Once data is stored inside a variable, it can be used and manipulated in your scripts.
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)
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.
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.
int
. These are declared as int varName = 10;
char exStr = “Jonathan”;
for strings. Strings use double quotes whereas single characters use single quotes.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.const int ledPin = 13;
Const makes the value of the variable ledPin
can be accessed in the program but never changed.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:
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.
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:
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.
Arduino Reference: This is the Arduino Reference page for variables. There is lots of useful content as well as more examples.