• Worldwide Shipping

  • 24/7 Customer Support

  • FAQ
  • Worldwide Shipping

  • 24/7 Customer Support

  • FAQ

Arduino IDE: Variables

What is a variable?

A variable is a name for a location in the memory, and that location contains the value we work with. The basic reason we use them is the fact that we and computers do not speak the same language. Digital electric circuits with logic gates (1 and 0 values), internally, use binary system. To us, it would be extremely complicated to communicate using binary system, so we use a compiler or traslator which turns our language into binary. The Arduino IDE also has a compiler specifically designed for AVR microcontrollers (called avr-gcc), and in this tutorial we will deal with variables we use when writing codes for Croduino and other Arduino compatible boards.

Online binary converter

While programming Croduino, we use them to name and save a value, for example sensor readings, for further use in the program. Variable has its type, name and value. Let's check out an example:

int led = 13;

int is a type of variable, led is the name of the variable, and 13 is variable's value. This part is called declaration or variable assignment, more about it in the next passage.

Besides that, variables have their own scope, so we divide them into local and global ones. Global variables are visible in every function of the program code, while local ones are visible only in the function they are declared for.

// all variables declined in this part Arduino IDE
// sees as global ones

// "led" is a global variable
int led = 13;

void setup() {
// all variables declined within a function Arduino IDE
// sees as local ones, and they can be used solely within that function

// "broj" / number is a local variable  
int broj = 3;
  // led variable can be invoked in any function

void loop() {
  // Arduino IDE will let us know about the error on this type of code
  // because we use local variable outside the defining function
  broj = broj + 1;

A code error can be corrected by defining a number value outside the function, thus it becomes a global variable.

How to set a variable

Variable name

Variable names can contain only letters (capital and small) and numbers, of characters only the underscore "_" symbol. Also, they can not contain special Croatian language characters such as "č","ć","đ", etc. They must not begin with numbers, have a space in the name or have the same name as some keywords predefined in the programming language. Usually, the names of the variables start with a lowercase letter.

These are some of the examples of the right (although not recommended) names of variables:


These are examples of incorrect names of variables:

13pin    (must not start with a number) 
moj pin  (must not contain space)
$led     (must not contain special characters)
očitanje (must not contain Croatian language characters)
while    (must not have the same name as predefined function)

Types of variables

Variable type is the way a variable is stored in memory, and it depends on what we actually want to save under a specific variable. A good programmer will watch out which type he will choose for defining his variable. Besides the fact that wrong choice of variable can lead to misunderstanding with a computer, it can also take up much more energy than it really needs. Namely, each type of variable takes up a certain computer memory regardless of whether it is used or not.

Below is a list of variable types, which  we often encounter in Arduino IDE, with the amount of memory they occupy.

    • boolean (8bit) - a simple true/false, value 1(true) or 0(false)
    • byte (8bit) - positive numbers from 0 to 255
    • char (8bit) - numbers from -128 to 127, we can define symbols using single brackets (e.g. 'e')
    • word (16bit) - positive numbers 0-65535
    • unsigned int (16bit) - the same as word
    • int (16bit) - numbers from -32768 to 32767
    • unsigned long (32bit) - positive numbers 0-4,294,967,295
    • long (32bit) - numbers between -2,1471486,648 and 2,147,483,647
    • float (32bit) - single precision numbers with decimal point from -3.42823e+38 to 3.4028235e+38
    • double (64bit) - doubleprecision numbers containing decimal point with great precision and range

Note: It is recommended to avoid float and use double instead (but only in Arduino, not generally C!). Prefix unsigned marks that only positive numbers are used, allowing a greater range of positive numbers (negative numbers can not be used with that variable).
Variable values

We can but do not have to assign a value to the variable. Sometimes the memory allocated to the variable already has a stored value so it is recommended to define a value of "0" if we want to perform mathematical operations with it.

int varijabla;
int varijabla = 0;   // both are correct

What you should bear in mind, is roll-over which appears when variable comes out of its domain.  In the example of a byte type variable which has a range of 0-255:

byte broj = 255;
broj = broj + 1;  // "broj" is now 0 

broj = 0;
broj = broj - 1;  // "broj" is now 255

#define i const

#define allows giving names to a constant value before compiling the program and we consider it a preprocessor command. This value can no longer be altered in the code.

#define led 13

const is a prefix added to a type of function when we want to create a constant (such as #define), thus it makes the variable read-only.

const float pi = 3.14;
Leave a Reply