Arduino Ultrasonic distance sensor

Arduino Ultrasonic distance sensor
Arduino Ultrasonic distance sensor

Your Arduino Ultrasonic distance sensor  project requires exact distance readings, not estimates. Avoid guesswork using an ultrasonic sensor and Arduino board for distance measurements. This article explains how ultrasonic sensors determine distance using sound waves. We’ll connect a sensor to an Arduino and upload code to read it. Your distance measurements will be pinpoint accurate in no time. Arduino distance detection projects benefit from economical, easy-to-use ultrasonic sensors. This useful capability can be added with a few components. Step-by-step instructions for accuracy distance measurements with this flexible sensor are below.

How Ultrasonic Sensors Work

Ultrasonic sensors measure distance using sound waves. They use an ultrasonic pulse and listen for the target object’s echo. The sensor measures object distance by monitoring echo return time.

The Transmitter

The sensor has a transducer that transmits and receives. It sends a 40kHz ultrasonic pulse to measure. The pulse reflects off items en route.

The Receiver

When the emitted pulse hits an object, it bounces off and returns to the sensor. The receiver detects this reflected signal and measures the transmission-to-echo time. Time can be used to compute distance when sound speed is known.

Calculating Distance

The distance formula is: Distance = Sound Speed x Time/2

The equation halves time to account for the pulse’s journey to and from the item. At normal temperature, sound travels 343 m/s. If transmission and echo took 58,000 microseconds, the distance would be 343 x 0.000058 / 2 = 0.5 meters.


Ultrasonic sensors measure distance in robotics and automated systems. Commonly used in devices like:

  • Vehicle parking sensors
  • Sensors detect motion
  • Sensors for fluid levels
  • Near-by sensors
  • Positioning systems
  • Tools for distance measurement

A simple distance measuring gadget can be made with an ultrasonic sensor, Arduino, and a few components. Your next Arduino project can easily sense distance with the sensor.

Project Parts for Arduino Ultrasonic Sensor

Arduino ultrasonic distance sensors require a few components. These parts are easy to find and use for beginners.

Ultrasound Sensor Module

This project revolves around an HC-SR04 ultrasonic sensor module. This sensor detects object echoes from ultrasonic sound pulses. By detecting the time between emitted and echoed pulses, it can correctly measure target distance. This waterproof sensor measures 2–400 centimeters.

The Arduino Board

Arduino microcontrollers are needed to run ultrasonic sensor code. This project works well with an Arduino Uno or Nano. Arduino reads sensor distance measurements and controls other components or outputs them.

Jumper Cables

Jumper wires (dupont wires) link the ultrasonic sensor to Arduino. Male-to-male and male-to-female cables are needed for correct connections. Vcc, Trig, and Echo are the ultrasonic sensor power, trigger, and receiving pins. Vcc and ground pins power the sensor, while Trig and Echo pins control ultrasonic pulses via Arduino digital pins.

Optional breadboard

A breadboard can help you prototype Arduino projects. The breadboard connects wires and components without soldering. Place your Arduino and ultrasonic sensor on the breadboard and link them with jumper wires. Solder the components on a permanent circuit board when your project works. If you prefer direct component connection, a breadboard is not needed for this basic two-component project.

With these few pieces, you can build an Arduino ultrasonic distance sensor. Connecting and uploading the sensor code is easy. You’ll have a working ultrasonic range finder for projects quickly. Start measuring!

Wiring the HC-SR04 Ultrasonic Sensor

The HC-SR04 ultrasonic sensor is useful for distance and object detection. Start by wiring it to your Arduino.


The Arduino VCC pin should be connected to 5V and the GND pin to ground. This powers the sensor.

Trigger, Echo

The sensor sends and receives ultrasonic pulses via Trigger and Echo pins. Connect the Trigger pin to an Arduino digital pin like pin 12. Send the ultrasonic signal. Echo receives the signal and should be attached to pin 11 or another digital pin.

Setting up Code

Define the Trigger and Echo pins in your Arduino program. Set Trigger and Echo pins to OUTPUT and INPUT, respectively.

To measure distance, send a short 10us pulse to the Trigger pin to send the ultrasonic signal. Measure the signal-to-echo time with pulseIn(). Divide this time by 58 for centimeters or 148 for inches.

As an example:

Define trigPin 12 and echoPin 11.

In void setup(), pinMode(trigPin, OUTPUT) and pinMode(echoPin, INPUT) are used.

Loop function: // Send 10us pulse for trigger digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW

// Measure pulse duration from echo long duration = pulseIn(echoPin, HIGH);

To calculate distance, use: int cm = duration / 58;

Print distance

Serial.print(“cm”); Serial.println(“cm”);

This simple code gives accurate HC-SR04 ultrasonic sensor distance readings. The sensor angle and position can be adjusted to detect things in front of it or develop proximity detectors and collision avoidance systems. Play with your new ultrasonic sensor!

Sensor Testing and Calibration

After connecting your ultrasonic sensor to an Arduino, verify and calibrate it for reliable readings. ###Taking baselines Start by uploading a basic Arduino program that reads the sensor and prints the serial monitor distance. Place the sensor 30 cm from a flat wall and record the reading. Note sensor readings as you move it closer and farther from the wall.

Looking for discrepancies

Do the readings match? If not, examine wiring and connections. The sensor may need calibration for temperature and humidity changes. Calibration requires several readings at fixed distances and correction factor determination. If 30 cm measurements are always 32 cm, your correction factor is -2. Apply this adjustment factor to all readings.

Troubleshooting interference

The sensor’s ultrasonic pulses may ricochet off surfaces other than the target, causing inaccurate measurements. Test alternative sensor placements and angles and add sound-absorbing materials to the testing area. You may also need to filter code outliers. Experiment to find the best arrangement for accurate distance readings in your application.

Ultrasonic sensors can give precise and consistent data for many tasks, but they need calibration and troubleshooting. Taking the effort to test and configure your sensor will give your Arduino project reliable distance data. You’re ready to use the sensor in projects with the baseline readings and correction factor!

Fun Arduino Ultrasonic Sensor Projects

Ultrasonic sensors are versatile and made for fun DIY projects. Here are some starter ideas.

Robot Car

Create an obstacle-avoiding robot automobile. Place an ultrasonic sensor in front of the car to identify obstacles. To avoid collisions, program the Arduino to read sensor data and steer the automobile left or right. You can add numerous sensors for 360-degree vision.

Movement-Activated Device

Make a motion-activated light, fan, or buzzer. When someone enters the ultrasonic sensor’s range, the device turns on. Adjust sensor sensitivity to adjust device activation distance. Simple way to automate and smarten your house.

Tool for Distance Measurement

Build a simple distance meter with an ultrasonic sensor. Point the sensor toward the target from a stationary base. Arduino calculates distance from ultrasonic pulse echo time to sensor. The distance can be shown on an LCD screen. This interesting experiment shows how ultrasonic sensors estimate distance using sound waves.


Ultrasonic sensors can detect intruders in a DIY alarm system. Install the sensor in a high-traffic hallway or doorway. Tell the Arduino to buzz or light up when the sensor senses movements. The Arduino can transmit phone notifications via WiFi or Bluetooth. Our basic example shows how ultrasonic sensors can detect objects in security systems.

With ultrasonic sensors and Arduino, you can make all kinds of cool devices and tools with some simple components and a creative imagination. Enjoy your creations and see what you can create!


With an Arduino, ultrasonic sensor, and a few lines of code, you can make an affordable yet precise distance measurer. Pretty cool! The best part is that you can utilize this project to make a parking sensor, an alert system to keep people away, or even a room map. Endless possibilities! Its simplicity makes it beginner-friendly. Try it, be creative, and enjoy distance sensing! Always remember that tremendous power comes with responsibility. Use it wisely!

Be the first to comment

Leave a Reply

Your email address will not be published.