Welcome to the eighth tutorial of our Raspberry Pi programming course. In our previous tutorial, we looked at how servo motors work and explored the various servo motors and their applications. However, in this tutorial, we will interface an ultrasonic sensor with a Raspberry Pi and use Python to perform its calculations.

Components:

  • Raspberry Pi
  • Ultrasonic sensor
  • Male to female jumper wires
  • Bread board
  • Any object
  • 1k ohm resistor
  • 2k ohm resistor

What are Ultrasonic sensors?

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

With an ultrasonic sensor, you may measure the distance between your body and a target item by sending and receiving ultrasonic sound waves. Unlike audible sound, ultrasonic waves move faster than humans can hear. To make the sound, the transmitter uses piezoelectric crystals. The sound then travels to and from the target. When it returns, the receiver gets the sound.

How Does an Ultrasonic Sensor Work?

An ultrasonic sensor is made up of two parts: a transmitter and a receiver. This is the most common setup, with them arranged side by side as close as possible. Smaller measurement errors are achieved when the receiver is near the emitter, as the path of sound from the source to the destination is more straight-lined. In addition, the transmitter and reception functionalities of some ultrasonic transceivers are combined into a single device, decreasing inaccuracy to the greatest extent possible while simultaneously reducing the PCB footprint of the device.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

A laser's sound waves seem more like light as they exit the transmitter, therefore, elements such as beam angle and dispersion must be considered. Moving further away from the transmitter causes sound waves to broaden and narrow the detection area. Ultrasonic sensors, instead of specifying a fixed detecting region, provide coverage specifications in the form of beam width or beam angle to account for this shifting terrain. For comparison, it is important to make sure that either the full beam angle, or the difference from a transducer's straight line, is being used.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

The beam angle has a secondary effect on the device's range. The energy of an ultrasonic pulse can travel farther before it dissipates to unusable levels when focused in a narrow beam, which increases the detection range. As a result of this, the detection range is reduced when using a narrower beam. Wide beams are better for broad detection and covering large regions, while narrow beams are better for preventing false-positives since they limit the detection region.

When looking for individual parts, transmitters and receivers for ultrasonic sensors can be found separately, or as part of a single device called an ultrasonic transceiver. In most analog ultrasonic sensor alternatives, a trigger signal is sent to the transmitter, and the receiver sends back a signal as soon as the echo is recognized. In order to meet specific requirements, the designer can alter the pulse length and any encoding. The host controller is ultimately responsible for decoding and calculating the time between the trigger and the echo. Using onboard digital ultrasonic sensor modules, the host receives the distance information, which is subsequently sent back to the sensor modules. It is not uncommon to encounter a single ultrasonic transmitter, receiver, or pre-mounted transceiver and logic board on a PCB in the usual range-finding arrangement, even though these components are usually purchased separately and combined with bespoke circuitry and programming. Despite the fact that these modules are easier to use, designers give up a significant amount of flexibility and customizability.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

How is distance calculated?

Ultrasonic sensors typically emit a chirp that is much higher in frequency than the range of human hearing, which is why they are referred to as "ultrasonic." This chirp is used to calculate the duration taken for sound to reflect from an item. This method is based on the principles of echolocation, which are used by bats to detect their prey. With this in mind, it is easy to convert the time of the ultrasonic chip to distance because the sound travels at 343 m/s in air at ambient temperature.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

For example, an ultrasonic sensor pointed towards a box would take 0.025 seconds to bounce back, indicating the distance between the sensor and the box:

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4
Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

How do we interface with a raspberry pi?

The modules have four connections:

  • 5 volts is known as VCC.
  • Trig: trigger
  • Echo: the sensor's response
  • GND stands for "ground."
Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

The sensor emits an ultrasonic signal if a high pulse is detected on the trigger pin. An echo will return a strong signal almost at the same time. Until the reflected sound is detected, the pin echo sends a strong signal. The echo output returns to a low level at this point. With the time difference between the rising and falling edges of your echo pin set, all that's left to tweak is the timing in your software.

However, the sensor must first be linked to the Raspberry Pi before we can proceed with the software. VCC is specified as 5 volts in the connection description. At this voltage, the sensor should be able to measure distances of up to 450 centimeters. The real meat of the affair is about to be revealed. If you set VCC to 5 volts, the echo pin will also output 5 volts. GPIO pins are prone to voltages above 3.3V, so it is imperative to avoid them. Two alternatives exist at this point:

  • To compensate for this, they use 3.3 V to power the sensor. The range has been reduced to about 50 cm.
  • The sensor is powered by 5 volts, and a simple voltage divider is used on the echo pin. So, you get the sensor's full range, but you'll need two additional resistors to do it. Any spare GPIO pin can be used in place of pins 11 and 13.
Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4
Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4
Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4
Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4
Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

1k ohm resistor

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

2k ohm resistor

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

Pin 18 will be connected between the 1k ohm and 2k ohm resistors as shown below

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

Python Scripting for Ultrasonic Sensor

Double-time distance measurements are made with the following Python program: The while-loop is where the values are really determined. For ten seconds, the trigger is engaged. Sensors use this signal to produce sound pulses. While waiting for both a trigger and a positive signal to arrive, the echo signal has a slight lag at the very edge. The following while loop is responsible for allowing this delay, which is less than a second long: The start time is decided as soon as the echo signal rises to a high level.

