How to Measuring Distance Using Ultrasonic Sensors with Raspberry Pi

In This Projectyou'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.

n HC-SR04 ultrasonic range finder

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 entitiesWhile 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 = DistanceTime

 

 

 

 

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  = Speed TimeTime/2

 

 

 

 

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

34300 =

2*DistanceTime

 


≡ Components Required :


1.Raspberry pi

2. Micro SD Card

3.Ethernet cable

4.Breadboard

5. Connecting wire

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:

1. Take the breadboard.
2. Connect the sensor on the breadboard. Be beyond any doubt that in case you connect it flipped, the associations recommended here would not work. Cross-check the connections, and make sure to simply make a adjust circuit.
 
3. Interface pin 6 of Raspberry Pi to the ground rail of the breadboard.
 
 4. Interface Raspberry pi pin 2 to the sensor's 5V pin.
 
 5. The trigger pin can be connected directly with the sensor. In reality, the Raspberry Pi sends a 3.3V signal to this trigger pin, which is satisfactory by the sensor.
 
 6. Take out an association from Raspberry pi pin 18 to the breadboard, and connect it to the terminal rails of the breadboard. From the same rowyou'll be able to interface one terminal of 2KΩ resistor. The second terminal goes to the same ground strip where Raspberry Pi ground and sensor's ground are associated, as appeared in the following picture.
 
Measuring Distance Using Ultrasonic Sensors Measuring Distance Using Ultrasonic Sensors
Measuring Distance Using Ultrasonic Sensors
 
The connections we have done are great to go. Cross-check the connection twice before you control up the Raspberry pi . We have done the method on the hardware. We have to compose the program (code) to tell the RAspberry pi that we have connected this sensor to these pins and to calculate the separate based on the equations we determined.

≡ Code Ultrasonic Sensors Raspberry Pi:


it's time to write the code that measures the distance as well as notifies
#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 arrangeddeliver the Raspberry pi module some time to get ready to require the readings.

 There's have to utilize the while() loop. Using this forever-running circlewe 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 moreJust ensure that the ultrasonic sensor is positioned well and pointed towards the target. Then againjust 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

RASPBERRY PI TUTORIALS FOR BEGINNERS

 

No comments

Theme images by Dizzo. Powered by Blogger.