Skip to content

Obstacle Avoidance Master

Obstacle avoidance is an important intelligent function of drones. In this project, you will learn how to use ultrasonic sensors to implement front obstacle avoidance.

  • Ultrasonic sensor principles
  • Sensor interface programming
  • Conditional logic design
  • Real-time control
Item Quantity Notes
ESP32 Drone 1 -
HC-SR04 Ultrasonic Module 1 Distance measurement sensor
Breadboard, Dupont Lines Several For connections

Connect HC-SR04 to ESP32:

HC-SR04 ESP32
VCC 5V
GND GND
Trig GPIO 18
Echo GPIO 19

Extract obstacle_avoid.zip and open with VS Code.

Step 3: Write Distance Measurement Function

Section titled “Step 3: Write Distance Measurement Function”

Open ultrasonic.c and implement ultrasonic_get_distance function:

float ultrasonic_get_distance(void) {
// 1. Send 10us trigger signal
gpio_set_level(TRIG_PIN, 1);
ets_delay_us(10);
gpio_set_level(TRIG_PIN, 0);
// 2. Measure Echo high level duration
uint32_t start = 0, end = 0;
while (!gpio_get_level(ECHO_PIN)) start = esp_timer_get_time();
while (gpio_get_level(ECHO_PIN)) end = esp_timer_get_time();
// 3. Calculate distance: sound speed 340m/s, distance = time * speed / 2
float distance = (end - start) * 0.034 / 2;
return distance;
}

Open flight_control.c and add to the main loop:

float distance = ultrasonic_get_distance();
if (distance < 50) { // Distance less than 50cm
if (distance < 20) { // Distance less than 20cm
// Emergency brake
motorsSetRatio(MOTOR_M1, 0);
motorsSetRatio(MOTOR_M2, 0);
motorsSetRatio(MOTOR_M3, 0);
motorsSetRatio(MOTOR_M4, 0);
} else {
// Slow down and turn left
motorsSetRatio(MOTOR_M1, current_ratio * 0.5);
motorsSetRatio(MOTOR_M2, current_ratio * 0.8);
motorsSetRatio(MOTOR_M3, current_ratio * 0.5);
motorsSetRatio(MOTOR_M4, current_ratio * 0.8);
}
}

Move obstacles (e.g., cardboard box) in front of the drone and observe if it can automatically avoid them.

  • Check sensor connections
  • Adjust sound speed parameter
  • Optimize code execution frequency
  • Reduce unnecessary delays

Congratulations! You have implemented the drone’s obstacle avoidance function, which is an important step towards intelligence!

In the next project, you will learn how to use the camera to implement simple visual recognition functions.

First published atdrone.imagrove.com/intermediate/obstacle-avoidance/Please credit Imagrove and link back to this page when quoting or republishing.

Questions or issues with this tutorial?

Sign in with GitHub to leave a comment. If a step fails for you, tell us — we verify it and update the tutorial.

ImagroveMore ESP32 tutorials · ESP32 IoT Tutorial

© 2026 ESP32 Drone. All rights reserved.