What are Variables and Data types in Arduino

What are Variables and data types in programming, and why variables and data types are used in programming Are some questions that may arise in your mind during Arduino programming?

Hello guys! Welcome back to my blog. Today we are going to see what variables and data types so let’s begin.

What are Variable??

Variables are nothing but memory space that helps to store, hold, and recall values. Variables can be defined as local variables or global variables depending on where the variable is initialized or created. Local variables are declared within a function only. This type of variable is used inside a specific function only.

Arduino variable types

We can use a variable many times in a program. It is easy to modify the values of variables at any time. For example, if we want to change the value of variable led from 13 to 10, then we have to change the value only at one place. It will automatically reflect at other places where the variable is used in the entire program.

To get more clear let’s take an example. Variables are just like a box. We keep things in a box and take them out whenever we want or require them.

So let’s compare, our box is refer to a variable, and things that we put in or out of the box is referred to as values, we can store the values in variables and use that variable according to various different operations.

       Syntax -    int led = 13

The name of the variable goes on the left side and the value that you want to store in the variable goes on the right side. One should define the name of the variable in such a way that it should give a clear description for another reader for understanding it.

  Here value 13 is stored inside variable name led, as 13 is an integer value data type we use will be int.

                     =  is an assigned operator.

    NOTE –  you can name your variable according to your choice & don’t get confused about Assign operator (=) & equality operator (==).

    After seeing the above syntax you may be wondering what datatype is.

What is Data type?? 

Data types are used to determine the number of memory byte that has to be assigned to a variable, and what kind & type of data is to be stored inside variables example – integer, string, or float. Data type describes the nature of value stored inside variables.

To understand data types let’s take an example of the box, we have three boxes that differ in size and we have three items a car, a cricket bat, and a wallet. Your job is to put items into the box that fits the best. So first we take a car, now you see box-2 has height but width is minimum so the car cannot get fit inside the box-2. Now, box-3 has small dimensions so the car cannot get fit in box-3. Similarly for cricket bat, it cannot get fit in box-1 and box-3.  Now we have item no 3 wallets, it can be fit into box-1 and box-3 as box-2 has a width of box-2 is less.

Same here, as we know variables are just like your box to store data or value, we have seen according to items box size changes, similarly according to the type of data such as numbers, decimal number, a character we choose data type example int, float, char.

 So let’s discuss some of the data types mostly used in Arduino Programming

  • Int
  • Long
  • Floats
  • Double
  • Byte
  • Boolean
  • Char

1) Int

unsigned int Arduino

an integer is the most common data type used to hold the whole number. You cannot store decimal values in int data type.

It ranges from -32,768 to 32,767 

I.e. positive thirty-two thousand seven hundred and sixty-seven to negative thirty-two thousand seven hundred and sixty-eight.

Size:   2 bytes (16 bits)

Example – int first_value = 100

                     Int second_value = -200

When we talk about signed integers, it includes negative as well as a positive value.

Suppose in case we have a condition where we want a number of days in a month, where negative values are not present we only want positive values so we need qualifier unsigned integer.

Unsigned integer – here we have no negative values it ranges from 0 to 65,535 having a size of 2 bytes (16 bits)

Example – unsigned int first_value = 80

                  Unsigned int second_value = 5000

2) long

Arduino variable types

if you want values higher than int data types then we have another data type called long.

It ranges from - 2, 147,483,648 to 2,147,483,647

Size: 4 bytes (32 bits)

Example – long  first_value =67762851

               long first_value = -3482638

Just like in unsigned integer we have unsigned long with a range from 0 to 4,294,967,295 with no negative values. It is just a very big number.

Example - unsigned long first_value = 1016743

3) float

Arduino types of variables

Suppose in the case where we have to save decimal values such as temperature or humidity reading, in that case, we used float data type.

It hold value ranges from -3.4028235E +38 to 3.4028235E+38

Size: 4 bytes (32 bits)

Example – float first_value = 29.33

               float second_value = 16.706 

4) double

double is similar to float both are used to store decimals. But double datatype holds 8 bytes of data that is bigger than float so you can store larger numbers in double such as location on map longitude and latitude values.

Size: 8-byte (64-bit)

double first_value = 1617066

 5) Byte

Arduino types of variables

byte data-types store values from 0 to 255 as name byte we can store one byte (8-bit).

    Example - byte first_value = 50

6) Boolean

There is another data type that holds a value smaller that a byte. Boolean data types range from true or false with the size of 1/8 bytes (1 bit).

Boolean data types are used when we want to represent two states high or low. For Boolean data type value other than zero is considered as true such as 2,-8, and 4 and for the false value we have 0 or keywords false, low.

7) char

variable types Arduino

character data types are used to store values such as letters, strings, or words.

It ranges from -128 to 127

Size: 1 byte (8 bit)

 Example – char first_value = ‘a’

char second_value = 97

The letter can also store numbers that represents a specific character in ASCII (American standard code for information interchange).

That means 97 in ASCII is equivalent to the character ‘a’ in lowercase.

Now, I hope you got cleared what is variables and data types. Now we will see what is declaration and initiation.

The declaration means declaring data type and giving a name to the variable & initiation means assigning value to a variable. 


Restriction while using a Variable

  • Spacing – not to give any space while declaring a name to a variable instead use under-scroll.
  • Unique word – don’t add unique symbols such as hashtags #, @ while declaring the name of the variable.
  • Don’t start with a number – while writing a variable name does not use a number first, start a variable name with a letter. You can’t start with a number but you can use the number in a variable name.
  • Keywords – you are not allowed to use keywords as variable names. Arduino has some pre-defined keywords, we can’t use them in a variable name.

 Conclusion - 

Today we learn about what is Arduino variable types, the scope of variables (local variables, global variables) and Arduino data types, the types of data types used in Arduino programming, and How to use variables and data types in Arduino.


 "I hope you find this lesson on Arduino very helpful to you and understand about variables and datatypes use in Arduino. In the upcoming lesson, we will learn about what functions till then bye. See you all in my next chapter of Arduino lessons."

Close Menu