To handle Arduino you will need to combine electronics and programming. You have to learn the things, they have to go hand in hand.
We have to start somewhere 😊. So let’s start with an introduction to programming, and in the next section we’ll see hardware and electronics.
If you are just starting out, my recommendation is to alternate. Learn a little programming, then electronics, then programming, then electronics… each time making both a bit more difficult.
For now, let’s see the basics of programming so you can start making your Arduino projects. If you want to learn more programming, you have this entire website, like this course 👇.
Learn to program in the introduction to programming course
So let’s start learning how to program our Arduino, by seeing what variables are Variables.
What is a variable?
A variable is a small space in the Arduino’s memory that we reserve to store a piece of data. Like a moving box.
So the Arduino doesn’t get confused with so many boxes, we need to do two mandatory things when creating it:
- Put a label on it (Name): To know how to ask for that data later (
speed,brightness,pinLed). - Tell it what fits inside (Type): It’s not the same to store a sock as an encyclopedia. In programming, we have to define the Data Type before using it.
The three fundamental types
Although C++ (Arduino’s language) has many types, to start you only need to master these three. They cover 99% of your initial needs.
ints are the standard boxes. They are used to store whole numbers, without decimals.
- Range: From -32,768 to 32,767 (on Arduino UNO).
- Uses: Pin numbers (
int led = 13), counters (int turns = 0), analog readings (0 to 1023).
int age = 35;
int pinLed = 13;
floats are special boxes for numbers with a decimal point.
- Uses: Precise mathematics, real voltage sensor readings (
2.5V), temperature (23.4ºC). - The price to pay: They are much slower for the Arduino to process and take up more memory. Don’t use
floatif you can useint.
float temperature = 23.5;
float voltage = 4.88;
bools are the smallest boxes. They can only contain two values: True or False.
- Values:
true(1) orfalse(0). - Uses: Switch states, control flags (“Is the system on?”).
bool systemOn = true;
bool isNight = false;
Scope
When we create a variable, it’s not always visible to all the code. It depends on where we write it. This is called Variable Scope.
These are variables we declare outside of any function, usually right at the very beginning of the code (before setup).
- Visibility: Everyone (
setup,loop, and any other function) can see, read, and write them. - Duration: They never die. They keep their value as long as the Arduino is on.
It’s like writing data on the classroom blackboard. Anyone who enters the room can see the number, erase it, and write another. The data stays there for the whole class.
These are variables we declare inside a function (inside the braces { ... } of setup, loop, or an if).
- Visibility: They only exist inside those braces. No one outside knows they exist.
- Duration: They are ephemeral. They are created when the function starts and are destroyed (erased) as soon as the function ends.
It’s like writing data in your pocket notebook. Only you see it. When you leave the class (the function ends), you tear out the page and throw it away. If you come back in, you start with a blank page.
Practical example: The Counter vs The Temporary
Let’s see a code that uses both types to understand the difference. We want to count how many times an LED blinks.
// --- GLOBAL VARIABLE ---
// It's outside. It's the "Blackboard".
// We need it to remember the count loop after loop.
int totalCounter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
// --- LOCAL VARIABLE ---
// It's inside. It's the "Notebook".
// It is created anew in EACH loop iteration.
int waitMilliseconds = 500;
// We increase the global count
totalCounter = totalCounter + 1;
// We print
Serial.print("Blink number: ");
Serial.println(totalCounter);
delay(waitMilliseconds);
// When reaching this closing brace '}', the variable
// 'waitMilliseconds' IS DESTROYED.
// But 'totalCounter' SURVIVES.
}
- What if I put
totalCounterinside theloop? In each iteration, a new variable would be created, set to 0, add 1… and would always print “1”. When the loop ends, it would be destroyed. The Arduino would have amnesia. - Why don’t I make them all global and avoid trouble? It’s tempting, but it’s bad practice. If you have a program with 100 global variables, it’s very easy to make a mistake and unintentionally use one variable for two different things. Also, they permanently occupy RAM memory.
Naming Tips
To finish, a tip on how to name your variables. Arduino does not allow spaces.
int number led = 13;❌ Fatal error.int number_led = 13;✅ Good (snake_case).int numberLed = 13;✅ Better (camelCase, the Arduino standard).
And please, be descriptive with the names.
int x = 13;😕 What is x?int redLedPin = 13;😎 Professional.
