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

💻 Programming Code

🎯 Lesson Objective

In this lesson, students will learn:

• How to program the Obstacle Avoiding Robot
• How the ESP32 measures distance using the ultrasonic sensor
• How the robot detects obstacles in front of it
• How the ESP32 controls the motors using the motor driver
• How the robot changes direction automatically

By the end of this lesson, students will be able to upload the program and run the Obstacle Avoiding Robot.


1️⃣ Complete Copy-Paste Code

Students can copy and paste the following program directly into Arduino IDE.

 
 
int trigPin = 5;
int echoPin = 18;

int motor1A = 26;
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;

long duration;
int distance;

void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);

Serial.begin(9600);
}

void loop()
{
distance = measureDistance();

Serial.print("Distance: ");
Serial.println(distance);

if(distance > 20)
{
moveForward();
}
else
{
avoidObstacle();
}

delay(100);
}

int measureDistance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration * 0.034 / 2;

return distance;
}

void moveForward()
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);

digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}

void moveBackward()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);

digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}

void turnLeft()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);

digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}

void avoidObstacle()
{
moveBackward();
delay(400);

turnLeft();
delay(500);
}

2️⃣ Program Logic

The robot follows this behavior:

1️⃣ The ultrasonic sensor measures the distance in front of the robot.
2️⃣ ESP32 reads the distance value.
3️⃣ If the distance is greater than 20 cm, the robot moves forward.
4️⃣ If the distance is less than 20 cm, the robot avoids the obstacle.
5️⃣ The robot moves backward and turns left.

This process repeats continuously.


3️⃣ Pin Declaration

 
int trigPin = 5;
int echoPin = 18;
 

These pins are used for the ultrasonic sensor.

• TRIG → sends ultrasonic signal
• ECHO → receives reflected signal


 
int motor1A = 26;
int motor1B = 27;
int motor2A = 14;
int motor2B = 12;
 

These pins control the motor driver inputs.

Motor mapping:

• motor1A & motor1B → Left motor
• motor2A & motor2B → Right motor


4️⃣ Variable Declaration

 
long duration;
int distance;
 

These variables store the distance measurement data.

duration stores the echo travel time.
distance stores the calculated distance in centimeters.


5️⃣ Setup Function Explanation

 
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
 

These lines configure the ultrasonic sensor pins.

• TRIG → OUTPUT
• ECHO → INPUT


 
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
 

These pins are configured as OUTPUT to control the motors.


 
Serial.begin(9600);
 

This starts serial communication so students can monitor the distance values.


6️⃣ Measuring Distance

The robot measures distance using the function:

 
int measureDistance()
 

Inside the function:

 
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

 

This sends a 10 microsecond ultrasonic pulse.


Next, the sensor waits for the echo signal.

 
duration = pulseIn(echoPin, HIGH);
 

This measures how long the echo signal remains HIGH.


Distance is calculated using the speed of sound.

 
distance = duration * 0.034 / 2;
 

Explanation:

Speed of sound = 0.034 cm per microsecond

The signal travels to the object and back, so we divide by 2.


7️⃣ Decision Making Logic

Inside the loop:

 
if(distance > 20)
{
moveForward();
}
else
{
avoidObstacle();
}
 

If the path is clear:

➡ Robot moves forward.

If an obstacle is detected:

➡ Robot performs obstacle avoidance.


8️⃣ Forward Movement Function

 
void moveForward()
{
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);

digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}

 

Both motors rotate forward and the robot moves straight.


9️⃣ Backward Movement Function

 
void moveBackward()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);

digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}

 

Both motors rotate backward.

The robot moves backward.


🔟 Turning Function

 
void turnLeft()
{
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);

digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}

 

Left motor moves backward and right motor moves forward.

This causes the robot to turn left.


1️⃣1️⃣ Obstacle Avoidance Function

 
void avoidObstacle()
{
moveBackward();
delay(400);

turnLeft();
delay(500);
}

 

When an obstacle is detected:

1️⃣ Robot moves backward.
2️⃣ Robot turns left.
3️⃣ Robot continues moving forward.


1️⃣2️⃣ Final Working Process

The Obstacle Avoiding Robot works as follows:

1️⃣ The ultrasonic sensor measures distance continuously.
2️⃣ ESP32 reads the distance value.
3️⃣ If no obstacle is detected, the robot moves forward.
4️⃣ If an obstacle is detected, the robot changes direction.
5️⃣ The robot continues navigating automatically.

This creates a fully autonomous obstacle avoiding robot.


1️⃣3️⃣ Common Mistakes

Students may face the following issues:

⚠ Sensor always showing 0 → check echo pin wiring
⚠ Robot not moving → check motor driver connections
⚠ Wrong distance readings → verify ultrasonic sensor placement
⚠ Robot not turning → check motor wiring

Always check the circuit connections before debugging the code.


🚀 What Happens Next

Now that the Obstacle Avoiding Robot program is uploaded, the final step is to test the robot and explore possible improvements.

Scroll to Top