In the final while loop, the echo signal's negative edge is identified and the stop time is calculated. To calculate how long it will take for a sound wave to travel between two points, subtract the starting location from its final destination. The speed of sound in the air is 343 m/s if this time is multiplied by the speed of sound. You must multiply the values by 100 in order to display them in cm. A speed of 34,300 cm/s is obtained. Finally, to acquire a single distance, multiply everything by 2.

This code's first line imports modules for working with the Raspberry Pi's GPIO pins.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

RPi.GPIO is a module that allows us to communicate with the Raspberry Pi's GPIO pins.

When delivering infrared sound waves, the Time Module is being used to create a delay before the sound is picked up by the receiver.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

When using the GPIO SetMode, the numbering scheme used to work on the Raspberry Pi's GPIO can be defined in two ways: GPIO.board and GPIO.BCM, respectively.

If you want to learn more about GPIO.Board and BCM, I'd like you to check out the following picture.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

Use the GPIO.setmode (GPIO.board) function to access the pin with the number in the circle, instead of relying on GPIo.Board.

GPIO.setmode (GPIO.BCM): BCM is the abbreviation for the Broadcom chip-specific number.GPIO.SetMode can be used to represent a pin number as a rectangle (GPIO.BCM).

When working with serial, SPI, I2C, and other communication protocols, I prefer to utilize the GPIO.setmode (GPIO.BCM) method.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

In these lines of code, we are setting up the PINs into which the ultrasonic sensors will be plugged.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

This line of code can be used to make the Raspberry Pi accept or send signals.

The Python Code for Distance Calculation

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

To keep the GPIO pin from going high for 2 milliseconds, this line of code is used. This ultrasonic sensor's datasheet says that this should happen.

There's a delay introduced in the process by this line of code: Time.sleep (2)

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

A delay is being added to the GPIO TRIG pin in accordance with sensor datasheets.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

Note: The print function described above will not work if you are using a Python version lower than 3.0. If this is the case, the print line can be modified as follows.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

Using the above line of code, we can find out how long the pulse takes to travel to the receiver after it has impacted an object. The return time of the sound wave is stored in this distance variable.

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4
Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

The code that includes all of the above lines is as follows:

Interfacing Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Raspberry Pi 4, Ultrasonic Sensor with Pi 4, pi 4 ultrasonic sensor, ultrasonic sensor pi 4, RPi4 ultrasonic sensor, Raspberry pi 4 ultrasonic sensor, ultrasonic sensor raspberry pi 4

Ultrasonic Sensors: Advantages and Drawbacks

Ultrasonic sensors, like any other technology, are best used in certain settings or applications. Among their many advantages are the following:

  • There is no effect on ultrasonic sensors on the color of what is being detected, even if it is water or glass, which can be seen through.
  • Most ultrasonic sensors can detect objects as close as a few centimeters away and as far as about five meters away.
  • Measurements of approximately 20 meters are possible with specially designed units.
  • This mature technology has been around for a long time and is extremely reliable and well-understood.
  • Ultrasonic sensors are capable of providing measurements with a degree of accuracy of within one percent, but they can go even further if necessary.
  • They can take a lot of measurements in a short amount of time, which leads to quick refresh rates.
  • Since they don't require any rare materials, they are usually relatively affordable.
  • When encoded chirp modules are used, ultrasonic sensors can withstand electrical noise as well as most acoustic noise, so they can be used in noisy places.

It is important to take into account the limits of ultrasonic sensors before making a final decision on which sensor to use.

  • Temperature and humidity affect the speed of sound, which can affect the accuracy of the measurements.
  • There is no way for an ultrasonic sensor to tell where an object is in relation to its sensing area, even though the detection zone is three-dimensional. It can't even tell if an object is round or round-colored.
  • It might not be a good idea to use ultrasonic sensors in projects that need to be small and hidden, like inside a car or a piece of industrial machinery.
  • Just like any other sensor, they can become clogged, wet, or frozen, causing them to malfunction.
  • In a vacuum, ultrasonic sensors can't work. This is because they need sound, which needs a medium to make it work.

What are the uses of ultrasonic sensors?

As a result, liquid level sensing is one of the most common uses of ultrasonic sensors, as they can detect liquids of any color or opacity while remaining non-contact. Due to the cost and simplicity of general object detection, the second option is a popular choice. There are a variety of object detection applications that include anti-collision detection, people detection, presence detection, and more.

If you want to get creative with ultrasonic sensors, you can take advantage of the one-way functionality of the transmitter and receiver. It is possible for animals to hear the ultrasonic pulses despite the fact that they are not audible to human ears. There are a number of uses for ultrasonic transmitters and receivers, including scaring off animals like birds and detecting noise.

Summary

An established technology, ultrasonic sensors have a wide range of uses, from industrial to consumer. Many new devices requiring presence detection or distance measuring can benefit from their simplicity, low cost, and durable construction. Because the hardware and software settings can be changed, they can be used in a wide range of situations.

Conclusion

In this tutorial, we learned how to connect ultrasonic sensors to the Raspberry Pi's GPIO 4 pins. In addition, we studied the sensor's fundamentals and the distance calculation used to determine an object's distance. Next, we'll learn how to connect a DHT11 sensor to a Raspberry Pi 4 board.