Data Types

Arduino data types play an essential role in Arduino programming. Arduino, bring a computer uses memory allocations to store and manipulate data. Therefore, as you write your code, you must always allocate special memory areas to store your data. Furthermore, the data types enable the Arduino to recognize the data received or sent, making it easy to handle the data.

Without data types, you cannot determine how many bytes of memory are dedicated to that variable, and what kind of data can be stored in the variable, which makes the data type of the variable important.

A data type can be defined as a classification that describes the value a variable holds and the operations, we can perform on it. Other than variables, functions also have data types depending on the values they return.

In this tutorial, you will learn about common data types used in Arduino programming. The data types are:

  • void
  • boolean
  • char
  • int
  • String
  • float

Note: This tutorial will not cover other data types such as long, arrays, pointers, and short as they are not very common, and we are trying to keep this simple and easy for beginners.

Without further ado, let us jump right into our first data type, void.

void

void is only used when declaring functions. It is used to indicate that the function is expected to return no values when they are called.

It is important to note that Setup and Loop functions are of type void and do not return any information as well.

Example void code

void setup () //The function does not return any data when called

{

Serial.begin(9600);

}

void loop()

Boolean

boolean holds either one of the two Boolean values, true or false. Therefore, Boolean can be used to check and store data of components with two status (True or False).

This Arduino Data type has a memory of 8 bit / 1 byte.

Example Boolean code

int LEDPin = 13;   // LED connected to pin 13

boolean LEDon = false;//Store the status of the LED

void setup()

{

pinMode(LEDPin, OUTPUT);

}

void loop()

{

if (digitalRead(LEDpin) == HIGH)

{  //LED is on

LEDon=true;

}

else{

LEDon=false;

}

}

char

char, which is short for character, is a data type used to store a character value, e.g., A, B, C. Initializing a single character uses a single quote, e.g. ‘A’. In contrast, a group of characters such as string uses a double quote such as “Good”.

Characters are typically stored as numbers based on the ASCII encoding. For the specific encoding, you can refer to the ASCII Chart, as shown below.

As characters will be stored as numbers, you can do arithmetic operations on them using the ASCII value of the character. The char data type encodes numbers from -128 to 127.

This Arduino data type has a memory of at least 8 bits. You are recommended to use char for storing characters.

Example char code

char myChar = 'A';

char myChar = 65; // both are equivalent

Integer

int, which is short for integer, is one of the most commonly used data types in Arduino. They are the primary data types for storing numbers. However, integers cannot hold decimal numbers. In case decimal numbers are assigned to an integer, the board will cut out the decimal part, leaving only the whole part.

NOTE: int size varies from board to board. For example, in ATmega based Arduino boards like the Uno, Mega and Nano, an int uses 2 bytes of memory and as a range of -32,768 to +32,767. While for the Due and SAMD based boards (e.g., MKR1000, Zero), int uses 4 bytes of memory and as a range of -2,147,483,648 to + 2,147,483,647.

Example int code

int count = 0; //creates a variable integer called 'count'

String

Sting data types are majorly used to store words or a collection of characters. You can use the String data type, which is part of the core as of version 0019, or you can make a string out of an array of type char and null-terminate it. In Arduino programming, Strings can be used to store names of things or display words. Strings must be written within two quotation marks.

Example String code

String Company = “Novatech”;

Float

The float is one of the essential Arduino data types, as it can store decimal numbers. This data type is for floating-point numbers, which are numbers with a decimal point.

Floating-point numbers are often used to approximate the analog and continuous values because they have greater resolution than integers.

This data type has a memory of 32 bit/ 4 bytes and a range of -3.4028235E+38 to +3.4028235E+38.

Example float code

float sensorCalbrate = 1.117;

int x=3;

int y=2;

float z;

x = 1;

y = x / 2;         // y now contains 1, ints can't hold fractions

z = x/y; // z now contains 1.5 the decimal can only be contained in a float value

Which data type do you choose to use when programming your Arduino?

The choice of the data type will depend on the kind of data to be stored and how the data will change in the future. For instance, it is advisable to store the results of calculations in a float as it can hold more accurate data compared to the integer data type. Furthermore, words cannot be stored in integers. Therefore, when you expect to store words, you must use the String or char data type. However, sometimes a wrong choice of the data type may not affect the program to a great extent as long as high precision is not needed.

You may also like...

Leave a Reply

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