Ultrasonic Sensors – Tutorials and Resources

This article was last modified on January 2018.

A beginner in robotics or automation starts with easy projects such as detecting an object with an ultrasonic sensor. Such a sensor can be connected directly to a development board like Arduino and with just a few lines of code, the user can identify an object placed in front of the sensor. This is one of the most common applications for an ultrasonic sensor. But things can evolve to complex applications and require experience in the field.

There are applications where it is necessary to use a single board computer like Raspberry Pi and less of a microcontroller. There are ways to improve the accuracy of the sensors, including the ambient temperature in the sound wave velocity calculation. There are modules for autonomous robots that can be connected to ultrasonic sensors. For all these applications, I’ve been looking for the best tutorials and divided them according to the ultrasonic sensor used. The idea of the article is to ease the work of those using ultrasonic sensors.

To get you an idea of how an ultrasonic sensor works, I summarized in a few words the principle of functioning. The working principle of an ultrasonic sensor is simple and use high-frequency sound waves that are evaluated when the sensor received back the waves. To determine the distance between the sensor and object, the sensor measures the elapsed time between sending and receiving the waves.

In general, the ultrasonic sensors are used to make measurements in different environments where other sensor types can be affected by the surface, material, light, dust, or other noises.

In robotics and automation, the ultrasonic sensors are used in applications such as:

  • object detection
  • detect the position of an object
  • measure the distance between the sensor and an object
  • counting the objects which pass through the front of the sensor
  • tracking an object

The HC-SR04 Ultrasonic Sensor

The HC-SR04 is an inexpensive sensor (the price is around $3/piece) very easy to use with a microcontroller or a single board computer. It is by far the most common ultrasonic sensor in robotics or automation projects. The distance it detects objects is between 2 centimeters and 450 centimeters. The accuracy depends largely on outside noise and temperature compensation when calculating the sound wave velocity in the air. Few libraries have been created to make reading data much easier with Arduino. Also for SR04, noise reduction was achieved for high accuracy in distance detection. All these are found in the tutorials below.

  • Getting Started with the HC-SR04 Ultrasonic sensor – It’s an introduction tutorial that requires a HC-SR04 sensor and an Arduino board. In this tutorial are the steps to connect the sensor to the board and the Arduino code to read the data returned by the sensor.
  • Arduino – HC-SR04 ultrasonic distance sensor – In this tutorial, the user uses the “Ultrasonic.h” library to read the data returned by the sensor. With a single line of code, you can read the data returned by the sensor in your preferred unit of measurement.
  • HC-SR04 Ultrasonic Range Sensor on the Raspberry Pi – HC-SR04 can be used as well with any of the Raspberry Pi boards. In this tutorial, you find how to connect the sensor to Pi and how to display the distance measured by the sensor. The file that returns the distance read by the sensor is a Python file.
  • Improve ultrasonic range sensor accuracy – There is a simple way to improve the accuracy of measuring the distance returned by the sensor. In the formula for calculating the speed of sound waves emitted by the sensor, the ambient temperature is also taken into account. In this tutorial, MagicByCalvin describes in a few words the physical phenomenon that improves the precision of returning the distance detected by the sensor. In addition to the ultrasonic sensor, a temperature sensor will also be included.
  • RPi: HC-SR04 Ultrasonic Sensor mini-project – If you want to make an IoT application, you need an Internet connection. Connecting an Arduino board directly to the Internet is a solution, but depending on the project needs, it is often not the best solution. But connecting an Arduino board to Raspberry Pi 3 might be the best solution. In this tutorial, you have an example of how you can return the distance detected by the HC-SR04 sensor using the libraries for Arduino and the Raspberry Pi board.
  • Measure Distance using Ultrasonic Sensor | Pi4J | JAVA | Pi – If you need automation, Java, and IoT, in this tutorial you have an example of how you can access the full I/O capabilities of the Raspberry Pi via Java and read the distance returned by the ultrasonic sensor.
  • Raspberry pi, an Ultrasonic Sensor and Node.js – Measure Distances – Node.js is another library that can be used to make the work more efficient with an ultrasonic sensor and Raspberry Pi. It is a JavaScript library and is used in this tutorial to detect the distance returned by SR04.
  • Wiring ESP8266 nodeMCU with HCSR04 Ultrasonic Sensor – For IoT applications, a much cheaper resource than Arduino and Raspberry Pi is the ESP8266 module. With a more limited capacity than an Arduino and Raspberry Pi, ESP8266 12E nodeMCU is a board that can be connected to the Internet and able to read the values returned by the ultrasonic sensor. In this tutorial are described the steps required to connect HC-SR04 to the nodeMCU and the script needed to read the values returned by the sensor.
  • Remove impulse noise from ultrasonic sonar data – The HC-SR04 sensor accuracy can be improved. Taking into account the ambient temperature is the first step in improving the measurement precision. In addition to taking into account the ambient temperature in the sound wave calculation equations, an impulse noise reduction method can also be applied to ultrasonic sonar data. In this tutorial, you can see an example of how to implement the median filter to remove impulse noise from ultrasonic sonar data.
Parallax PING)))
Parallax PING)))

Parallax PING)))

PING))) differs from other sensors by using a single pin for trigger and echo. For example, the HC-SR04 uses a separate pin for trigger and echo. The sensor can detect objects in the range of 3 centimeters to 3 meters. The price for a single sensor is $29.99, which making it a much more expensive version compared to other sensors in its category like HC-SR04. PING))) can easily be used with an Arduino and Raspberry Pi. Below you find the tutorials and resources needed to start working with both development boards and the PING))) sensor.

  • Ping Ultrasonic Range Finder – This is a basic tutorial for PING))). In this tutorial was used an Arduino UNO board and several current wires to power the sensor and read the values returned by the sensor.
  • Parallax PING))): The Ultrasonic Distance Sensor – This is also an introduction tutorial for the PING))) sensor and Arduino, but it includes instructions for using a servo motor to scan the area around the sensor.
  • Raspberry Pi and Parallax Ping))) – Raspberry Pi can also read the values returned by the ultrasonic sensor. In this tutorial, you find instructions to connect the sensor directly to the Raspberry Pi board and commands to run the Python script to read the values returned by the sensor.
  • Calibrate PING))) – Like any other ultrasonic sensor, the PING sensor))) can return wrong values. Errors appear for various reasons, such as noise or not a constant voltage supply. By calibration, the errors that may occur in sensor operation cannot be reduced to zero, but it can significantly reduce the external effects that can result in the return of the wrong values.
Devantech SRF04/SRF05
Devantech SRF04 Sensor

Devantech SRF04 Sensor

Under the name of Devantech are two important ultrasonic sensors: SRF04 and SRF05. There is only one major difference between the two sensors. The difference is that the range varies between 3 centimeters to 3 meters for SRF04 and 1 centimeters to 4 meters for SRF05. The price of the sensors is about the same and is around USD $22.00. Both sensors have the same resolution that is 3-4 centimeters. The design and the operation mode of the SRF sensors are very similar to the HC-SR04 sensor. The popularity of these sensors is not very high, which translates into a relatively small number of projects where the sensors have been used. Below are tutorials to read the values detected by sensors with the Arduino board.

  • SRF05 (ultrasonic sensor) and arduino – This tutorial explains the easiest way to work with the SRF05/SRF04 sensor. The sensor is connected to a digital output pin on the Arduino board and the values returned by the sensor are used to calculate the distance to an object detected by the sensor.
  • SRFxx Sonic Range Finder Reader – This tutorial is written by Arduino and uses the “Wire.h” library to return the distances measured by the SRF sensor. Connecting the sensor to the Arduino board is through the I2C serial protocol and you also need a capacitor set on the sensor power pin to power the sensor without voltage fluctuations.
Maxbotix I2CXL-MaxSonar-EZ4
The I2CXL-MaxSonar-EZ4 Sensor

The I2CXL-MaxSonar-EZ4 Sensor

Maxbotix has launched a full range of ultrasonic sensors, but in this article, I will refer to one – I2CXL-MaxSonar-EZ4. The technical specifications make it probably the most special of all sensors in this article. It has a resolution of 1 centimeter and can detect objects at a distance of maximum 765 centimeters. It is a sensor designed especially for indoor applications and has a high noise tolerance. The price of such a sensor is $39.95. Below you will find a series of tutorials to use EZ4 with both Arduino and Raspberry Pi, but also with the Pixhawk controller.

  • How to Use an Ultrasonic Sensor with an Arduino – This tutorial written by Maxbotix is an introduction to Arduino’s connection and programming to read the values detected by the EZ4 sensor. In the tutorial, there are explanations for each piece of code, and how to connect the sensor to the Arduino board on the analog or digital pins.
  • Using an I2C?MaxSonar with an Arduino – The ultrasonic sensor can be connected to the Arduino using the I2C protocol. To ease the work of writing many lines of code that will eventually return the values read by the sensor, this tutorial explains the use of the “SoftI2CMaster.h” library. The library is adapted for use with the MaxSonar sensors.
  • Interfacing Ultrasonic Sensors with a Pixhawk or Ardupilot Mega (APM) – Pixhawk is a controller used in autonomous applications for multi-rotor copters and robots. Such a controller needs sensors to be able to detect obstacles on a particular route. In this tutorial, the Maxbotix sensor is used to allow an autonomous drone to land on the ground. Even though it is an ultrasonic sensor designed for indoor applications, the sensor also works in outdoor applications.
  • Interfacing a Raspberry Pi with an ultrasonic sensor to read serial data – In this tutorial you find the instructions to connect the ultrasonic sensor to Raspberry Pi and to read the values returned by it. On Raspberry Pi runs a Python script to read the distance from the sensor to an object.
  • Arduino UNO R3 + Maxsonar EZ3 calibration – Connecting the ultrasonic sensor to the 5V pin of the Arduino board does not guarantee a normal operation for the EZ3 sensor. Arduino board can provide 5V, but there are also cases when the voltage may vary depending on other consumers connected to the board. For a more accurate sensor operation, it must be calibrated. In this tutorial, you find instructions and the Arduino code used to calibrate the EZ3 sensor.
URM37 V4.0

URM37 V4.0

URM37 V4.0 is a sensor that integrates (in addition to trigger and echo) a temperature sensor for a more accurate measurement. This feature is unique to an ultrasonic sensor. The sensor can be used to detect objects between 5 centimeters and 500 centimeters with a resolution of 1 centimeter. The price is $13.90.

  • URM37 V3.2 Ultrasonic Sensor – In this tutorial you find the necessary information to connect the ultrasonic sensor to the Arduino board or an Arduino clone. Also, there is the code for reading the sensor values and returning the detected distance.
  • URMSerial.h – URM 37 Control Library Version 2.0.0 – Using a library can save time in trying to display the values returned by the sensor and controlling certain sensor parameters. This Arduino code is written using the URMSerial.h library.
  • rangefinder.c for the URM37 ultrasonic rangefinder used with the Raspberry Pi – The Raspberry Pi board can also be used to display the values returned by the URM37 sensor. After connecting the sensor to a microcontroller board like Arduino, with this script you can display using Raspberry Pi the values returned by the sensor.

Commerce Content is independent of articles and advertising, and if you buy something through our posts, I may get a small share of the sale.

8 comments » Write a comment

  1. if we place two HC-SRO4 sensors facing each other will they effect each other??

    please reply soon

    thank you in advance

    • Hello,

      Yes, two ultrasonic sensors could interfere and that way you have to wait until the first sensor ping to finish and then start to take reading for the second.

  2. I have a question about the parallax ultrasonic sensors. When using the VEX microcontroller, can you you the parallax “ping” sensors? If so, how are they wired?

    With the VEX ultrasonic, there are two sets of wires (input/output) that I plug into two digital ports and I have figured out how to program these to get my robot to work. But when I try to use the parallax, there is only one set and I’m not too sure how to program these.

    When I wire the parallax and plug it into digital port 1, the green light come on but I cannot get it to stop my robot. I then tried using a “Y” harness from one parallax and plug the two wires into ports 1 and 2….one lights up green, then other nothing. Then I tried to use 2 parallax sensors with their own wires plugging into ports 1 and 2. Ping in port 1 lights up but ping 2 in port 2 does not.

    The reason I ask is that I have about 30 parallax pings and very few VEX ultrasonic sensors.

    Any help or guidance would be greatly appreciated. Oh, I’m programming in robotC for VEX (not too sure if that makes a difference).

    Thank you for your time!


  3. Hi
    I’m doing water level controller project of residential water tank. i decided to measure the level of water using ultrasonic sensor with cheap rate. please suggest what type of ultra sonic sensor should i use ? Will ultra sonic sensor detect the water level ?

    • An ultrasonic sensor is both temperature and humidity dependent. I think that is not the best solution to measure the water level with an ultrasonic sensor, instead of this, you can try with an infrared proximity sensor

  4. So say we have something on a table outside with ultrasonic sensors around it. Which sensor would be best to use to detect it and how difficult would it be to program it to let us know that something is there or not?

  5. Hi there,
    I was wondering if it possible to use an ultrasonic sensor for just receiving ultrasonic signals?
    For example if an ultrasonic signal was emitted from a power source, how could I use these types of sensors to receive these signals and then plot them against time and amplitude?

Leave a Reply

Required fields are marked *.