One of the fundamental aspects of programming in Arduino IDE is the use of variables. Variables are essential building blocks that store data. In this tutorial you will learn about variables, different data types that variables can store, defining and naming variables, and assign values to them.
What is Variable?
Variables in Arduino Programming Language are containers that can hold different types of data, such as numbers, characters, or even custom data types. These data types include int (integer), float (floating-point number), char (character), and more. Using variables allows you to store values temporarily or permanently during the execution of your Arduino program.
Types of Variables
Arduino supports various data types that allow you to work with different kinds of data efficiently.
Below is a table outlining the common types of variables in Arduino and their characteristics.
Data Type | Size in Bytes | Description | Example |
---|---|---|---|
int | 2 Bytes | It is short for integer and is used to store whole numbers. It stores a 2 byte(16 bits) signed integer value that is in the range of -32,768 to 32,767. | int sensorValue = 10; |
float | 4 Bytes | Used for storing a signed 4 byte(32-bit) value that is integer or a value with decimal point (say 11.16) that is in range of -3.4028235E+38 to 3.4028235E+38. | float temperature = 25.5; |
char | 1 Byte | It stores 8 bit numerical ASCII value of characters like alphabets, symbols etc. It can also store a signed number that is in the range of -128 to 127. Character literals are written in single quotes like 'a', '#' etc and their ASCII numerical is stored at the corresponding variable location. | char firstInitial = 'A'; |
String | 6 Bytes | Used for handling sequences of characters | String name = "John"; |
bool | 1 Byte | Each bool variable occupies one byte in memory and it can have two values: true or false. | bool isOn = true; |
byte | 1 Byte | Byte stores an 8-bit numerical value without decimal points in the range of 0 - 255. | byte data = 150; |
long | 4 Bytes | Used for storing signed integer values that are in range of -2,147,483,648 to 2,147,483,647. | long largeNumber = 1000; |
unsigned int | 2 Bytes | The unsigned int type stores a value up to two bytes or 16 bits. It stores positive values only. The range of the unsigned int data type is 0 to 65,535. | unsigned int positiveNumber = 50; |
unsigned long | 4 Bytes | Used for storing larger positive whole numbers that are in range of 0 to 4,294,967,295. | unsigned long largePositiveNumber = 10000; |
double | 4 Bytes | Used for storing double-precision | double pi = 3.14159265359; |
How to Name a Variable
You can give a variable any name as long as it sticks to the following rules. It is best practice to give variables meaningful names that help you and others understand the program better.
- Variables can contain numbers 0 to 9, but cannot start with a number.
- Variables can consist of both uppercase (A-Z) and lowercase (a-z) letters, e.g. 12var is not allowed, but var12 is allowed.
- Variables cannot have the same names as Arduino language keywords, e.g. you cannot have a variable named char.
- Variable names are case-sensitive, so Pin and pin are two different variables.
- Variables must have unique names, i.e. you cannot have two variables with the same name.
- Variables may not contain any special characters, except the underscore (_).
Declaring a Variable
Variables have to be declared before they can be used. Declaring a variable means defining its type, setting a specified name, and optionally assigning an initial value. For example, to declare an int variable called inputVariable, we would use the following:
int inputVariable = 7;
‘int’ is written first, followed by the name of the variable. Then the variable is set equal to a number, variable, or function. In this case, inputVariable is set equal to the number seven. Finally, we end all variable declarations with a semicolon (;).
This only needs to be done once in a program but the value can be changed at any time using arithmetic and various assignments.
Variable Scope
A variable can be declared at the beginning of the program before void setup(), locally inside of functions, and sometimes within a statement block such as for loops. Where the variable is declared determines the variable scope, or the ability of certain parts of a program to make use of the variable.
Global Variables
A global variable is one that can be used by every function and statement in a program. This variable is declared at the beginning of the program, before the setup() function.
For example,
// Global variable
int ledPin = 13; // The pin number where the LED is connected
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
// Turn the LED on
digitalWrite(ledPin, HIGH);
// Delay for 1 second
delay(1000);
// Turn the LED off
digitalWrite(ledPin, LOW);
// Delay for 1 second
delay(1000);
}
We can notice that the ledPin is used both in the loop() and setup() functions.
The value is used in both functions, so, changing the value in one function will reflect in the other.
Local Variables
A local variable is defined within a specific function or block of code. They have a limited scope, meaning they are only accessible within the function or block in which they are defined. It is therefore possible to have two or more variables of the same name in different functions of the same program that contain different values.
For example,
void setup()
{
Serial.begin(9600);
}
void loop() {
// This is a local variable 'count' that is only accessible within the 'loop()' function
int count = 0;
// Increment the 'count' variable by 1
count++;
// Print the value of 'count' to the Serial Monitor
Serial.print("Count value: ");
Serial.println(count);
// Wait for a second
delay(1000);
}
In this example, the variable count is a local variable within the loop() function. It gets declared and initialized to 0 each time the loop() function is called. Since count is local, it is independent of any other variable with the same name that may exist in other parts of the program.