Arduino Variables Basics: Data Types and Declaration
Arduino Variables Basics: Data Types and Declaration
Variables are essential in Arduino programming. Whether you’re reading sensor values, controlling motors, or communicating with other devices, you’ll use variables to store and manipulate data.
Why Use Variables
Imagine writing a temperature monitoring sketch and using the raw number 25 scattered throughout. When you change sensors, you’d have to update every occurrence. Instead, using sensorThreshold = 25 lets you change the value in one place. Variables also make code readable, easier to debug, and reduce errors from typing wrong numbers.
Common Data Types in Arduino
1. int (Integer)
Stores whole numbers, 2 bytes. Most commonly used data type for general Arduino projects.
int sensorValue = 0;
int ledPin = 13;
int buttonState = 0;
Range: -32,768 to 32,767
2. float (Floating-point)
Stores decimal numbers, 4 bytes. Use when you need fractional precision.
float temperature = 25.5;
float voltage = 3.3;
float pi = 3.14159;
Range: ±3.4028235E+38 with 6-7 digits of precision
Note: Float calculations are slower on standard Arduino boards (not Due) because there’s no FPU hardware. If you don’t need decimals, use int for better performance.
3. char (Character)
Stores single characters or ASCII codes, 1 byte.
char grade = 'A';
char message = 'H';
Range: -128 to 127 or 0 to 255 (unsigned)
4. boolean
Stores logical values: only true (1) or false (0).
boolean isButtonPressed = false;
boolean ledOn = true;
5. long
Stores large whole numbers, 4 bytes.
long elapsedTime = 0;
long counter = 100000;
Range: -2,147,483,648 to 2,147,483,647
6. String
Stores text strings, multiple characters. Different from char which stores only one character.
String welcomeMessage = "Hello Arduino";
String statusText = "Sensor Active";
Type Modifiers
For integer types, you can add modifiers before the data type to change properties.
unsigned
Forces storage of positive values only, doubling the positive range.
unsigned int positiveValue = 500;
unsigned long bigPositive = 3000000;
Range examples:
- unsigned int: 0 to 65,535 (instead of -32,768 to 32,767)
- unsigned long: 0 to 4,294,967,295
short and long
Adjust the size of integer variables.
short smallNumber = 100;
long largeNumber = 5000000;
Variable Declaration Syntax
Basic format: {data_type} {variable_name};
int count; // Declaration without initial value, defaults to 0
int temperature = 30; // Declaration with initial value
Practical Code Examples
Reading Sensor and Controlling LED
const int SENSOR_PIN = A0; // Analog pin for sensor
const int LED_PIN = 13; // LED pin
const int THRESHOLD = 500; // Decision threshold
int sensorValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(SENSOR_PIN);
if (sensorValue > THRESHOLD) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
Serial.println(sensorValue);
delay(100);
}
Key points:
- Use
const intfor values that shouldn’t change during execution - Use descriptive variable names like
THRESHOLDinstead of scattered raw numbers
Assigning Variables Later
int a = 10;
int b = 20;
int sum;
sum = a + b; // sum now equals 30
boolean isActive = false;
isActive = !isActive; // isActive is now true
Declaring Multiple Variables
int x = 5, y = 10, z = 15;
float temp1 = 25.0, temp2 = 26.5;
Summary Table of Common Variable Types
| Type | Size | Range | Use When |
|---|---|---|---|
| boolean | 1 bit | true/false | States, on/off flags |
| byte | 1 byte | 0-255 | 8-bit unsigned data |
| char | 1 byte | -128 to 127 | Single characters or ASCII |
| int | 2 bytes | -32,768 to 32,767 | General whole numbers |
| unsigned int | 2 bytes | 0 to 65,535 | Positive-only whole numbers |
| long | 4 bytes | -2,147,483,648 to 2,147,483,647 | Large whole numbers |
| unsigned long | 4 bytes | 0 to 4,294,967,295 | Large positive numbers |
| float | 4 bytes | ±3.4E+38 | Decimal numbers |
| double | 8 bytes (Due only) | ±1.7E+308 | High-precision decimals |
| String | variable | Text strings | Working with text |
[image: Table comparing Arduino variable types showing size and range with visual symbols]
Memory Considerations
Arduino Uno has only 2 KB of SRAM. Using wrong data types wastes memory:
- Storing values 0-255: use
byteinstead ofintsaves 1 byte per variable - Storing values 0-65535: use
unsigned intinstead oflongsaves 2 bytes
// Memory-efficient declarations
byte ledState = 0; // instead of int ledState
unsigned int pixel = 40000; // instead of long pixel
[image: Diagram showing Arduino Uno SRAM memory and how different variable types consume memory]
Summary
Variables are fundamental to Arduino programming. Keep these principles in mind:
- Use int as default for general purposes—it covers most values in typical projects
- Use float when you need decimal precision like temperature readings or analog signals
- Use unsigned when you only need positive values to maximize range
- Use descriptive names so code is understandable later
- Mind your memory especially as projects grow more complex
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย