Arduino Data Types & Variables – Full
Explanation with Examples
🔹 What are Data Types in Arduino?
Data types define the type of data a variable can store. In Arduino, different data types are used
to store numbers, characters, and complex structures like arrays.
🔹 Basic Data Types in Arduino
Data Type Size Range Usage
void
Used for functions that don’t return
- -
a value
boolean 1 byte true (1) or false (0) Used for on/off conditions
char
Stores a single character or small
1 byte -128 to 127
numbers
byte 1 byte 0 to 255 Stores unsigned small numbers
int
2
-32,768 to 32,767 Used for general integer values
bytes
unsigned int
2
0 to 65,535 Only positive numbers
bytes
long
4 -2,147,483,648 to
Large numbers
bytes 2,147,483,647
unsigned long
4
0 to 4,294,967,295 Large positive numbers
bytes
float
4
±3.4E-38 to ±3.4E+38 Stores decimal values
bytes
double
4 Same as float (on Arduino Stores high-precision decimal
bytes Uno) values
string (String
class) - - Used for text manipulation
char array - - Stores a sequence of characters
1️⃣ Integer Data Types (**, **, ``)
📌 Example: Using Integers for LED Blinking
int ledPin = 13; // Store LED pin number
int delayTime = 500; // Store delay in milliseconds
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(delayTime);
digitalWrite(ledPin, LOW);
delay(delayTime);
}
2️⃣ Floating-Point Data Type (**, **)
📌 Example: Reading Temperature from an LM35 Sensor
int sensorPin = A0; // Analog pin for LM35
float temperature; // Stores temperature value
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
temperature = sensorValue * 0.48828125; // Convert to Celsius
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000);
}
3️⃣ Boolean Data Type (``)
📌 Example: Button Press Control
int buttonPin = 2;
int ledPin = 13;
boolean buttonState;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == true) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
4️⃣ Character Data Type (``)
📌 Example: Display Characters on Serial Monitor
char myChar = 'A';
void setup() {
Serial.begin(9600);
Serial.print("Character stored: ");
Serial.println(myChar);
}
void loop() {
}
5️⃣ String Data Type (**, ** )
📌 Example: Print Name on Serial Monitor
String myName = "Arduino";
void setup() {
Serial.begin(9600);
Serial.println("My name is " + myName);
}
void loop() {
}
📌 Alternative: Using `` for Strings
char myName[] = "Arduino";
void setup() {
Serial.begin(9600);
Serial.print("My name is ");
Serial.println(myName);
}
void loop() {
}
6️⃣ Unsigned Data Types (**, ** , ``)
📌 **Example: LED Blink Timer with **``
unsigned long startTime;
void setup() {
Serial.begin(9600);
startTime = millis(); // Store current time in milliseconds
}
void loop() {
Serial.print("Time elapsed: ");
Serial.println(millis() - startTime);
delay(1000);
}
🔹 Summary Table of Example Uses
Data Type Usage Example
int Storing pin numbers, counters
float Storing temperature, voltage
boolean Checking true/false conditions
char Storing a single character ('A')
String Storing text data ("Arduino")
char array Alternative for text storage ("Hello")
long Storing large numbers
unsigned long Storing millisecond timestamps (millis())
byte Storing small numbers (0-255)