How to Measuring Distance Using Ultrasonic Sensors with Raspberry Pi
In This Project, you'll learn the basics of How to Measuring Distance Using Ultrasonic Sensors with Raspberry Pi. You'll get it on how to utilize the distance calculation formulas on the Raspberry Pi.
≡ What is the ultrasonic sensor :
Ultrasonic sensors produce ultrasound waves that are targeted towards an obstacle after which they wait for the echo to be heard. this sensor works at an ultrasonic frequency, which is higher than the capable of being heard frequency range of humans. The human's normal hypothetical capable of being heard frequency range is 20 Hz to 20 kHz. The ultrasonic sensor transmits the sound waves higher than 20 kHz frequency. so that why you hear any
sound when you use an ultrasonic sensor.
Ultrasonic waves are primarily utilized because they are not capable of being heard to the human ear and also because they give precise distance measurements over short distances. You'll certainly utilize acoustic sound for this reason, but it's not pleasant to have a noisy robot shouting every few seconds.
ultrasonic sensors create sonic bursts and calculate the echo. This echo is gotten back by the same sensor, calculating the time interval between the transition of the signal and reception of the echo to choose the distance to a target. The concept behind this sensor is almost the same concept used in Radar.
Usually, the representation of the HC-SR04 sensor, which can be utilized for this project The sensor appeared here has one transmitter and one receiver. For more accuracy, there can be different transmitters and receivers. In any case, this sensor can provide accuracy close ±3 cm within the run of 400 cm.
For example, on the off chance that the measured distance is 270 cm, the real separate can be 273 cm or 267 cm. Under the cylinders, the sensor has a control circuit that takes care of everything, counting 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 Raspberry pi pins directly. When we provide input from Raspberry pi to the trigger pin of the sensor, the transmitter radiates the sound pulses. These sound pulses bounce back from the solid object or surface, and we get the pulse from the echo pin. At that point, we calculate the time of arrival of the echo, and ready to calculate the distance.
More About hc-sr04 sensor datasheet
≡ How to calculation Distance on the ultrasonic sensor :
The speed of sound depends on which medium the sound wave is traveling in and the surrounding temperature as well as elevation from sea level. physicists have calculated the speed of sound at the sea level and have found it to be 34,300 cm/s. In case you measure the distance underwater at that point the speed of sound is 1,48,200 cm/s. See, it changed drastically when the medium changed.
This once more depends on the water's temperature and so many other entities. While making a project, make beyond any doubt that you use the proper speed of sound. Here, we are utilizing discussion as the medium.
We know that, Speed =
When we measure the time, it is measured based on the time taken in going towards the target and returning to the source of the sound waves. In any case, we need to calculate the time just for the one-way journey in order to measure the distance. For case, we are measuring the distance from point A to B. The sensor will create the sound from point A.
Let's assume that this sound comes to point B in time T1. At point B, the sound is reflected and comes to 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's why we require to divide the measured time by the calculate of 2.
So now, our equation is as follows:
Distance =
We know that the speed of sound is 34,300 cm/s:
34300 =
≡ Components Required :
7. Resistors
8. HC-SR04 ultrasonic range finder
This book will help you to gain more knowledge of Raspberry pi Software and Hardware Problems and Solutions
Raspberry Pi Cookbook
≡ Circuit diagram Ultrasonic Sensors Raspberry Pi:
We'll perform the taking after steps:
≡ Code Ultrasonic Sensors Raspberry Pi:
#Libraries
import RPi.GPIO as GPIO
import time
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
#set GPIO Pins
GPIO_TRIGGER = 23
GPIO_ECHO = 24
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
print("\nBeginning Measurements...\n")
def distance():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
if __name__ == '__main__':
try:
while True:
dist = distance()
print ("Measured Distance = %.1f cm" % dist)
time.sleep(0.5)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()
when you run code you can see like this Measured Distance = 9.1321cm
Compared to the steps to set up the program, there are a few minor changes in the compiled code. You may find that we utilized GPIO.setwarnings(False). This command turns off the warnings created whereas compiling the code. When the program is begun and the GPIOs are arranged, deliver the Raspberry pi module some time to get ready to require the readings.
There's a have to utilize the while() loop. Using this forever-running circle, we will see the live remove variation by indicating the sensor towards different targets. Sometime recently we start to compile the extend, check the connections once more. Just ensure that the ultrasonic sensor is positioned well and pointed towards the target. Then again, just put the sensor perpendicular to the table so that the barrels stay parallel to the table surface.
If you want to know more about raspberry pi then click on the link below
No comments