Raspberry Pi 3 – Day 6
Ultrasonic distance sensor
In air, sound travels at a speed of 343 metres per second. An ultrasonic distance sensor sends out pulses of ultrasound which are inaudible to humans, and detects the echo that is sent back when the sound bounces off a nearby object. It then uses the speed of sound to calculate the distance from the object.
Ultrasonic distance sensors are designed to measure distance between the source and target using ultrasonic waves. We use ultrasonic waves because they are relatively accurate across short distances and don’t cause disturbances as they are inaudible to human ear.
HC-SR04 is a commonly used module for non contact distance measurement for distances from 2cm to 400cm. It uses sonar (like bats and dolphins) to measure distance with high accuracy and stable readings. It consist of an ultrasonic transmitter, receiver and control circuit. The transmitter transmits short bursts which gets reflected by target and are picked up by the receiver. The time difference between transmission and reception of ultrasonic signals is calculated. Using the speed of sound and ‘Speed = Distance/Time‘ equation, the distance between the source and target can be easily calculated.
Potential Divider
Formula
Schematic Diagram of the Circuit
Wiring Diagram of the Circuit
Python Code
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) TRIG = 23 ECHO = 24 print "Distance Measurement In Progress" GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN) GPIO.output(TRIG, False) print "Waiting For Sensor To Settle" time.sleep(2) GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False) while GPIO.input(ECHO)==0: pulse_start = time.time() while GPIO.input(ECHO)==1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 distance = round(distance, 2) print "Distance:",distance,"cm" GPIO.cleanup()
import RPi.GPIO as GPIO #Import GPIO library import time #Import time library GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering TRIG = 23 #Associate pin 23 to TRIG ECHO = 24 #Associate pin 24 to ECHO print "Distance measurement in progress" GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in while True: GPIO.output(TRIG, False) #Set TRIG as LOW print "Waitng For Sensor To Settle" time.sleep(2) #Delay of 2 seconds GPIO.output(TRIG, True) #Set TRIG as HIGH time.sleep(0.00001) #Delay of 0.00001 seconds GPIO.output(TRIG, False) #Set TRIG as LOW while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW pulse_start = time.time() #Saves the last known time of LOW pulse while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH pulse_end = time.time() #Saves the last known time of HIGH pulse pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance distance = round(distance, 2) #Round to two decimal points if distance > 2 and distance < 400: #Check whether the distance is within range print "Distance:",distance - 0.5,"cm" #Print distance with 0.5 cm calibration else: print "Out Of Range" #display out of range
test6.py ( old )