Arduino Data Types – Tutorial & Examples
In Arduino programming, variables are used to store data, and each variable has a specific data
type that determines the kind of values it can hold. Understanding data types is essential for writing
efficient and bug-free code.
1. Basic Data Types
Data Type Size (Bytes) Range Example
int 2 -32,768 to 32,767 int x = 100;
unsigned int 2 0 to 65,535 unsigned int u = 40000;
long 4 -2,147,483,648 to 2,147,483,647 long big = 100000;
float 4 ±3.4028235E+38 (6-7 decimals) float pi = 3.14;
double 4 (8 on some boards) Same as float on Uno double val = 3.14159;
char 1 -128 to 127 / ASCII characters char letter = 'A';
byte 1 0 to 255 byte b = 255;
boolean 1 true or false boolean ledOn = true;
String varies text storage String name = "Arduino";
2. Example Codes
// Integer Example
int ledPin = 13;
boolean ledState = true;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
if (ledState) {
digitalWrite(ledPin, HIGH); // LED ON
} else {
digitalWrite(ledPin, LOW); // LED OFF
}
}
// Float Example with Sensor Value
float temperature = 25.6;
float humidity = 60.2;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Temp: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
delay(1000);
}
3. Arrays & Constants
You can store multiple values of the same type in arrays. Constants are used to define fixed values.
// Array Example
int numbers[5] = {10, 20, 30, 40, 50};
// Constant Example
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}