Course Content
IoT Engineering Course using ESP32 Wifi Robots

📘Variables & Data Types


🎯 Lesson Objective

By the end of this lesson, students will:

  • Understand what a variable is

  • Understand how memory stores data

  • Learn different data types used in ESP32/Arduino

  • Understand size and range of data types

  • Learn when to use which data type

  • Understand common beginner mistakes


1️⃣ What is a Variable?

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

In simple words:

A variable is a container that stores data.

Example:

 
int temperature = 25;
 

Here:

  • temperature is the variable name

  • 25 is the value stored


2️⃣ Why Variables Are Important in IoT

IoT systems constantly handle data like:

  • Temperature values

  • Gas sensor readings

  • Water levels

  • Mode settings

  • Button states

All this information must be stored somewhere.

That storage is done using variables.


3️⃣ How Memory Works (Simple Understanding)

ESP32 has RAM (memory).

When you declare:

 
int temperature = 25;
 

The system:

  • Reserves memory space

  • Stores the number 25

  • Labels it as “temperature”

Whenever you use temperature,
ESP32 reads that memory location.


4️⃣ Variable Syntax

General format:

 
dataType variableName = value;
 

Example:

 
float voltage = 3.3;
bool relayState = true;
 

Structure:

dataType → What kind of data
variableName → Name you choose
value → Stored value


5️⃣ Common Data Types in ESP32 / Arduino


🔹 1. int (Integer)

Stores whole numbers.

Example:

 
int temperature = 30;
 

Range (32-bit ESP32):

  • -2,147,483,648 to 2,147,483,647

Used for:

  • Sensor values

  • Counters

  • Digital states


🔹 2. float (Decimal Numbers)

Stores decimal values.

Example:

 
float voltage = 3.3;
 

Used for:

  • Temperature with decimals

  • Voltage

  • Sensor calibration

Important:
Float uses more memory than int.


🔹 3. double

On ESP32, double is same as float (4 bytes).

In advanced systems, double gives higher precision.

For this course:
Use float.


🔹 4. char (Single Character)

Stores a single character.

Example:

 
char grade = ‘A’;
 

Used for:

  • Serial input

  • Character commands


🔹 5. bool (Boolean)

Stores only two values:

  • true

  • false

Example:

 
bool pumpState = true;
 

Used for:

  • ON/OFF state

  • Mode selection

  • Flag variables

Very important for automation logic.


🔹 6. String

Stores text.

Example:

 
String message = “Gas Detected”;
 

Used for:

  • Display messages

  • Blynk labels

  • Serial printing

Note:
String uses more memory.


6️⃣ Choosing the Right Data Type

Using correct data type is important.

Example:

Temperature:
Use float if decimal needed.

Gas sensor:
Use int (ADC value 0–4095).

Relay state:
Use bool.

Using wrong type wastes memory or causes errors.


7️⃣ Variable Naming Rules

Variable names:

✔ Must start with letter or underscore
✔ Cannot contain spaces
✔ Cannot use special symbols
✔ Cannot use reserved keywords

Correct:

 
int waterLevel;
bool manualMode;
 

Wrong:

 
int 1temp;
float gas-value;
 

8️⃣ Global vs Local Variables


🔹 Global Variable

Declared outside functions.

Accessible everywhere.

Example:

 
int temperature = 25;

void setup() {
}

void loop() {
}

 

Used when:
Data needed in multiple functions.


🔹 Local Variable

Declared inside a function.

Accessible only inside that function.

Example:

 
void loop() {
int value = 10;
}
 

Used for temporary storage.


9️⃣ Updating Variables

Variables can change value.

Example:

 
int counter = 0;

void loop() {
counter = counter + 1;
}
 

This increases counter each time loop runs.

Very important in IoT logic.


🔟 Real IoT Example

Smart AC Example:

 
float temperature = 28.5;
float setPoint = 30.0;
bool acState = false;

void loop() {
if (temperature > setPoint) {
acState = true;
} else {
acState = false;
}
}
 

Here:

temperature → sensor data
setPoint → user input
acState → system decision

All controlled by variables.


1️⃣1️⃣ Memory Size of Data Types (ESP32)

Data Type Size
bool 1 byte
char 1 byte
int 4 bytes
float 4 bytes
double 4 bytes
String dynamic

ESP32 has limited RAM.

Using correct data types improves performance.


1️⃣2️⃣ Common Beginner Mistakes

❌ Using float when int is enough
❌ Forgetting to initialize variable
❌ Declaring variable inside loop unnecessarily
❌ Using wrong data type
❌ Confusing = and ==

Remember:

= → Assignment
== → Comparison

Example:

 
if (temperature == 30)
 

Not:

 
if (temperature = 30)
 

1️⃣3️⃣ Constants

Sometimes values should not change.

Use:

 
const int ledPin = 2;
 

This prevents accidental changes.

Used for:

  • Pin numbers

  • Fixed thresholds


1️⃣4️⃣ Variable Scope Importance

Understanding scope helps in:

  • Avoiding bugs

  • Reducing memory usage

  • Writing clean code

Good programming = controlled variables.


📌 Lesson Summary

In this lesson, we learned:

  • What is a variable

  • How memory stores data

  • int, float, char, bool, String

  • Global vs local variables

  • Updating values

  • Choosing correct data types

  • Common mistakes

Variables are the backbone of logic.

Without variables → No IoT system works.

Scroll to Top