Introduction

A variable is used in programming to store a value that may change during the life of the Arduino sketch. Memory is set aside for storing the variable and the variable is given a name which allows it to be accessed in the sketch.

Types of variables

These are the few variables that are used frequently in Arduino sketches:

  1. int: Integers are primary data-type for number storage. On evive, an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) – 1).
  2. float: Datatype for floating-point numbers, a number that has a decimal point. Floating-point numbers are often used to approximate analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information. Floats have only 6-7 decimal digits of precision.
  3. char: This type of datatype stores character value. Character literals are written in single quotes, like this: ‘A’ (for multiple characters, we use strings as use double quotes: “ABC”).
evive Explore
Explore: There are also other types of variables in Arduino. Click here to explore more.

Variable Definition

The standard form of variable definition is:

Variable_Datatype Varible_Name;

Variable_Datatype can be int or float depending on the type of variable you are want. Variable_Name is the name of variable. Variable is referenced or used by its name in the program.

By giving the variable a type and name, space is made available in memory for this variable.

Example: if I want to count the number of times main loop have been executed, I have to first define the variable count by:

int count;

Naming Rules for Variables

Variables can be given any name that you like, so long as it sticks to the rules set out below. It is best to give variables meaningful names that help you and others to understand the sketch better.

  • Variables can consist of any letters (a to z and A to Z).
  • Variables can contain the numbers 0 to 9, but may not start with a number.
  • Variables may not have the same names as Arduino language keywords, e.g. you cannot have a variable named float.
  • Variables must have unique names i.e. you cannot have two variables with the same name.
  • Variable names are case sensitive, so Count and count are two different variables.
  • Variables may not contain any special characters, except the underscore (_).

Assigning Value to Variable

A variable can be assigned a value by the following form:

Variable_Name = Expression;

Expression will give a valid number that will be assigned to the variable. Let us count how many time loop has been executed. Following is the Arduino sketch:

Arduino Loop Example

Let us make a new sketch, where you will add two float variable to a new variable. Given below is the code:

Arduino Precision

When the value of the variable is sent to the serial monitor window, we can see that println() automatically rounds the number off to two decimal places.

The second time that println() is used to send the value of the variable to the serial monitor window, the number of decimal places is specified as 5. This is done by passing a second parameter value of 5 to the println() function.