connect@howdiyworld.com
|

Ultrasonic Distance Measurement

BLOCK DIAGRAM

Components You Need

Ultrasonic Sensor
RGB LED
Jumpers
HowDIY

Procedure

  1. Ultrasonic Sensor and RGB LED
  2. Connections
  3. Program
  4. Measuring Distance

How Ultrasonic sensors work

Ultrasonic sensor emits sound waves at a frequency too high for humans to hear. They then wait for the sound to be reflected back, calculating distance based on the time required. Transmitter sends a sound wave and receiver receives the reflected sound wave.

RGB LED

A RGB LED or Light Emitting Diode is a Semiconductor  with Four terminal(Pin) component, the Four terminals are R,G,B(+)Positive and (-)Negative. To get Red Light Connect R and Negative pins where R is positive, To get Green and Blue do the same to G and B Pins.

Connections

·         Connect Ultrasonic Sensor’s VCC to 2nd pin.

·         Connect Ultrasonic Sensor’s Trigger to 12th pin.

·         Connect Ultrasonic Sensor’s Echo to 18th pin.

·         Connect Ultrasonic Sensor’s Ground(GND) to 20th pin.

·         Connect RGB LED’s  Red(R) to 22nd pin.

·         Connect RGB LED’s Green(G) to 24th pin.

·         Connect RGB LED’s Blue(B) to 26th pin.

·         Connect RGB LED’s Ground(GND) to 30th pin.

Program

import RPi.GPIO as GPIO

import time

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM) #GPIO Mode (BOARD / BCM)

 #set GPIO Pins

GPIO_TRIGGER = 18

GPIO_ECHO = 24

 #set GPIO
direction (IN / OUT)

GPIO.setup(GPIO_TRIGGER, GPIO.OUT)

GPIO.setup(GPIO_ECHO, GPIO.IN)

GPIO.setup(25,GPIO.OUT) # RED

GPIO.setup(8,GPIO.OUT) #GREEN

GPIO.setup(7,GPIO.OUT) #BLUE

GPIO.output(25,False)

GPIO.output(8,False)

GPIO.output(7,False)

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(1)

 if dist<10:

 GPIO.output(25,True)

 GPIO.output(8,False)

 GPIO.output(7,False)

 if dist>10:

 if dist<20:

 GPIO.output(8,True)

 GPIO.output(25,False)

 GPIO.output(7,False)

 else:

 GPIO.output(7,True)

 GPIO.output(8,False)

 GPIO.output(25,False)

 # Reset by pressing CTRL + C

 except KeyboardInterrupt:

 print(“Measurement stopped by User")

 GPIO.cleanup()

Measuring Distance

Here you can see the Distance between the sensor and obstacle

If the distance between them is anywhere between 0-10 cm Red light is Emitted

If the distance between them is anywhere between 10-20 cm Green light is Emitted

If the distance is above 20 cm blue light is Emitted

Social media & sharing icons powered by UltimatelySocial
Facebook
Twitter
YouTube
LinkedIn
Instagram