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

🧠 Logical Operators

🎯 Lesson Objective

In this lesson, students will understand:

• What logical operators are
• Why logical operators are used in programming
• The three main logical operators used in Arduino programming
• How logical operators combine multiple conditions
• How robots make complex decisions using logical operators

This lesson expands decision-making by allowing programs to check multiple conditions at the same time.


1️⃣ What are Logical Operators?

Logical operators are used to combine two or more conditions in a program.

They help the program evaluate complex decision-making situations.

Sometimes a robot needs to check more than one condition before performing an action.

For example:

• If an obstacle is detected AND the robot is moving → stop the robot.
• If the light is bright OR the switch is pressed → turn on a device.
• If the robot is NOT detecting an obstacle → continue moving.

Logical operators allow programs to handle these types of situations.


2️⃣ Types of Logical Operators

There are three main logical operators used in Arduino programming:

Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

Each operator checks conditions in a different way.


3️⃣ Logical AND (&&)

The AND operator checks whether both conditions are true.

If both conditions are true, the result becomes true.

If either condition is false, the result becomes false.

Syntax Example

if (condition1 && condition2) {
// execute code
}

Example

 
if (distance < 20 && robotMoving == true) {
stopRobot();
}
 

Explanation:

• The robot stops only if both conditions are true.
• If either condition is false, the robot continues its operation.


4️⃣ Logical OR (||)

The OR operator checks whether at least one condition is true.

If either condition is true, the result becomes true.

Syntax Example

 
if (condition1 || condition2) {
// execute code
}

Example

if (fireDetected || gasDetected) {
activateAlarm();
}

Explanation:

• If fire is detected OR gas is detected, the alarm activates.

Only one condition needs to be true for the action to occur.


5️⃣ Logical NOT (!)

The NOT operator reverses the logical value of a condition.

If the condition is true, the NOT operator makes it false.

If the condition is false, the NOT operator makes it true.

Syntax Example

 
if (!condition) {
// execute code
}

Example

 
if (!obstacleDetected) {
moveForward();
}

Explanation:

• If an obstacle is not detected, the robot continues moving forward.


6️⃣ Combining Logical Operators

Logical operators can be combined to create more advanced decision-making logic.

Example:

 
if (distance < 20 && !obstacleCleared) {
stopRobot();
}

In this case:

• The robot stops if an obstacle is close
• AND the obstacle has not been cleared

This allows robots to make more intelligent decisions.


7️⃣ Logical Operators in Robotics Projects

Logical operators are commonly used in robotics applications.

Examples include:

🚧 Obstacle avoiding robots
🔥 Fire detection systems
💡 Smart lighting systems
🤖 Autonomous robots

These operators allow robots to evaluate multiple sensor inputs simultaneously.


8️⃣ Example in a Robot System

Let’s consider an example robot scenario.

The robot should stop if:

• An obstacle is detected AND the robot is moving.

Example:

 
if (distance < 20 && motorRunning) {
stopRobot();
}

This ensures the robot stops only when both conditions are satisfied.


🚀 What Happens Next

Now that you understand how logical operators allow programs to evaluate multiple conditions, the next step is to learn how programs repeat instructions automatically.

Scroll to Top