Chapter 3. Measuring Distance Using Ultrasonic Sensors

We, humans have five senses. They are touch, smell, sight, hearing, and taste. However, computers and robots can have as many senses as we want. We can sense different things around us; for example, changes in the temperature can be felt by our skin, but we cannot precisely say what the actual temperature value that we are feeling is. Computers such as RasPi can be used to sense and monitor the surrounding entities. It does the job well, precisely, and untiringly. The computing and interfacing capability of RasPi allows us to interface sensors with it.

Measuring distance using meter tapes and odometers is impractical or inconvenient for some of the applications. If you want to measure the depth of the ocean, how can you use meter tapes? The best option is to use technology such as Sonar or satellites. However, in our homes, labs, and even in our daily life, we often use different ultrasonic sensors for various applications. These applications include overhead tank water-level observation and automated path-finding robots. They also act as an aid to the visually impaired person, as a vehicle parking assistant, and so on.

In this chapter, you will learn the basics of a widely used distance meter, the ultrasonic sensor. You will understand how to use the distance calculation formulas on the RasPi. Further on you will also learn about the hardware setup of the board and also the fundamental requirements to make certain connections. Software level understanding is essential while using the ultrasonic sensor to run the codes and measure the distance precisely.

By the end of this chapter, we will have a project ready. This project will assist the visually impaired to avoid obstacles

We will cover the following topics in this chapter:

  • The ultrasonic sensor
  • Distance calculation methods
  • Hardware setup of the board
  • Software understanding
  • Installing and running the codes
  • Schedule your code while booting up
  • Troubleshooting
  • Obstacle avoidance system for the visually impaired and blind people

Let's understand the chapter to develop a project for a good cause!

The mysterious ultrasonic sensor

When you go on an expedition to mountains, you must have experienced the echo phenomenon while shouting loudly towards high mountains. You can even experience this phenomenon in a hall that doesn't have interiors such as curtains and furniture (in a new house). The ultrasonic sensor works on a similar principal. Ultrasonic sensors generate ultrasound waves that are targeted towards an obstacle after which they wait for the echo to be heard. However, why don't you hear any sound when you use an ultrasonic sensor? The answer is pretty simple: this sensor works at an ultrasonic frequency, which is higher than the audible frequency range of humans. The human's average theoretical audible frequency range is 20 Hz to 20 KHz. The ultrasonic sensor transmits the sound waves (also called as a sonic burst) higher than 20 KHz frequency. Ultrasonic waves are mainly used because they are not audible to the human ear and also because they provide precise distance measurement over short distances. You can definitely use acoustic sound for this purpose, but it's not nice to have a noisy robot shouting every few seconds. Elaborately, ultrasonic sensors produce sonic bursts and calculate the echo. This echo is received back by the same sensor, calculating the time interval between the transition of the signal and reception of the echo to decide the distance to a target. The concept behind this sensor is almost the same concept used in Radar. This is even more precise than an ultrasonic sensor and works on a very high frequency range (VHF). We can see its overall construction in the following image. This is the representation of the HC-SR04 sensor, which will be used for this project. You can purchase this sensor from any leading e-store or a hobby electronics store near you. The sensor has two cylinders on the board, and these cylinders have metallic nets on top of them. Usually, these cylinders are made up of steel or any equivalent material.

The mysterious ultrasonic sensor

Typical ultrasonic sensor

The sensor shown here has one transmitter and one receiver. For more accuracy, there can be multiple transmitters and receivers. However, this sensor can provide accuracy near ±3 cm within the range of 400 cm. For example, if the measured distance is 270 cm, the actual distance can be 273 cm or 267 cm. Under the cylinders, the sensor has a control circuit that takes care of everything, including the communication with RasPi. There are four pins that come out of the sensor: ground, echo, trigger, and supply. The ground and 5 V supply can be connected to RasPi pins directly. When we give an input from RasPi to the trigger pin of the sensor, the transmitter emits the sound pulses. These sound pulses bounce back from the solid object or surface, and we get the pulse from the echo pin. Then, we calculate the time of arrival of the echo, and we can calculate the distance. There is some physics behind this calculation, which you will learn in the next subsection. This will help us build the code to interface this sensor with RasPi easily. Let's take a peep into the physics behind the sensor.

Distance calculation

Do you know what the speed of sound is? Well, this really depends on which medium the sound wave is travelling in and the ambient temperature as well as elevation from sea level. Brilliant physicists have calculated the speed of sound at the sea level and have found it to be 34,300 cm/s. If you measure the distance under water then the speed of sound is 1,48,200 cm/s. See, it changed drastically when the medium changed; isn't this interesting? This again depends on the water's temperature and so many other entities. While making a project, make sure that you use the correct speed of sound. Here, we are using air as the medium.

We know that,

Distance calculation

When we measure the time (the duration of sending a sound pulse and receiving it back), it is measured based on the time taken in going towards the target and returning to the source of the sound waves. However, we want to calculate the time just for the one-way journey in order to measure the distance. For example, we are measuring the distance from point A to B. The sensor will generate the sound from point A. Let's suppose that this sound reaches point B in time T1. At point B, the sound is reflected and reaches back to the sensor at point A in time T2. So, the actual time we measure at the ultrasonic sensor is T = T1 + T2. That is why we need to divide the measured time by the factor of 2.

So now, our equation is as follows:

Distance calculation

We know that the speed of sound is 34,300 cm/s:

Distance calculation

Let's simplify it further to use in the code:

Distance calculation

That's all! We know the equation for the distance, and we also know the working principal for the ultrasonic sensor. We will use this exact equation directly in the Python script. So what are we waiting for? Let's start building the project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.36.141