0% found this document useful (0 votes)
5 views2 pages

Arduino Data Types Tutorial v3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Arduino Data Types Tutorial v3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Arduino Data Types – Tutorial & Examples

In Arduino programming, variables are containers for storing data. Each variable has a data type
that defines the kind of values it can hold, such as numbers, text, or true/false values. Below are the
most common Arduino data types, with declaration examples and usage.

1. Data Types with Declarations & Usage


int - Used for integer numbers. Declaration: int counter; Example: int counter = 100;
unsigned int - Same as int but only positive numbers. Declaration: unsigned int distance;
Example: unsigned int distance = 50000; long - Stores very large integers. Declaration:
long population; Example: long population = 7000000L; float - Stores decimal
numbers. Declaration: float voltage; Example: float voltage = 3.75; double - Double
precision floating point. On Uno, same as float. Declaration: double pi; Example: double pi =
3.141592; char - Stores a single character (ASCII). Declaration: char letter; Example: char
letter = 'A'; byte - Stores numbers from 0 to 255. Declaration: byte value; Example: byte
value = 200; boolean - Stores true or false. Declaration: boolean ledState; Example:
boolean ledState = true; String - Stores text. Declaration: String name; Example:
String name = "Arduino";

2. Example Codes
// Integer and Boolean Example
int counter = 0;
boolean ledOn = true;

void setup() {
Serial.begin(9600);
}

void loop() {
counter++;
Serial.print("Counter: ");
Serial.println(counter);
if (ledOn) {
Serial.println("LED is ON");
} else {
Serial.println("LED is OFF");
}
delay(1000);
}

// Float Example
float voltage = 5.0;
float resistance = 220.0;
float current;

void setup() {
Serial.begin(9600);
}

void loop() {
current = voltage / resistance;
Serial.print("Current: ");
Serial.println(current);
delay(2000);
}

3. Arrays & Their Use


An array is a collection of variables of the same data type stored under one name. Arrays are
useful when you want to store multiple values, like sensor readings or pin numbers. Declaration:
int numbers[5]; Initialization: int numbers[5] = {10, 20, 30, 40, 50}; You can
access elements using their index (starting from 0).
// Array Example
int numbers[5] = {10, 20, 30, 40, 50};

void setup() {
Serial.begin(9600);
}

void loop() {
for (int i = 0; i < 5; i++) {
Serial.print("Value at index ");
Serial.print(i);
Serial.print(" = ");
Serial.println(numbers[i]);
}
delay(2000);
}

4. Quick Reference – Data Types Summary


Type Size (Bytes) Range Example
int 2 -32,768 to 32,767 int x = 25;
unsigned int 2 0 to 65,535 unsigned int u = 40000;
long 4 -2,147,483,648 to 2,147,483,647 long big = 100000L;
float 4 ±3.4028235E+38 float pi = 3.14;
double 4 (Uno) Same as float (Uno) double val = 2.71828;
char 1 -128 to 127 / ASCII char c = 'B';
byte 1 0 to 255 byte b = 200;
boolean 1 true/false boolean flag = true;
String varies text String s = "Hello";

You might also like