Arduino Ultrasonic Range finder

Arduino Ultrasonic Range finder
Arduino Ultrasonic Range finder

Want to add high-tech features to your next DIY project? Making your robot more aware of its surroundings? Maybe you just love ultrasonic sensors. Whatever your reason, Arduino ultrasonic range finder integration is simple. With a few standard components, some code, and this approach, you can ping things quickly. This DIY guide will help you set up an Arduino ultrasonic range finder. Learn how the technology works, how to build the circuit, upload the code, and measure distances accurately. The tech may terrify you, but this project is fun, simple, and excellent for makers of all experience levels trying ultrasonic sensing.

Introduction to Ultrasonic Sensors

Ultrasonic sensors measure distances and detect things using sound waves. They send ultrasonic sound pulses that bounce off nearby objects and return to the sensor. The sensor measures the object’s distance by measuring the echoes’ return time.

How Do They Work?

Ultrasonic range finders use transducers to transform electrical information into sound waves and vice versa. Arduino signals cause the transducer to create a high-frequency ultrasonic sound pulse. It then listens for the echo and measures the time till it hears it. Knowing sound speed, the sensor can measure object distance.

Choosing an Ultrasonic Sensor

The HC-SR04 and HY-SRF05 are the most popular Arduino ultrasonic range finders. They work similarly but have distinct specs. The HC-SR04 has a 2–400 cm range and 3 mm precision. From 3 to 10 meters away, the HY-SRF05 can detect objects with 1 cm precision. The HC-SR04 is cheap and suitable for most DIY Arduino applications.

Setting Up the Hardware

Arduino ultrasonic range finders require four wires: Vcc, GND, Trigger, and Echo. Vcc and GND power the sensor. The Trigger pin emits the ultrasonic pulse when Arduino signals it. Echo pin delivers a signal to Arduino when reflected sound is detected, allowing sensor to determine distance.

You can detect things and measure distances quickly if you understand ultrasonic range finders and how to set one up with your Arduino. These adaptable sensors enable interactive DIY creations.

How Ultrasonic Range Finders Work

Ultrasonic range finders measure distance using sound waves. They send ultrasonic pulses that bounce off things and return to the sensor. Range finders measure object distance by measuring echoes’ return times.

Transmitting Ultrasonic Pulses

A transducer generates ultrasonic sound waves at 40 kHz, beyond human hearing, in the range finder. A transducer turns electrical energy into sound waves that leave the sensor. Sound-speed ultrasonic pulses travel 343 meters per second.

Receiving the Echo

Ultrasonic pulses reflect off objects and echo back to the range finder. These ultrasonic echoes are detected by the transducer’s microphone. The pulse transmission to echo reception time is measured.

Calculating the Distance

Using sound speed and echo return time, the range finder may measure object distance using this formula:

Distance = Sound Speed x Time/2

To account for sound wave propagation to and from the object, time is divided by two. The range finder aggregates ultrasonic pulse distances to increase accuracy.


Uses for ultrasonic range finders are varied. Robots use them for obstacle detection and navigation. Smartphones with ultrasonic range finders detect motions and deliver haptic feedback. Garage door sensors, automated lighting controls, and vehicle parking assistance systems employ them. Ultrasonic range finders measure distances cheaply and non-contactly.

Parts Needed for an Arduino Ultrasonic Range Finder

Arduino ultrasonic range finders require a few components. These fundamental parts are available online and in electronics stores.

Ultrasonic Sensor

The range finder’s heart is an ultrasonic sensor like the HC-SR04. This sensor calculates distance by emitting ultrasonic pulses and measuring their reflection time. The HC-SR04 is cheap and ideal for this purpose.

Arduino Board

This project requires an Arduino board to run code. An Arduino Uno works well and is ideal for novices. The Arduino triggers the ultrasonic sensor and calculates distance from its data.

Jumper Wires

The Arduino board and ultrasonic sensor are connected using jumper wires. Male-to-male jumper wires connect the sensor to the Arduino. Most ultrasonic sensors have VCC, GND, Trigger, and Echo pins. Trigger and Echo pins connect to Arduino digital pins, while VCC and GND pins connect to power and ground.

Breadboard (Optional)

A breadboard can let you prototype your circuit before soldering. Breadboards allow component and wire connections without soldering. You can permanently solder the components after your range finder works.

These minimal parts are enough to make an Arduino-based ultrasonic range finder. Many Arduino examples and tutorials provide simple sensor code. Your DIY range finder will be up and running quickly!

Wiring Up the Ultrasonic Sensor to the Arduino

After gathering all the parts, wire them to your Arduino board. The ultrasonic sensor has Vcc, Trig, Echo, and Gnd pins.

Connecting the Power Pins

First, connect the ultrasonic sensor’s Vcc pin to the Arduino’s 5V pin and its Gnd pin to any Arduino Gnd pin. It powers the sensor.

Connecting the Trigger Pin

The Trig pin triggers the ultrasonic sensor. This pin triggers ultrasonic pulses from the sensor. Connect this pin to Arduino digital pin 7.

Connecting the Echo Pin

Sensor ultrasonic signals are output by the Echo pin. Connect this pin to Arduino digital pin 6. This pin measures the ultrasonic pulse reflection time to the sensor.

Adding the Code

After wiring, upload Arduino code to activate the sensor. The code declares pins 7 and 6 outputs and inputs. To activate the sensor, it sends a 10-microsecond pulse to the Trig pin. Use the Echo pin to measure the sensor’s ultrasonic reflection reception time. Time is used to compute centimeter distance.

The sensor reliably measures distance by measuring the time delay between emitted and received ultrasound pulses. The maximum range of most ultrasonic rangefinders is 4 meters or 13 feet. How cool that a little sensor can measure distances so precisely!

Your ultrasonic rangefinder is now ready to detect objects. Enjoy testing its capabilities! Any questions? Let me know.

Coding the Arduino to Read Distance Data

The Arduino sends and receives ultrasonic sensor data via pins. Jumper wires link the sensor to the Arduino board. VCC powers the sensor, Trig delivers the ultrasonic pulse, Echo receives it, and GND grounds it.

Connecting the Sensor

The Arduino’s VCC pin should be connected to 5V, GND to GND, Trig to digital pin 8, and Echo to digital pin 9. This completes hardware configuration.

Coding the Distance Measurement

Next, software. The Arduino calculates distance from echo pulse width using pulseIn(). First, set pins 8 and 9 to Trig and Echo:

const int trigPin = 8;  
const int echoPin = 9;   

Next, define the speed of sound as 344 meters/second:

#define SOUND_SPEED 0.0344 // meters per microsecond

In the setup(), set pin modes:

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 

In the loop(), send a pulse on the Trig pin:

digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

Measure the Echo pulse:

duration = pulseIn(echoPin, HIGH);

Calculate the distance in cm:

distance = duration * SOUND_SPEED/2;

Print the distance:

Serial.print("Distance: "); 
Serial.println(" cm");

Upload this code to Arduino and open Serial Monitor. It will print the centimeter distance. Put your hand or item in front of the sensor to witness the distance change.

A sensor and Arduino were used to build an ultrasonic range finder with simple programming. Have more questions? Let me know!


So there! Build an ultrasonic range finder from scratch with an Arduino, some simple electronics, and some programming. You learned to solder and program, and now you can measure distances for your next Arduino project. The world has many objects to discover. The options are unlimited for creating a robot that navigates obstacles, an automated garage door opener, or a backyard motion sensor. We’ve barely scraped DIY ultrasonic sensor possibilities. Take this project to the next level and develop something great! Be creative and joyful crafting.

Be the first to comment

Leave a Reply

Your email address will not be published.