What types of variables do we have in Arduino?

Reading time: 6 min.

Variables in Arduino

In programming the type of declared variables is very important. It determines the correctness of the code, as well as the capabilities of the variable and, for example, the accuracy of calculations in the program we write. This article presents the types of variables used in programming in Arduino IDE environment.

Computer programming with variables

Computers, including miniature prototyping platforms like the Arduino, perform calculations and logic operations using a processor. At the heart of the processor and microcontroller, is the arithmetic logic unit (ALU), which performs simple operations on locations in memory, such as addition, subtraction and logical product. The arithmetic-logic unit does not recognise what this data represents, even despite the knowledge of the designer or target user. Data subject to arithmetic-logic operations, are needed in performing calculations on integers as well as floating-point numbers.

All the context for these operations comes from the compiler, and hints about the context come to the compiler from the user. The programmer’s task, on the other hand, is to tell the compiler which of the values taken for calculations is an integer and which is a floating-point number. Thus, the compiler must understand what we mean when, for example, we want to add together two numbers – one integer and the other – floating point. In some cases it is a simple operation, and in other cases it is not. This seemingly simple process is more complex than it may seem, especially when viewed from the perspective of performing these calculations directly by a human on a piece of paper or in memory. For this reason, when entering numerical data into a program, it is necessary to specify the type of variable assigned to this argument.

Arduino - variables types

Variable types are used to specify data types and associated functions to handle data. Specifying them is used to declare functions and variables that define the bit pattern and memory space.

The following variable types are used in the Arduino IDE development environment, which we will discuss with examples:

  • void
  • int
  • char
  • float
  • double
  • unsigned int
  • short
  • long
  • unsigned long
  • byte
  • word

Void

The void data type specifies an empty set of values and is only used to declare functions. It is used as the return type for functions that do not return any value. Consider the following code:

  1. int a = 5;
  2. void setup( )
  3. {
  4. . //
  5. }
  6. void loop ( )
  7. {
  8. .
  9. .
  10. }

Int

The data type int encompasses whole numbers such as 1, 27, 39, -11, -27, -46, etc. They do not have a fractional part (mantissa). The integer data type is considered a basic data type for storing numbers. The size of the int data type is two bytes (16 bits). The range of the int data type includes numbers from -32768 to 32767 or in exponential notation from – (2^15) to ((2^15) – 1).

In AVR ATmega microcontrollers and the Arduino UNO R3 board, the int data type stores values up to two bytes in size. However, for the Arduino Zero and Arduino MKR1000 boards based on the SAMD21 microcontroller, as well as the Arduino Due, the int data type stores values from 4 to 32 bits. Therefore, the minimum range includes numbers from -(2^31) to ((2^31)-1) or, written differently, from -2147483648 to 2147483647. Negative numbers, on the other hand, are stored in the form of powers of 2 with a natural exponent. The value of the most significant bit (MSB) determines the sign of the number. Consider the following code:

  1. int var = val;

where:

var = variable
val = value assigned to the variable

 
 For example:
  1. int a;
  2. int b = 4;

Any variable or variable identifier becomes an integer variable and can only store whole numbers. Consider the following code:

  1. int Suma = 0;
  2. void setup( )
  3. {
  4. Serial.begin(9600);
  5. }
  6. void loop ( )
  7. {
  8. Suma++; //przy kazdym okrazeniu petli, suma jest zwiekszana o 1
  9. Serial.println (Suma); //wyswietlanie obecnego wyniku sumy
  10. delay(2000); //nastepne okrazenie petli za 2s
  11. }

Char

The data type char can store any number of character sets. An identifier declared as char becomes a character variable. Literals are written in single quotes. It is often said that the char type is an integer type. This is because symbols, letters, etc. are represented in memory by associated numerical codes, and these are exclusively whole numbers. The size of the char data type is at least eight bits. We can use the byte data type for an 8-bit or one-byte-long data type like unsigned char. For example, the letter ‘A’ in ASCII encoding has the numerical value assigned of 65. For this reason, if we enter the expression ‘A’ + 2, according to the ASCII standard, it will have a value of 67. Consider the code:

  1. char var = val;

where:

var = variable

val = value assigned to the variable

Consider the following code:

  1. char myvariable = ‘ B ‘;
  2. char myvariable = 66 ; // both values ​​are equal

Float

