Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

📦 Variables and Data Types

🎯 Lesson Objective

In this lesson, students will understand:

• What variables are in programming
• Why variables are important in robotics programming
• Different types of data that programs handle
• Common data types used in Arduino programming
• How variables are used in robotics projects

This lesson introduces the concept of variables, which are used to store and manage information in a program.


1️⃣ What is a Variable?

A variable is a named storage location in memory used to store data.

In simple terms, a variable is like a container that holds information which can be used or changed during the execution of a program.

For example, a program may store:

• Sensor readings
• Motor speed values
• Distance measurements
• System states

These values are stored in variables so the program can use them later.

For example:

A robot might store the distance measured by an ultrasonic sensor in a variable.

The program then uses this value to decide whether the robot should move forward or turn.


2️⃣ Why Variables Are Important

Variables allow programs to store and manipulate information dynamically.

Without variables, programs would not be able to:

📏 Store sensor values
⚙️ Track motor states
🔁 Perform calculations
🤖 Make decisions

In robotics systems, variables help the program respond to real-time changes in the environment.

For example:

A robot might constantly update a variable containing the distance to an obstacle.

If the distance becomes too small, the robot changes direction.


3️⃣ Declaring a Variable

Before a variable can be used in a program, it must be declared.

Declaring a variable tells the microcontroller:

• The name of the variable
• The type of data it will store

Example:

 
int distance;
 

In this example:

int represents the data type
distance is the name of the variable

This variable can now store integer values such as sensor readings.


4️⃣ What are Data Types?

A data type defines the type of information a variable can store.

Different types of data require different storage formats.

Common examples include:

• Numbers
• Decimal values
• Characters
• Logical values (true or false)

Using the correct data type helps ensure that the program runs efficiently and correctly.


5️⃣ Common Data Types in Arduino Programming

Several data types are commonly used in robotics programs.

🔢 int (Integer)

The int data type is used to store whole numbers.

Examples:

 
int speed = 100;
int distance = 25;
 

Integers are often used for:

• Sensor values
• Motor speeds
• Counters


🔢 float

The float data type stores decimal numbers.

Example:

 
float temperature = 26.5;
 

This data type is useful when working with sensors that produce decimal values.


🔤 char

The char data type stores a single character.

Example:

 
char grade = 'A';
 

This type is rarely used in basic robotics projects but is useful in communication systems.


✔️ bool (Boolean)

The bool data type stores logical values.

It can only store two possible values:

true
false

Example:

 
bool obstacleDetected = true;
 

Boolean variables are often used in decision-making conditions.


6️⃣ Assigning Values to Variables

After declaring a variable, we can assign values to it.

Example:

 
int speed = 150;
 

Here:

speed is the variable name
150 is the value assigned to the variable

The program can later modify this value if necessary.

For example:

 
speed = 200;
 

The variable now stores the new value.


7️⃣ Variables in Robotics Projects

In robotics programming, variables are used in many situations.

Examples include:

📏 Storing distance measured by an ultrasonic sensor
⚙️ Storing motor speed values
🌡 Storing temperature readings
💡 Storing light intensity values

By storing and updating these values, the robot can analyze its environment and make decisions.


🚀 What Happens Next

Now that you understand how variables store information in a program, the next step is to learn how the microcontroller interacts with external devices.

Scroll to Top