Ultrasonic Sensor

Ultrasonic Sensor – Obstacle-Avoiding Robot
The HC-SR04 ultrasonic sensor is one of the most suitable and affordable sensors for a beginner obstacle-avoiding robot. It uses a 40 kHz ultrasonic signal to detect objects and calculates their distance from the robot by measuring the returning echo. The common HC-SR04 module has four pins—VCC, TRIG, ECHO and GND—and typically operates at 5V. Its stated measurement range is approximately 2 cm to 400 cm, making it suitable for detecting walls, furniture and other objects in front of a small robot. (Compoden)
For this DIY project, the HC-SR04 can be mounted at the front of the robot and connected to the Arduino. The Arduino sends a short trigger signal to the sensor, receives the echo response and calculates the distance. If the measured distance falls below a programmed threshold—for example, 20 cm—the Arduino can instruct the motor driver to stop the motors and turn the robot in another direction. This makes the sensor an important part of the robot’s autonomous decision-making system. (Arduino Docs)