Variables
Labelled boxes where your program keeps things it needs to remember.
In 10 seconds
- • A variable is a named box holding a value.
- • int = whole numbers, float = decimals, bool = true/false.
- • Name your pins with const int for readable code.
A variable is a box with a name written on the side, and something stored inside it. You can look in the box whenever you like, and you can swap the contents for something new. That is genuinely all there is to the idea — the details are just about what kind of thing each box can hold.
Making a variable
You create one by saying what type it is, giving it a name, and usually putting something in it straight away:
int ledPin = 9;— a whole number.float temperature = 21.5;— a number with a decimal point.bool doorOpen = false;— true or false, nothing else.char grade = 'A';— a single character.String name = "Radar";— a piece of text.
After that, using the name is the same as using the value. digitalWrite(ledPin, HIGH) means exactly the same as digitalWrite(9, HIGH), but it reads better and it is far easier to change later.
Why the type matters
The Arduino has a small amount of memory — about 2 kilobytes on an Uno — so it wants to know how big each box needs to be. An int takes two bytes and can hold values from −32,768 to 32,767. A byte takes one and holds 0 to 255. A long takes four and handles much bigger numbers, which is why timing code uses unsigned long for millis(): after about 32 seconds an int would overflow and wrap around to a negative number, and your timer would go haywire.
Types also change the maths. Divide 7 by 2 as ints and you get 3, because ints simply throw away the fraction. Do the same with floats and you get 3.5. This trips up everyone at least once when a temperature calculation mysteriously loses its decimals.
const: boxes that never change
Pin numbers do not change while a program runs, so it is good practice to mark them: const int LED = 9;. The word const means constant. It saves memory and, more usefully, it makes the compiler shout at you if you ever accidentally try to change it.
Where a variable lives
This idea is called scope, and it explains a lot of confusing errors. A variable created inside a function only exists while that function is running — it is created fresh each time and thrown away at the end. A variable created at the top of your sketch, outside all the functions, is global: every function can see it, and it keeps its value between loops.
That difference matters enormously. If you want your project to remember something from one loop to the next — a score, a state, a last-recorded time — the variable must be global. Put it inside loop() and it will be wiped clean thousands of times a second.
Naming things well
Programs are read far more often than they are written, usually by you, three weeks later, having forgotten everything. int t = 30; means nothing. int alarmTemperature = 30; explains itself. Take the extra second.
Quick quiz: Variables
3 questions to check it stuck.
Next lesson
🔀 If Statements
Ready to try it for real?
Reading is good. Wiring is better. Pick a project and put this to work.
Explore projects