Arduino

Arduino data types

When writing an Arduino code to perform a task or working on a project the data has to be classified in different types. The microcontroller understands the information in the form of 0 and 1 and it is difficult for a user to declare the number of bites for each data. To organize and categorize the data in different forms is quite helpful for programmers to understand the data given and perform the assigned task. For these different data types are used in the Arduino programing.

There are two basic data types that are used in Arduino programming: one is variable data type and the other is constant data type.

Variable data types in Arduino

The variable data type is used for the data that will change may be after each iteration of the loop or may be some given time frame. The variable data type is further divided into thirteen different types:

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

void data type in Arduino

This data type is used in Arduino programming only when the functions are to be declared. Similarly, this data type also tells that the declared function has no returning value. Moreover, set-up and loop functions also use the void data type.

void setup()
{
Serial.begin(9600); //To start a serial communication
}
void loop(){

}

int and unsigned int data type in Arduino

To define the data having only numbers the data type we use is the “int” type. This data type specifies only the integers Usually, the data is in the form of numbers, so it is the most used data type in Arduino programming. The “unsigned int” is used only when positive values are to be used having a range from 0 to 65,535. This size can vary based on the specification of the Arduino.

The difference between the int and unsigned int is that the unsigned int data type can store big values of data because it cannot store negative values and this gives the unsigned int an extra space. The int data type has only two-byte memory and it can store negative numbers as well.

An Arduino code example is given to further explain how to declare int and unsigned int data types:

int a=2;
int b=-7;
unsigned int c= 5;

char and unsigned char data type in Arduino

The char data type is a short form of character data type that is only used to store the alphabets like “a, b, d, A, B, D”. The unsigned char means the positive numbers only since the characters are also stored in numbers in Arduino so when only positive values are assigned in the characters the unsigned char is used . Both signed and unsigned char have equal storage space,but their ranges differ from each other; the signed char has a range of -127 to 127 as it can accommodate both positive and negative numbers whereas, for unsigned char the range is from 0 to 255.

There are two ways of declaring the characters one is by simply declaring it with its alphabet and the second way is by giving its ASCII code. The ASCII is the conversion of the alphabets to a number. Here in the Arduino code the character B is declared in both forms.

char=’b’;
char val=-98;
unsigned c=5;

bool data type in Arduino

To perform the logical operations the data type used is bool data type. Bool is the short form of the Boolean. This data type categorizes the output in binary form that is 0 or 1 and is used with the Boolean operators. The use of data type is further explained with the help of example code by applying the logical operator.

int a=6;
int b=5;
bool c;
void setup()
{
Serial.begin(9600);
c= a&&b;
Serial.println(c);
}

void loop(){
}

byte data type in Arduino

This data type is used to allocate the specific space for the data that is in the form of characters; the purpose of the space allocation is to minimize the extra usage of the space by the data. The bytes can be allocated ranging from 0 to 255. Here is the syntax b is the variable and 35 is the value assigned to variable b:

byte b =35;

word data type in Arduino

The word data type only stores unsigned numbers that is positive numbers having a range from 0 to 65535:

word z = 3000;

long and unsigned long

The long data type is used to extend the storage if large numbers are to be stored. The use for the unsigned long is the same as that of long datatype; the main difference is that the unsigned long is used only in case of positive numbers. To get a more clear concept we can say that using long data types on a 32 bit system will have a range from (-2^32) to (2^32-1) however in case of unsigned the range will be (2^32 -1).

long A = 100000;
Or
a = 100000L;
unsigned long a = 100000;

double data type in Arduino

When the data is in decimals and precision in the data is required up to 15 digits the double data type is used. It can also be said as a double precision data type having a wider range of digits.

double f = 789.56213;

short data type in Arduino

When the data used is short or small is to categorize the data type used for such data as short type data. It also uses 2-byte memory of Arduino and has 16 bit values but has a range of (-2^15) to (2^15) -1).

short m = 15;

float data type in Arduino

The most important and the most common data type after the integer data type is the float data type. This data type is used when the data is in the decimal form. This data type has a memory of 4 bytes and has a range of almost to 7 digits. This data type is also called single precision data type:

float X= 12.35;

const data type in Arduino

This data type is used only when the data is not variating, that is the value once defined will not change throughout the program

const float X = 57.69;

Conclusion

The data types are used to find the type of data so that the associated functions can be used with them in programming. The data types are used to declare variables and functions in Arduino programming. This article explains in detail what are the data types that are used in the Arduino programming language and the declaration of each type in the code is also given.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.