In this article we will take a look at data types and how they are used in Arduino programming. Data types are important to understand because they determine the types of data that variables can store.

Watch the video for this tutorial here:

The 3-in-1 Smart Car and IOT Learning Kit from SunFounder has everything you need to learn how to master the Arduino. It includes all of the parts, wiring diagrams, code, and step-by-step instructions for 58 different robotics and internet of things projects that are super fun to build!

The data type of a variable is defined when the variable is declared. To declare the data type of a variable, write the data type before the variable name like this:

int variable

This declares variable as an integer. The data type used will depend on the size and type of value the variable will hold. The data type determines how much memory will be reserved for the variable.

The int Data Type

The most common data type is the integer. Variables declared as an int can hold whole number values like 3, 25, or 1023. To declare a variable as an integer, write it like this:

int variable = 4;

In this example we are declaring an int variable called variable and setting it equal to the number four. Integer variables use two bytes of memory, so they can hold 216 different numbers (up to 65,536). But the int data type can hold positive and negative values, so the range is actually split between -32,768 to 32,767.

There’s also an unsigned int data type. Unsigned means that the number is positive. Unsigned integer variables take up two bytes of memory. But since they only hold positive values, they can hold integers from 0 to 65,535.

To declare a variable as an unsigned int, use this code:

unsigned int variable = 4;

The long Data Type

The long data type is used to store large integers. Variables declared as long use four bytes of memory, so they can hold up to 232, or 4,294,967,296 different numbers. But since long variables are signed, that number is split between positive and negative values (-2,147,483,648 to 2,147,483,647).

To declare a variable with the long data type, use this code:

long variable = 4;

The unsigned long data type also uses four bytes of memory. But it can only store positive integers, so it can hold numbers from 0 to 4,294,967,295.

To declare a variable as an unsigned long, use this code:

unsigned long variable = 4;

The float Data Type

The long and int data types can only store integers – whole numbers. But what if you need to store a value with a decimal point or fraction? Use the float or double data types. float and double variables can store positive and negative fractional numbers. They both do exactly the same thing, so the keywords are interchangeable. They also both use 4 bytes of memory. The values they store can range from 3.4028235E+38 to -3.4028235E+38.

When declaring a variable with float or double, the number must be written with a decimal point or it will be treated as an integer. Something to keep in mind, is that math is much slower with floats and doubles compared to ints. So your program will run faster if you can get away with using ints instead of floats or doubles.

To declare a variable as an unsigned long, use this code:

float variable = 4.000;

The boolean Data Type

Sometimes you only need a variable to store two values. For example, when you read a digital pin or write to an LED, the only values you need to store are HIGH and LOW, 1 and 0, or true and false.

boolean variable = HIGH;

For these types of variables, you can use the boolean data type. Boolean variables can only store two possible values: true or false, HIGH or LOW, and 1 or 0. They only use one byte of memory.

The char Data Type

Use the char data type if your variable only needs to hold a single letter, number, or character. The char data type stores ASCII characters like a, b, c, 1, 2, 3, and *, %, and $ signs. Any ASCII character can be stored in a char variable and they only use one byte of data.

Every ASCII character has a unique number assigned to it. You can find the ASCII value for each character in a table like this:

ASCII Table.png

The table is broken up into two columns for each ASCII character. The value column corresponds to the numerical value of each character and the char column shows the actual character.

char variable = a;

To store an ASCII character in a char variable, use single quotation marks around the character when you declare the variable. For example, to store the degrees symbol in a char variable you could use this code:

char variable = "°";

To store the value of an ASCII character in a variable, use the ASCII number without quotation marks. For example, this will store the ASCII character “@” in the variable variable:

char variable = 248;

Using ASCII numerical values in char variables makes it possible to do arithmetic with letters. This is sometimes used to increment or decrement through the alphabet.

The byte Data Type

The byte data type is similar to the unsigned char data type. It can store positive integers from 0 to 255. It only uses one byte of memory, so it’s a good way to save program space if you need to store small values like pin numbers.

byte variable = 255;

The const Variable Modifier

The const variable modifier is not a data type, but it has an effect on how the variable behaves in an Arduino program. The const keyword makes the variable constant. Once a constant variable is declared and set equal to a value, it can’t be changed later in the program.

The const modifier is usually used for variables that will store things like pin numbers and mathematical constants. Values that will not change in the program.

Declaring a variable as const makes it read only, so it doesn’t use any SRAM, which reduces the size of your program.

const int variable = 255;

In most cases you can get away with declaring variables as integers. But using the ideal data type will conserve memory and make your programs run faster and more efficiently. For example, if you know that a variable will never store a value greater than 255, it would make sense to declare it as a byte instead of an int.

Hope this helped you get to know data types and how they’re used in Arduino programming. Be sure to leave a comment below if you have any questions!