กลับหน้าหลัก
views
Arduino Variables Basics: Data Types and Declaration
Last updated on

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";
Comparison diagram showing int, float, char, and boolean data types with their sizes and example values

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 int for values that shouldn’t change during execution
  • Use descriptive variable names like THRESHOLD instead 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

TypeSizeRangeUse When
boolean1 bittrue/falseStates, on/off flags
byte1 byte0-2558-bit unsigned data
char1 byte-128 to 127Single characters or ASCII
int2 bytes-32,768 to 32,767General whole numbers
unsigned int2 bytes0 to 65,535Positive-only whole numbers
long4 bytes-2,147,483,648 to 2,147,483,647Large whole numbers
unsigned long4 bytes0 to 4,294,967,295Large positive numbers
float4 bytes±3.4E+38Decimal numbers
double8 bytes (Due only)±1.7E+308High-precision decimals
StringvariableText stringsWorking 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 byte instead of int saves 1 byte per variable
  • Storing values 0-65535: use unsigned int instead of long saves 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:

  1. Use int as default for general purposes—it covers most values in typical projects
  2. Use float when you need decimal precision like temperature readings or analog signals
  3. Use unsigned when you only need positive values to maximize range
  4. Use descriptive names so code is understandable later
  5. 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

จ้างทำโปรเจคเลย

ความคิดเห็น

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

กำลังโหลดรีวิว...