Arduino is a versatile and popular open-source electronics platform that has revolutionized the world of
In Arduino programming, variables, data types, and constants are of utter importance as they facilitate storing and manipulating data. Here's a brief description of each.
Variables are defined as named containers that are used to store data. The data value can change during the execution. Variables are like labeled boxes in which you can place different types of information, and later, you can change that content as needed. They are used to hold various data, such as numbers, characters, and even more complex types like arrays or structures. The basic syntax for declaring a variable in Arduino is:
In Arduino programming, variables can have different scopes based on where in the code they are declared.
Local variables: The variables that are defined within a function or block of code are termed as local variables. They are only accessible within the specified block of code. Once the function execution is complete, the memory occupied by local variables is released.
void someFunction() {int localVar = 10;// localVar is accessible only within this function}
Global variables: Global Variables are declared outside of all functions, typically at the beginning of the sketch. They have a global scope, meaning they can be accessed from any part of the code, including inside functions.
int globalVar = 100; // global variablevoid setup() {// Some code}void loop() {// Some code}
Formal parameters: Formal parameters are variables declared in the function's definition and act as placeholders for values that are passed to the function when it's called.
void printNumber(int num) {// num is a formal parameterSerial.println(num);}
Constants are like variables, but their value remains constant throughout the program's execution. They are used to represent fixed values that should not be altered during the program's runtime. The constants defined in the Arduino program do not occupy any program memory. The compiler replaces references to these constants with the value specified at the time of translation. These constants are introduced with the #define
keyword.
#define
keywordOne can define constants using #define
keyword.
There are two types of constants, explained below.
Arduino allows you to define constants using the const
keyword. These constants are used to represent fixed values and are typically used for things like pin numbers or calibration factors.
const int ledPin = 13;const float pi = 3.14159;
When you define a constant with the const
keyword, its value cannot be changed during the program's execution. It acts as a read-only variable.
As constants are read-only, their value cannot be modified after they are defined. This ensures that the constant retains its intended value throughout the program's execution.
Arduino defines some logical level constants, representing true and false values as 1 and 0, respectively.
int ledPin = 13;int buttonPin = 2;void setup() {pinMode(ledPin, OUTPUT);pinMode(buttonPin, INPUT);}void loop() {int buttonState = digitalRead(buttonPin);if (buttonState == HIGH) { // HIGH represents logical true (1)digitalWrite(ledPin, HIGH); // Turn on the LED} else {digitalWrite(ledPin, LOW); // Turn off the LED}}
Using constants can be particularly useful when working with hardware, as you can define pin numbers, specific values, or calibration factors in one place, making it easier to update them if needed.
Data types define the type of data that can be stored in a variable. Arduino supports several standard data types. There exist two ways in which data types are used to declare. These can be:
Data types are used to specify the type of data that a variable can hold or that a function can return. This helps the Arduino compiler allocate the right amount of memory and perform appropriate operations on the data.
Data types determine the memory space occupied and the operations allowed on the data. Different data types occupy varying amounts of memory, and each type has specific rules for how data can be manipulated. For example, arithmetic operations on integers differ from those on floating-point numbers.
Void: The void keyword is only used to declare functions. The void indicates that the function is not expected to return information (no return value) for the function from which it was called. We can also write our own function with it, which we just call elsewhere in the code.
Bool (8-bit): A simple logical true/false type. It can have two possible values which are true
or false
, represented as 1 and 0, respectively.
Byte (8-bit): An unsigned number ranging from 0 to 255. It's commonly used for saving memory when the range is sufficient.
Char (8-bit): This data type occupies one byte of memory and is capable of storing a character value. Letters are enclosed in apostrophe ‘A’, character strings in double quotation marks ‘abc’. It is a signed data type, which means that you can store food between -128 and 127.
Unsigned char (8-bit): Same as byte data type. It takes up one byte of memory. Numbers from 0 to 255 can be stored in it. If possible, use the byte instead
Int (16-bit): A signed number ranging from -32,768 to 32,767. It's suitable for most general-purpose integer calculations.
Unsigned int (16-bit): An unsigned number ranging from 0 to 65,535. It's used when you only need positive values and want to save memory.
Word (16-bit): Same as unsigned integers. Stores unsigned numbers in 16 bits, from 0 to 65535.
Long (32-bit): It stores signed numbers ranging from -2,147,483,648 to 2,147,483,647., used when a wider range of integers is required.
Unsigned long (32-bit): It can store an unsigned number ranging from 0 to 4,294,967,295. It's used for a wider range of positive values.
Short (16-bit): A 16-bit data type for ATMega and ARM-based Arduino. It stores values between -32,768 and 32,767.
Float (32-bit): Float tracks numbers with a decimal point. Floating point numbers can be up to 3.4028235E + 38 and up to -3.408235E + 38. The float data type has only 6-7 decimal digits. This is for all digits and not for decimal places.
Double (32-bit): It is called a double-precision floating point number. On Uno and other ATMEGA-based tables, this takes up 4 bytes.
String char array: One can create a string by using a string array. These can be declared in a simple way. The last element of the character arrays is 0 (zero), indicating the compiler to recognize it as the end of the string.
Array: A memory location known as an array holds components of the same kind. Enter the array's name and the element's position number as a reference to a particular spot or element in the array.
By understanding variables, data types, and constants, you can effectively manage and manipulate data in Arduino programming to control various electronic components and create exciting projects.