Ultrasonic Sensor With Arduino - Lesson #16

by lucascreator in Circuits > Arduino

51 Views, 2 Favorites, 0 Comments

Ultrasonic Sensor With Arduino - Lesson #16

instructables-cover(1).png
1.jpg

Measuring distance without touching anything might sound like advanced robotics, but it's actually one of the most common sensing techniques in electronics.

Car parking assistance systems, obstacle-avoiding robots, automatic doors, and many industrial machines rely on the same technology: ultrasonic distance sensing.

This is lesson 16 of my 24-part series called Arduino for Beginners.

In this tutorial, we will build a complete distance measurement system using an Arduino and an ultrasonic sensor.

The measured distance will be displayed in real time on an LCD screen. While the circuit itself is simple, the concepts behind it are powerful and widely used in real engineering systems.

You will learn how ultrasonic sensors work, how to connect one to an Arduino, how time is converted into distance, and how software filters unreliable readings.

By the end, you won't just have a working device - you'll understand the physics and logic that make it possible.

Supplies

2.jpg
3.jpg
4.jpg
5.jpg
6.jpg

For today's project, you'll need:

  1. 1 x Arduino UNO
  2. 1 x I/O expansion shield
  3. 1 x Ultrasonic sensor
  4. 1 x I2C LCD module
  5. A few connecting wires

YouTube Tutorial

Ultrasonic Sensor With Arduino - Lesson #16

I've recently posted a tutorial about this project on YouTube explaining everything you can read on this article. You can watch it right above.

How Ultrasonic Distance Measurement Works

7.jpg
8.jpg
9.jpg

Ultrasonic sensors measure distance using sound waves, but at frequencies higher than humans can hear.

Human hearing typically ranges up to about 20 kHz. Ultrasonic sensors operate above this range, which is why their signals are inaudible.

If you look at the front of a typical ultrasonic module, you'll see two round metal cylinders. One is a transmitter, and the other is a receiver. Their roles are simple:

  1. The transmitter emits a short burst of ultrasonic sound.
  2. The sound wave travels through the air.
  3. If an object is in front of the sensor, the wave hits it and reflects back.
  4. The receiver detects the returning wave.

This process is called echo ranging or time-of-flight measurement.

Sound travels through air at approximately 343 meters per second at room temperature. Instead of directly measuring distance, the Arduino measures how long the sound wave takes to leave the sensor, reach an object, and return.

Distance in physics is:

Distance = Speed * Time

However, the measured time includes the trip to the object and back again. That means the actual one-way distance is half of the total travel path. The code accounts for this automatically - as we'll see later.

A simple mental model is this: the sensor "shouts", then listens for its own echo. A fast echo means a nearby object. A slow echo means the object is farther away.

The Arduino measures time in microseconds, which is precise enough to detect very small distance changes.

Sponsor

20250609_162406.jpg
20250609_162454.jpg

Before we build the project, I'd like to take a moment to thank today's sponsor: DFRobot.

DFRobot is one of the leading global providers of open-source hardware for makers, students, and engineers.

Throughout this entire Arduino for Beginners series, I'm using the MindPlus Arduino Coding Kit.

This kit includes an Arduino board, sensors, jumper wires, and everything needed to follow along with the lessons in the series.

One of the main reasons I like this kit so much is that it makes the learning process easir for beginners.

Instead of worrying about compatibility issues or missing components, you can focus entirely on what actually matters: understanding how electronics work and how to build things with them.

If you want to follow along more easily and get the most out this series, I highly recommend checking out a kit like this one.

Thank you again to DFRobot for sponsoring this project and helping make STEM education more accessible to everyone.

Project

10.jpg
11.jpg
12.jpg
14.png
13.jpg
15.jpg
16.jpg

In this project, the ultrasonic sensor continuously measures the distance to whatever is in front of it.

That distance is then displayed on an LCD screen. This setup forms the basis of many real applications such as:

  1. Robot obstacle detection
  2. Liquid level measurement
  3. Presence detection systems
  4. Smart parking systems

For this project, you'll need:

  1. Arduino UNO (or compatible board)
  2. I/O expansion shield (optional but helpful)
  3. Ultrasonic sensor
  4. I2C LCD module
  5. Jumper wires

Wiring the Circuit

Start by placing the I/O expansion shield on top of the Arduino if you have one.

Then, connect the I2C LCD according to this:

  1. SCL → A5
  2. SDA → A4
  3. GND → GND
  4. VCC → 5V

After that, attach the ultrasonic sensor like this:

  1. Trig → Digital Pin 9
  2. Echo → Digital Pin 10
  3. GND → GND
  4. VCC → 5V

If you need additional help, you can follow the schematic above.

After these connections, the hardware is ready.

Software Setup

Open the Arduino IDE and load the project code. It's available on the GitHub repository of this series.

Before uploading, make sure the DFRobot_RGBLCD1602 library is installed. This library is required to control the LCD module used in this build.

Once the library is installed, upload the code to the Arduino. If the wiring is correct, the LCD will begin showing distance readings immediately (as you can see in the last images above).

Code

17.png
18.png
19.jpg

Let's break down how the program works.

Variable and Pin Definitions

At the start of the code, we define constants and variables. This makes the program easier to read and modify.

// Ultrasonic sensor pins
const int trigPin = 9;
const int echoPin = 10;

long duration;
float distance;

const float MIN_DISTANCE = 5.0; // cm
const float MAX_DISTANCE = 100.0; // cm

setup() Function

The setup() function runs once when the Arduino powers on.

Here, the LCD is initialized so it can display text. The Trig pin is set as an output because it sends signals, and the Echo pin is set as an input because it receives signals.

void setup() {
// Initialize LCD
lcd.init();
lcd.setColorWhite();
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("Distance:");

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

loop() Function

The loop() function runs repeatedly and performs the measurement cycle.

First, the Trig pin is set LOW briefly to ensure a clean signal. Then, the code sends a very short HIGH pulse (around 10 microseconds). This tells the sensor to emit an ultrasonic burst.

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

Next, the Arduino listens on the Echo pin. A built-in timing function measures how long the Echo pin stays HIGH. This duration represents the total travel time of the sound wave.

The program then calculates the distance using the time value and the speed of sound. The result is converted into centimeters.

// Read echo time (timeout increased for safety)
duration = pulseIn(echoPin, HIGH, 60000);

// If no echo received
if (duration == 0) {
lcd.clear();
lcd.print("No object found");
delay(300);
return;

} else {
// Convert to distance (cm)
distance = duration * 0.034 / 2;

// Continue below...

Defining a Valid Detecting Zone

Ultrasonic sensors are not perfect. At very short distances, the sensor may not detect the echo correctly. At long distances, the echo can become too weak (see the last image above).

To improve reliability, the program defines a valid range - for example, from 5 cm to 100 cm.

If the measured distance falls inside this range, it is displayed on the LCD. If it falls outside, the program shows a message like "No object found".

This prevents false readings from being treated as real measurements.

In real-world engineering, this kind of filtering is essential. Without it, robots and automated systems could react to noise or invalid data.

// Check detection zone
if (distance < MIN_DISTANCE || distance > MAX_DISTANCE) {
lcd.clear();
lcd.print("No object found");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.setCursor(0, 1);
lcd.print(distance, 1);
lcd.print(" cm");
}
}

Why This Project Matters

This project demonstrates more than just reading a sensor. It shows how:

  1. Sound propagates (Physics)
  2. Signal timing (Electronics)
  3. Data processing (Programming)

work together to create an intelligent system.

The same principles scale to more advanced systems like drones, autonomous vehicles, and industrial automation.

If you need more help to understand Arduino coding, I wrote down a tutorial all about that. Don't forget to check it out.

Conclusion

21.jpg
22.jpg

You have built a complete non-contact distance measurement system using an Arduino, an ultrasonic sensor, and an LCD display.

Today you learned how ultrasonic waves reflect, how time-of-flight measurement works, and how software converts timing data into physical distance.

You also saw the importance of defining valid operating ranges to avoid unreliable data. These ideas are fundamental in real engineering applications.

With this foundation, you can now extend the project into obstacle-avoiding robots, interactive installations, or smart sensing systems. Understanding how sound, time, and code work together opens the door to building more intelligent and responsive hardware.

Thank you for reading this article. And before you leave, I recommend you read this one. I'm sure it'll help you improve your Arduino skills.