Ultrasonic Sensor With Arduino - Lesson #16
by lucascreator in Circuits > Arduino
51 Views, 2 Favorites, 0 Comments
Ultrasonic Sensor With Arduino - Lesson #16
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
For today's project, you'll need:
- 1 x Arduino UNO
- 1 x I/O expansion shield
- 1 x Ultrasonic sensor
- 1 x I2C LCD module
- A few connecting wires
YouTube Tutorial
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
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:
- The transmitter emits a short burst of ultrasonic sound.
- The sound wave travels through the air.
- If an object is in front of the sensor, the wave hits it and reflects back.
- 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
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
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:
- Robot obstacle detection
- Liquid level measurement
- Presence detection systems
- Smart parking systems
For this project, you'll need:
- Arduino UNO (or compatible board)
- I/O expansion shield (optional but helpful)
- Ultrasonic sensor
- I2C LCD module
- 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:
- SCL → A5
- SDA → A4
- GND → GND
- VCC → 5V
After that, attach the ultrasonic sensor like this:
- Trig → Digital Pin 9
- Echo → Digital Pin 10
- GND → GND
- 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
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.
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.
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.
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.
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.
Why This Project Matters
This project demonstrates more than just reading a sensor. It shows how:
- Sound propagates (Physics)
- Signal timing (Electronics)
- 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
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.