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

📏 Ultrasonic Sensor (HC-SR04)

🎯 Lesson Objective

In this lesson, students will understand:

• What an ultrasonic sensor is
• How the HC-SR04 ultrasonic sensor works
• The pins and components of the ultrasonic sensor
• How the sensor measures distance
• How ultrasonic sensors are used in robotics projects

This lesson introduces an important sensor used for distance measurement and obstacle detection.


1️⃣ What is an Ultrasonic Sensor?

An ultrasonic sensor is a device that measures the distance between the sensor and an object using sound waves.

Unlike light sensors or infrared sensors, ultrasonic sensors work by sending out high-frequency sound waves that are not audible to humans.

These sound waves travel through the air, hit an object, and then reflect back to the sensor.

By calculating the time taken for the sound waves to return, the sensor can determine the distance to the object.

Ultrasonic sensors are widely used in robotics and automation systems because they provide reliable distance measurements.


2️⃣ The HC-SR04 Ultrasonic Sensor

One of the most commonly used ultrasonic sensors in robotics is the HC-SR04 ultrasonic sensor.

This sensor is popular because it is:

✔ Affordable
✔ Easy to use
✔ Accurate for short-distance measurement
✔ Compatible with most microcontrollers

The HC-SR04 can measure distances typically between:

📏 2 cm to 400 cm

This makes it suitable for robotics applications such as obstacle detection and automation systems.


3️⃣ Main Parts of the HC-SR04 Sensor

The HC-SR04 ultrasonic sensor contains two main circular components on the front.

🔊 Ultrasonic Transmitter

The transmitter emits ultrasonic sound waves into the environment.

These sound waves travel forward until they hit an object.


🎤 Ultrasonic Receiver

The receiver detects the sound waves that bounce back after hitting an object.

The time taken for the echo to return is used to calculate the distance.


4️⃣ HC-SR04 Pin Configuration

The ultrasonic sensor has four pins, each serving a specific function.

🔴 VCC

This pin supplies power to the sensor.

It is usually connected to 5V power supply.


⚫ GND

This pin is the ground connection and completes the electrical circuit.


🟡 TRIG (Trigger Pin)

The trigger pin is used to start the distance measurement.

When the microcontroller sends a short signal to this pin, the sensor emits ultrasonic waves.


🔵 ECHO (Echo Pin)

The echo pin receives the signal when the ultrasonic waves return after hitting an object.

The duration of the echo signal helps determine the distance.


5️⃣ How the Ultrasonic Sensor Measures Distance

The ultrasonic sensor measures distance using a simple principle based on time and sound speed.

The working process is as follows:

1️⃣ The microcontroller sends a short pulse to the TRIG pin.

2️⃣ The sensor transmits ultrasonic sound waves.

3️⃣ The sound waves travel through the air and hit an object.

4️⃣ The waves bounce back toward the sensor.

5️⃣ The ECHO pin outputs a signal that lasts for the time taken by the sound waves to return.

The microcontroller measures this time and calculates the distance using the speed of sound.


6️⃣ Applications of Ultrasonic Sensors

Ultrasonic sensors are widely used in many robotics and automation systems.

Some common applications include:

🚗 Obstacle detection in mobile robots
🚗 Parking assistance systems in cars
🗑 Smart dustbin lid detection
📦 Distance measurement systems
🏠 Home automation devices

Because ultrasonic sensors work using sound waves, they are effective in different lighting conditions.


7️⃣ Ultrasonic Sensor in Our Robotics Projects

In this course, the HC-SR04 ultrasonic sensor will be used in several projects.

Examples include:

🗑 Smart Dustbin Project

The ultrasonic sensor detects when a hand approaches the dustbin and signals the servo motor to open the lid.


🚗 Obstacle Avoiding Robot

The robot continuously measures the distance in front of it and changes direction if an obstacle is detected.


📡 Advanced Obstacle Avoiding Robot

Multiple ultrasonic sensors help the robot detect objects in different directions.


8️⃣ Advantages of Ultrasonic Sensors

Ultrasonic sensors offer several advantages for robotics projects.

✔ Accurate distance measurement
✔ Works in low-light conditions
✔ Simple interface with microcontrollers
✔ Affordable and widely available

These advantages make ultrasonic sensors a popular choice for robotics beginners.


🚀 What Happens Next

Now that you understand how the ultrasonic sensor detects distance and obstacles, the next step is to learn about another sensor used in robotics.

Scroll to Top