Ugenz Academy/IoT Tutorial

Variables Operators And Data Types

15 min read2,844 words

Reading progress

Keep learning

Variables, Operators and Data Types

Now we move from hardware to programming.

In the previous topic, we learned that GPIO pins allow the ESP32 to interact with the physical world. But the ESP32 needs somewhere to store sensor readings, numbers, states, text, and other information.

That's where variables and data types come in.


1. What Is a Variable?

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

Think of a variable as a labeled box.

Variable ┌─────────────┐ │ temperature │ │ 28 │ └─────────────┘

The name is:

temperature

The stored value is:

28

For example:

int temperature = 28;

This means:

Create a variable named temperature and store the integer value 28 in it.


2. Why Do We Need Variables?

Suppose a temperature sensor gives:

28°C

You could store it:

int temperature = 28;

Then use it later:

Serial.println(temperature);

Or make a decision:

if (temperature > 30) { // Turn fan ON }

Without variables, writing practical programs would become extremely difficult.


3. Basic Variable Structure

A variable declaration generally looks like:

data type + variable name + value

Example:

int age = 20;

Here:

int → Data type age → Variable name 20 → Value

Another example:

float temperature = 28.5;

Here:

float → Data type temperature → Variable name 28.5 → Value

4. Data Types

A data type tells the ESP32 what kind of data a variable stores.

Common types include:

int float double char bool String

There are also unsigned integer types and fixed-width integer types that are useful when precise memory size matters.


5. int

int is commonly used for whole numbers.

Examples:

int age = 20; int count = 10; int temperature = 28; int sensorValue = 1500;

There is no decimal part.

10 25 100 -5

6. Integer Example

Suppose you have a people counter:

int people = 0;

When someone enters:

people = people + 1;

Now:

people = 1

Another person enters:

people = people + 1;

Now:

people = 2

This is exactly how variables are used in practical ESP32 projects.


7. float

Use float when you need decimal values.

Example:

float temperature = 28.5; float humidity = 65.7; float voltage = 3.3;

This is useful for:

  • Temperature
  • Humidity
  • Voltage
  • Distance
  • Calculations
  • Sensor measurements

For example:

float distance = 25.6;

8. double

double is used for floating-point values with potentially higher precision.

Example:

double value = 3.1415926535;

On ESP32, double generally provides higher precision than float, unlike some older microcontroller platforms where double may have the same precision as float.

For many basic sensor projects:

float

is sufficient.


9. bool

bool represents a logical state.

It has two values:

true false

Example:

bool lightOn = true;

Another:

bool motionDetected = false;

This is extremely useful for IoT projects.

For example:

if (motionDetected) { // Turn light ON }

10. char

char stores a single character.

Example:

char grade = 'A';

Notice the single quotes:

'A'

not:

"A"

Examples:

char letter = 'U'; char symbol = '#'; char digit = '5';

11. String

String can store text.

Example:

String name = "Uday";

Another example:

String message = "Welcome Home";

You can print it:

Serial.println(message);

Output:

Welcome Home

Strings are useful for:

  • Messages
  • Wi-Fi names
  • Status information
  • User commands
  • HTTP data
  • JSON-related processing

However, in memory-constrained embedded systems, excessive dynamic String usage can sometimes contribute to heap fragmentation. For larger or long-running projects, fixed-size character arrays (char[]) can be preferable in some situations.


12. Character vs String

This is a common beginner confusion.

Character

One character:

char letter = 'A';

String

Multiple characters:

String name = "Uday";

Think:

'A' ↓ One character "Uday" ↓ Multiple characters

13. Constants

Sometimes you don't want a value to change.

Use:

const

Example:

const int LED_PIN = 25;

Now you shouldn't change:

LED_PIN

during the program.

This is useful for pin definitions:

const int LED_PIN = 25; const int BUTTON_PIN = 18; const int RELAY_PIN = 26;

Your code becomes easier to understand.


14. Variables Can Change

Example:

int count = 0;

Later:

count = 1;

Later:

count = 2;

So:

count ↓ 0 → 1 → 2 → 3 → 4

That's why it is called a variable.


15. Assignment Operator =

The = operator assigns a value.

Example:

int x = 10;

means:

Put 10 into x.

Then:

x = 20;

means:

Replace the value of x with 20.

It does not mean mathematical equality in this context.


16. Arithmetic Operators

ESP32 programming uses normal arithmetic operators.

Addition

+

Example:

int result = 10 + 5;

Result:

15

Subtraction

-
int result = 10 - 5;

Result:

5

Multiplication

*
int result = 10 * 5;

Result:

50

Division

/
int result = 10 / 5;

Result:

2

Modulus

%

It gives the remainder.

int result = 10 % 3;

Result:

1

Because:

10 ÷ 3 = 3 remainder 1

17. Increment Operator

You will frequently use:

++

Example:

int count = 0; count++;

Now:

count = 1

Again:

count++;

Now:

count = 2

This is extremely useful for counters.


18. Decrement Operator

Use:

--

Example:

int count = 5; count--;

Result:

4

So:

++ → increase by 1 -- → decrease by 1

19. Compound Assignment Operators

Instead of:

count = count + 5;

you can write:

count += 5;

Similarly:

count -= 5; count *= 5; count /= 5;

Example:

int count = 10; count += 2;

Now:

12

20. Comparison Operators

Comparison operators are used when you want to compare values.

Equal to

==

Example:

if (temperature == 30) { }

Means:

Is temperature equal to 30?


Not equal

!=
if (temperature != 30) { }

Means:

Is temperature not equal to 30?


Greater than

>
if (temperature > 30) { }

Less than

<
if (temperature < 30) { }

Greater than or equal

>=

Less than or equal

<=

21. = vs ==

This is one of the most important beginner concepts.

=

Assignment:

temperature = 30;

Means:

Set temperature to 30.

==

Comparison:

temperature == 30

Means:

Is temperature equal to 30?

Remember:

= → Assign == → Compare

22. Logical Operators

Logical operators allow you to combine conditions.

Three important ones are:

&& || !

23. AND &&

Both conditions must be true.

Example:

if (temperature > 30 && humidity > 70) { // Alert }

Meaning:

Temperature > 30 AND Humidity > 70

Both must be true.

TRUE && TRUE → TRUE TRUE && FALSE → FALSE FALSE && TRUE → FALSE FALSE && FALSE → FALSE

24. OR ||

At least one condition must be true.

Example:

if (motionDetected || doorOpen) { // Security alert }

Meaning:

Motion detected OR Door open

Truth table:

TRUE || TRUE → TRUE TRUE || FALSE → TRUE FALSE || TRUE → TRUE FALSE || FALSE → FALSE

25. NOT !

! reverses a Boolean value.

Example:

bool lightOn = true;

Then:

!lightOn

is:

false

Similarly:

!false

becomes:

true

26. Practical Example — Smart Home

Suppose:

float temperature = 32.5; bool motionDetected = true; bool lightOn = false;

You can make decisions:

if (temperature > 30) { lightOn = true; }

Or:

if (temperature > 30 && motionDetected) { Serial.println("Room is hot and occupied"); }

This combines:

  • Variable
  • float
  • bool
  • Comparison
  • Logical operator
  • if condition

These concepts are the foundation of IoT programming.


27. Type Conversion

Sometimes you need to convert one data type to another.

For example:

int value = 10; float result = value;

Now:

value → 10 result → 10.0

You can also explicitly cast:

float result = (float)10 / 3;

This gives approximately:

3.333...

instead of integer division.


28. Important Integer Division Example

Look at:

int result = 5 / 2;

The result is:

2

not:

2.5

because both values are integers.

But:

float result = 5.0 / 2.0;

gives:

2.5

This distinction is very important in sensor calculations.


29. Memory Size Matters

Every variable consumes memory.

For example:

Variable ↓ Memory ↓ Stored value

Different data types use different amounts of memory.

On ESP32, common types have typical sizes such as:

TypeTypical Size
bool1 byte*
char1 byte
int4 bytes
float4 bytes
double8 bytes
long4 bytes
long long8 bytes

*Actual object/storage behavior can depend on compiler/layout; the important point is that bool represents a Boolean value and its storage can vary by context.

For exact-width programming, you can use types such as:

int8_t uint8_t int16_t uint16_t int32_t uint32_t int64_t uint64_t

These are especially useful in embedded programming when exact integer sizes matter.


30. Signed vs Unsigned

An integer can be:

Signed

Can represent positive and negative values.

int temperature = -5;

Unsigned

Represents non-negative values.

unsigned int count = 100;

If a value can never be negative, an unsigned type may sometimes be appropriate.

For example:

uint32_t milliseconds;

can be useful for time values.


31. Variable Naming Rules

A variable name can contain:

  • Letters
  • Numbers
  • Underscores

But it shouldn't start with a number.

Good:

temperature sensorValue motor_speed count1

Bad:

1temperature

Avoid confusing names like:

a x abc

in larger projects.

Prefer:

temperature humidity motorSpeed doorState

Good names make your program easier to understand.


32. Case Sensitivity

C++ is case-sensitive.

These are different variables:

temperature Temperature TEMPERATURE

For example:

int temperature = 25;

is not the same as:

int Temperature = 25;

Be consistent with naming.


33. Local Variables

A variable declared inside a function generally has local scope.

Example:

void setup() { int value = 10; }

value is available within that scope.

You can't generally use it elsewhere:

void loop() { Serial.println(value); // Not available here }

34. Global Variables

A variable declared outside functions can be used by multiple functions, subject to its scope.

Example:

int sensorValue = 0; void setup() { Serial.begin(115200); } void loop() { sensorValue = 100; Serial.println(sensorValue); }

Here sensorValue is global.

This is common in ESP32 projects.


35. const Variables

For values that shouldn't change:

const int LED_PIN = 2;

This is better than repeatedly writing:

2

throughout your code.

For example:

const int LED_PIN = 2; void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { digitalWrite(LED_PIN, HIGH); }

Now it's immediately obvious that LED_PIN refers to the LED.


36. #define

You may also see:

#define LED_PIN 2

This is a preprocessor macro.

It is common in Arduino code, but for simple constants, modern C++ style often prefers:

const int LED_PIN = 2;

or:

constexpr int LED_PIN = 2;

Both approaches are common.


37. Sensor Example

Imagine a DHT sensor.

It gives:

Temperature = 28.6°C Humidity = 65.2%

You might store:

float temperature = 28.6; float humidity = 65.2;

Then:

if (temperature > 30) { Serial.println("High Temperature"); }

And:

if (humidity > 70) { Serial.println("High Humidity"); }

This is the basic pattern behind many ESP32 sensor applications.


38. People Counter Example

Suppose your ESP32 counts people entering a room.

Start:

int peopleCount = 0;

Someone enters:

peopleCount++;

Someone leaves:

peopleCount--;

Display:

Serial.println(peopleCount);

You could also prevent negative values:

if (peopleCount > 0) { peopleCount--; }

Now variables and conditions work together.


39. Practical Smart-Home Example

Let's combine several data types:

const int LIGHT_PIN = 25; int peopleCount = 0; float temperature = 28.5; bool securityArmed = true; String roomName = "Living Room";

We now have:

LIGHT_PIN → constant integer peopleCount → integer temperature → floating point securityArmed → Boolean roomName → text

The ESP32 can use all of these to control the system.


40. Complete Example

const int LIGHT_PIN = 25; int peopleCount = 2; float temperature = 31.5; bool securityArmed = true; String roomName = "Living Room"; void setup() { Serial.begin(115200); pinMode(LIGHT_PIN, OUTPUT); } void loop() { if (temperature > 30 && peopleCount > 0) { digitalWrite(LIGHT_PIN, HIGH); Serial.println("Room occupied and temperature is high."); } else { digitalWrite(LIGHT_PIN, LOW); } Serial.println(roomName); Serial.println(temperature); Serial.println(peopleCount); Serial.println(securityArmed); delay(1000); }

This small program already demonstrates several important programming concepts.


41. Think of Data Like This

Imagine your ESP32 has a storage cabinet:

ESP32 MEMORY │ ┌──────────────┼──────────────┐ │ │ │ ↓ ↓ ↓ temperature peopleCount security 28.5 5 true float int bool

Each variable has:

Name Type Value

For example:

temperature │ ├── Type: float └── Value: 28.5

42. Operators — Quick Summary

Arithmetic

+ Addition - Subtraction * Multiplication / Division % Remainder ++ Increment -- Decrement

Assignment

= Assign += Add and assign -= Subtract and assign *= Multiply and assign /= Divide and assign

Comparison

== Equal != Not equal > Greater than < Less than >= Greater/equal <= Less/equal

Logical

&& AND || OR ! NOT

43. The Most Important Difference

Remember:

=

means:

Store/assign

while:

==

means:

Compare

Example:

temperature = 30;

changes the value.

But:

temperature == 30

asks a question.


44. How This Connects to ESP32 Hardware

Now connect this topic with the previous one.

Suppose:

PIR Sensor ↓ GPIO ↓ digitalRead() ↓ bool motionDetected ↓ if condition ↓ digitalWrite() ↓ Relay ↓ Light

Programming and hardware now work together:

Physical World ↓ GPIO ↓ Variable ↓ Operator ↓ Decision ↓ GPIO ↓ Physical World

This is the fundamental programming cycle of an ESP32.


45. Key Points to Remember

  1. A variable stores data.
  2. A data type tells the program what kind of data it is.
  3. int → whole numbers.
  4. float → decimal numbers.
  5. double → higher-precision floating-point values.
  6. bool → true or false.
  7. char → one character.
  8. String → text.
  9. const → value intended not to change.
  10. = → assignment.
  11. == → comparison.
  12. && → AND.
  13. || → OR.
  14. ! → NOT.
  15. ++ increases a value by 1.
  16. % gives the remainder.
  17. Variable scope determines where a variable can be accessed.
  18. Data types determine how data is represented and stored.
  19. Variables allow sensor readings and device states to be stored and processed.
  20. Operators allow the ESP32 to calculate and make logical decisions.

The big picture

SENSOR ↓ Read the value ↓ VARIABLE ↓ OPERATOR ↓ DECISION ↓ OUTPUT ↓ LED / RELAY / MOTOR

This is the programming foundation you'll use throughout the rest of the ESP32 roadmap.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy