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 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.
- 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.
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.
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 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 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.