A number containing a fractional part and a whole part is considered a floating-point number. For example, 3.142 is a floating-point number. The number 14 is an integer, and 14.0 is a floating-point number. Due to greater precision, fractional numbers are used to approximate continuous and analog values (e.g., when measuring electrical voltages using an ADC converter built into a microcontroller). Floating-point numbers can also be written in exponential form. Numbers can be as large as 3.4028235E+38 and as small as -3.4028235E+38. Thus, the size of the float data type is 4 bytes or 32 bits.

Consider the code:

  1. float var = val;

where:

var = variable

val = value assigned to the variable

Consider the code:

  1. int x ;
  2. int y ;
  3. float z ;
  4. void setup ( )
  5. {
  6. Serial.begin (9600);
  7. }
  8. void loop ( )
  9. {
  10. x = 3 ;
  11. y = x/2 ; // y is an int variable and cannot store fractions, so it will return 1
  12. z = (float) x / 2.0 ; // z stores the value 1.5 because it is of type float
  13. // this means we have to use division by 2.0 instead of 2
  14. }

 

From the above code, it follows that even if we use whole numbers for floating-point calculations, we should append a decimal point and a zero to them. Otherwise, the compiler will understand that we used an integer and will return integer values. Floating-point numbers can also be converted to integer form. Consider the code:

  1. float a =4.7 ;
  2. int b = a + 0.5 ; // the program returns the value 5

 

Double

The double data type is also used for handling decimal or floating-point numbers. It occupies twice as much memory as the float type. It stores floating-point numbers with greater precision and range. The double type denotes double-precision floating-point numbers and takes up four bytes on ATmega and UNO boards and 8 bytes on Arduino Due. Consider the code:

  1. double var = val;

where:

var = variable

val = value assigned to the variable

Unsigned int

The unsigned int type stores a value up to two bytes or 16 bits. It holds only positive values. The range of the unsigned int data type is from 0 to 65,535 or from 0 to ((2^16) – 1). Arduino Due stores an unsigned data value of 4 bytes or 32 bits. The difference between the unsigned data type and the signed one is the sign bit. In a 16-bit number, 15 bits are interpreted with two’s complement, while the most significant bit is interpreted as a positive or negative number. If the most significant bit is “1”, it is considered a negative number. Consider the code:

  1. unsigned int var = val;

where:

var = variable

val = value assigned to the variable

For example:

  1. unsigned int pinLED = 3;

Short

Short is an integer data type that stores two bytes or 16-bit data.

The range of the short variable type is from -32768 to 32767 or – (2^15) to ((2^15) – 1). Arduinos based on ARM and ATmega microcontrollers typically store the short data type value in two bytes. Consider the code:

  1. short var = val;

where:

var = variable

val = value assigned to the variable

For example:

  1. short pinLED = 3;

Long

The long data type is considered to be an extended size variable that stores four bytes (32 bits). The size range of the long data type spans from -2147483648 to 2147483647. When using integers, at least one of the numbers should be followed by the letter L, which enforces the number to be of the long data type. Consider the code:

  1. long var = val;

where:

var = variable

val = value assigned to the variable

For example:

  1. long speed = 186000L;

Unsigned long

Data types of the unsigned long kind are also considered to be extended size variables that store 4 bytes (32 bits). However, this type does not store negative numbers, like other unsigned data types, making its range span from 0 to 4,294,967,295 or (2^32 – 1). Consider the code:

  1. unsigned long var = val;

where:

var = variable

val = value assigned to the variable

For example:

  1. unsigned long time;

Byte

Byte is an unsigned data type that stores values between 0 and 255. Consider the code:

  1. byte var = val;

where:

var = variable

val = value assigned to the variable

For example:

  1. byte x = 22;

Word

The word type concerns non-negative numbers written in sixteen bits or two bytes and ranges from 0 to 65535. Let’s consider the code:

  1. word var = val;

where:

var = variable

val = value assigned to the variable

For example:

  1. word d = 5000;

How useful was this post?

Click on a star to rate it!

Average rating 4.4 / 5. Vote count: 30

No votes so far! Be the first to rate this post.

Share:

Picture of Maciej Figiel

Maciej Figiel

Versatile, he is eager to take on challenges because he thinks it is the fastest way to progress. He values contact with nature and an active rest. Automotive and new technologies enthusiast.

See more:

Leave a Reply

Your email address will not be published. Required fields are marked *

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.