Introduction to Variables
Variables in Arduino programming store and manage data. For instance, they hold values like sensor readings or user inputs. As a result, they are key to controlling hardware components effectively.
Declaring Variables
Typically, you declare a variable at the beginning of your sketch before the setup()
and loop()
functions. For example:

Variable Data Types
Number Variables
These variables store numbers for counting, measuring, or math in Arduino projects. They’re great for sensors and calculations.
int
First, an int stores whole numbers. For example, use it to count button presses. Therefore, it’s perfect for tracking values like 1 or -10. However, it can’t hold decimals. Additionally, its range is -32,768 to 32,767. So, choose it for small counts.
unsigned int
Next, an unsigned int stores positive whole numbers. For instance, it tracks time in milliseconds. So, it allows bigger numbers. That said, it skips negative numbers. In fact, its range is 0 to 65,535. Thus, use it for positive counts.
long
A long holds big whole numbers. For example, count up to 1,000,000. Hence, it’s great for large values. However, it uses more memory. Moreover, its range is -2,147,483,648 to 2,147,483,647. So, pick it for huge numbers.
unsigned long
Furthermore, an unsigned long stores big positive numbers. For instance, use it for long timers. Consequently, it’s ideal for time tracking. However, it can’t store negatives. In fact, its range is 0 to 4,294,967,295. Thus, use it for long durations.
byte
Additionally, a byte stores small numbers from 0 to 255. For example, set LED brightness. Therefore, it saves memory. Even so, it can’t hold big numbers. Also, its range is 0 to 255. So, use it for simple tasks.
float
A float stores decimals, like 3.14. For example, track temperature, like 23.5°C. So, it’s great for precision. Even so, it uses more memory. In fact, it supports 6-7 decimal places. Thus, choose it for decimals.
double
Lastly, a double is like float but sometimes more precise. For instance, use it for exact math. As a result, it’s good for complex calculations. In contrast, it’s often the same as float on Arduino. What’s more, it has the same range. So, use it carefully.
Text and Logic Variables
These variables store text, letters, or true/false values. They’re perfect for displays or making choices in Arduino code.
char
First, a char stores one letter, like ‘A’. For example, show letters on a display. Therefore, it’s great for text. Even so, it holds only one character. Furthermore, its range is -128 to 127 or single letters. So, use it for single characters.
unsigned char
Next, an unsigned char stores positive numbers or characters. For instance, use it like a byte. So, it’s simple and small. However, it can’t store negatives. In fact, its range is 0 to 255. Thus, use it for small data.
boolean
Also, a boolean stores true or false. For example, check if a button is pressed. Hence, it’s perfect for yes/no checks. Though, it only has two values. In fact, its range is true or false. So, use it for simple decisions.
String
Furthermore, a String stores text, like “Hello”. For example, show messages on a screen. Thus, it’s easy for words. Nevertheless, it uses more memory. Also, it holds any text, limited by memory. Thus, use it for messages.
array
Finally, an array is a list of values, like numbers or letters. For instance, store sensor data like {10, 20}. Therefore, it organizes multiple values. However, you must set its size. In fact, its range depends on the type, like int. So, use it for lists.
Scope
A variable’s scope defines where it works in your code. For example, local variables are declared inside a function or block. As a result, they only work within that function or block. In contrast, a global variable is declared outside any function. Therefore, they can be used throughout your entire Arduino sketch.
Constant Variables
In addition, Arduino also supports constants, which are values that don’t change during the execution of the program. Constants are declared using the const
keyword. For example:

Manipulating Variables
Also, you can perform various operations on a variable, such as addition, subtraction, multiplication, and division. However, the operation depends on the data type. For example:

Type Casting
To begin, type casting changes the data type of a variable. For instance, you can convert a float to an int. Similarly, you can cast an int to a float. As a result, this helps perform specific Arduino calculations.

Variable Naming Convention
To start, variable names in Arduino must follow specific rules. For example, they should begin with a letter, not a number. Additionally, you can use letters, digits, or underscores after the first letter. In contrast, names are case-sensitive, so myVar
and myvar
are different variables. As a result, clear names like ledPin
or sensorValue
make your code easier to read.
Memory Usage
Finally, Arduino boards have limited memory for variables. For example, RAM and Flash store your program and data. As a result, using variables efficiently prevents memory shortages. For instance, avoid creating too many large variables. Instead, use smaller data types like int for simple numbers.
Conclusion
In summary, you’ve learned the essentials of Arduino variables. To start, a variable stores and manages data for your projects. For example, their scope determines where they work in your code. Additionally, you can perform operations like addition or type casting for specific tasks. Moreover, clear naming makes your code easy to read. As a result, efficient variable use saves Arduino’s limited memory.
Congratulations! You’ve completed another exciting lesson in the Arduino Bootcamp. Keep coding and building amazing projects!
So, what’s next? Keep experimenting. Keep asking questions. And most of all, keep building.
You’ve completed another major milestone in your Arduino Bootcamp.
Great job!
We’ll see you in the next lesson!
Finally, if you have any questions or want to share your projects? Connect with us on X/Twitter for more tips and inspiration!
Ready for more? click Next to continue learning.