Course Content
📘 MODULE 11 – Edge Avoiding Robot
📦 MODULE 12 – Smart Multi-Function Robot (Mega Project)
Arduino Hands-On Programming and Robotics Course

📘 Lesson P3 – Variables and Data Types in Arduino

🎯 Learning Objectives

After completing this lesson, students will be able to:

✅ Understand what variables are

✅ Understand why variables are needed

✅ Declare variables correctly

✅ Store and modify values

✅ Understand common Arduino data types

✅ Choose the correct data type for different situations

✅ Avoid common beginner mistakes


1. Introduction

Imagine you want Arduino to store:

  • Temperature = 30°C
  • Distance = 25 cm
  • LED Status = ON
  • Motor Speed = 150

Where will Arduino store these values?

The answer is:

Variables

Variables are one of the most important concepts in programming.

Without variables, programs cannot remember information.


2. What is a Variable?

A variable is a named storage location in memory that holds data.

Think of a variable as a labeled box.

Example:

📦 Temperature Box

Inside:

 
30
 

Arduino can access this value anytime using the variable name.


Real-Life Example

Imagine your school has lockers.

Each locker has:

  • A name/number
  • Stored items

Similarly:

Variables have:

  • Name
  • Stored Value

Example:

 
int age = 22;
 

Here:

  • Variable Name = age
  • Value = 22

3. Why Do We Need Variables?

Suppose an ultrasonic sensor measures distance.

The value changes continuously:

 
20 cm
25 cm
30 cm
15 cm
 

Arduino needs a place to store these changing values.

Variables provide that storage.

Without variables:

  • Sensor values cannot be stored
  • Calculations cannot be performed
  • Decisions cannot be made

4. Declaring a Variable

Creating a variable is called:

Variable Declaration

Syntax:

 
dataType variableName;
 

Example:

 
int distance;
 

This creates a variable named distance.


5. Assigning Values

After creating a variable, we can store data.

Example:

 
int distance;

distance = 25;
 

Now:

distance contains:

 
25
 

6. Declaration and Assignment Together

Most programmers write:

 
int distance = 25;
 

This:

  • Creates variable
  • Stores value

in a single line.


7. Variable Naming Rules

Variable names must follow rules.


Correct Names

 
temperature
distance
motorSpeed
studentAge
 

Incorrect Names

 
2distance
student age
motor-speed
 

These will generate errors.


Rules for Naming Variables

Rule 1

Must begin with:

  • Letter
  • Underscore

Rule 2

Cannot start with a number.

Wrong:

 
int 1value;
 

Rule 3

No spaces allowed.

Wrong:

 
int room temperature;
 

Rule 4

Avoid special symbols.

Wrong:

 
int temp@;
 

8. What are Data Types?

Different kinds of data require different storage.

Example:

  • Age = Whole Number
  • Temperature = Decimal Number
  • LED Status = True/False

Arduino provides data types for different situations.


9. Integer (int)

Most commonly used data type.

Stores whole numbers.

Example:

 
int age = 22;
 

Possible values:

 
0
10
50
100
-20
 

Not allowed:

 
22.5
15.7
 

Applications

  • Distance
  • Counter
  • Sensor Values
  • Scores

10. Float

Stores decimal numbers.

Example:

 
float temperature = 36.5;
 

Possible values:

 
12.5
25.75
3.14
 

Applications

  • Temperature
  • Voltage
  • Scientific Calculations

11. Character (char)

Stores a single character.

Example:

 
char grade = 'A';
 

Stores:

 
A
B
C
1
$
 

Only one character.


Applications

  • Menu Systems
  • Serial Communication

12. Boolean (bool)

Stores only two values.

 
true
false
 

Example:

 
bool ledState = true;
 

Used for decision making.


Applications

  • Button Pressed?
  • LED ON?
  • Sensor Active?

13. String

Stores text.

Example:

 
String name = "Shiv";
 

Stores multiple characters.


Applications

  • User Names
  • Messages
  • LCD Display Text

14. Common Data Types Table

Data Type Example
int 25
float 25.5
char ‘A’
bool true
String “Arduino”

15. Memory and Data Types

Different data types use different memory.

Data Type Memory
bool 1 Byte
char 1 Byte
int 2 Bytes
float 4 Bytes

Choosing the correct data type helps save memory.


16. Changing Variable Values

Variables can change during program execution.

Example:

 
int count = 0;

count = 1;

count = 2;
 

The value updates continuously.


17. Example Program

 
int age = 22;

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

Serial.println(age);
}

void loop()
{

}
 

Output:

 
22
 

18. Using Multiple Variables

 
int distance = 25;
float temperature = 30.5;
bool motorState = true;
 

Arduino can store different kinds of information simultaneously.


19. Variables in Real Projects

Ultrasonic Distance Meter

 
int distance;
 

Stores measured distance.


Temperature Monitoring

 
float temperature;
 

Stores temperature value.


LED Control

 
bool ledState;
 

Stores LED status.


20. Common Beginner Mistakes

Mistake 1

Using variable before declaration.

Wrong:

 
distance = 25;

int distance;
 

Mistake 2

Missing semicolon.

Wrong:

 
int distance = 25
 

Mistake 3

Using wrong data type.

Example:

 
int temp = 25.5;
 

Decimal part gets lost.


Mistake 4

Using spaces in variable names.

Wrong:

 
int room temp;
 

21. Best Practices

✅ Use meaningful names

Good:

 
temperature
distance
motorSpeed
 

Bad:

 
x
y
z
 

✅ Use appropriate data types


✅ Keep naming consistent


✅ Add comments when necessary


📊 Summary

In this lesson, we learned:

✅ What variables are

✅ Why variables are important

✅ Variable declaration

✅ Variable assignment

✅ Data types

✅ int

✅ float

✅ char

✅ bool

✅ String

Variables are the foundation of programming because they allow Arduino to store, process, and manipulate information.


📖 Key Terms

Variable

A named memory location used to store data.

Data Type

Defines the type of data stored.

int

Stores whole numbers.

float

Stores decimal numbers.

char

Stores a single character.

bool

Stores true or false.

String

Stores text.

Declaration

Creating a variable.

Assignment

Storing a value in a variable.


🎯 Quiz

1. What is a variable?

A. Function

B. Memory location for storing data ✅

C. Sensor

D. Pin


2. Which data type stores decimal values?

A. int

B. bool

C. float ✅

D. char


3. Which data type stores true or false?

A. String

B. bool ✅

C. float

D. int


4. Which statement is correct?

A. int distance = 25;

B. int 25 = distance;

C. int distance 25;

D. distance int = 25;


5. Which data type stores text?

A. char

B. int

C. String ✅

D. bool


🏠 Assignment

Task 1

Create variables for:

  • Name
  • Age
  • Temperature
  • Distance
  • LED Status

Task 2

Write a table showing all common Arduino data types and their uses.

Task 3

Explain the difference between int and float with examples.

Task 4

Create a simple program that stores your name and age and displays them in Serial Monitor.

Task 5

List five real-world applications where variables are used in Arduino projects.

Scroll to Top