What is Arduino?
An Arduino is an open source platform for trying out crazy hardware and software tricks on a small 2.7-inches board. It is majorly used for building small projects which are creative and sometimes even beyond our imagination. There are a number of Arduino microcontroller boards available in the market which lets you finish your DIY electronics project in a useful and quick way.
With Arduino, you can create anything you have ever visualized into your head. Just by giving simple inputs we get the desired output. Arduino is somewhat similar to the Raspberry Pi. However, it’s configured in a much simpler way. Moreover, its main purpose is to do automation. Arduino can read inputs like an LED Sensor, a finger on a button and turn it into an output. We can tell the board what to do by providing a set of instructions as an input.
There is a built in software available for Arduino to write code named as Arduino IDE. It helps the coders to write and upload the program easily and quickly. And all these require a variable type (set of rules) for providing authentication.
In this article, we will cover the basics of data-types used in Arduino.
What is a Data type?
The size and the type of value to be stored in a variable is known as Data type.
Now you will wonder what a variable is? Let’s discuss in short about what is a variable?
Variable is basically a container which stores value in it. This value can be of any type. Variables are user-defined. There is a set of rules which we need to follow while defining the name for a variable. For example, I have 2 apples, 1 mango, 4 oranges and one container in which I can keep those fruits. Here the container is our variable(where we can store values), and the fruits are our data types.
Let’s get back to what is a data type and understand in detail.
So in a variable, we store different types of information which can be a whole number, decimal number or even alphabets. Some data types which also known as primitive data types are already predefined in the language itself. You can also create your own data type if you want to.
List of Arduino data types covered in this article:
Let’s now discuss all of them in detail and understand each with an example.
Int is a short form of ‘Integer’ which used to store Whole Numbers. It can store a data of 16-bit(2-byte) value. It ranges from -32,768 to 32,767.
Example on how to initialize Int.
int apple = 2; (This will store value 2 in variable apple)
int apple; (This will have an empty variable apple)
Int apple, orange, mango; (multiple integer variables)
apple=orange=mango = 2; (This will store value 2 in apple, orange, and mango)
Note: The size and the range differ from board to board.
Used to store Decimal points. Float data type majorly used for higher precision values. It can store a data of 32-bit(4-byte) value. It ranges from -3.4028235E+38 to 3.4028235E+38. List of items like Salary, temperature, PI value use Float variable type.
Example on how to initialize float.
float temp = 3.14; (This will store decimal value 3.14 in variable temp)
It uses only 1-byte or 8-bit of memory. It stores characters like A, B….., Z. Initializing single character will require single quotes ‘Y’, while initializing multiple characters will require double quotes “XYZ.” In the backend, all character’s ASCII values are stored as numbers. For example, if you write ‘B’ in the backend, it stored as “66” according to the ASCII value.
See the full table here. ASCII values
Example on how to initialize char.
char alphabets = ‘A’; (This will store A character value “65” in alphabets)
Boolean can store values only in True or False. It uses 8-bit or 1-byte of memory.
Example on how to initialize boolean.
boolean check = true; (Initializing variable check with true)
boolean mate = false; (Initializing variable mate with false)
Long is an enlarged version of Int. It can store double the size of an int. It can store a data of 32-bit(4-byte) value. It ranges from -2,147,483,648 to 2,147,483,647 and used to store huge numbers in billions and trillions.
Example on how to initialize long.
long number = 2343457391;
Arduino ATMega and ARM uses short data type. It can store a data of 16-bit(2-byte) value. It ranges from -32,768 to 32,767.
Example on how to initialize short.
short number = 30;
A byte stores an 8-bit unsigned number, from 0 to 255.
Example on how to initialize short.
byte a = 100;
It depends on the board what size is used on it. On some boards, word data type uses 16-bits and on others 32-bits.
Example on how to initialize word.
word check = 500;
How to know what data type to select?
It ultimately depends on what kind of data you want to store.
Suppose if you want to save age of a person. In this case, if you are taking int then it’s a wastage of memory because the age will never be 32000. Instead, I would insist on taking Byte which accepts a number from 0 – 255 which is more than enough when considering about age.
All data types are important. Defining our own variable types every time we write programs can be daunting. So, these are some of the primitive data types which make our task easier to code for Arduino.
I hope you learned something new today from this article.
If you have any questions regarding Arduino, please let us know in the comments section below.