Skip to main content

Interfacing HC-SR04 Ultrasonic Sensor with Raspberry Pi

By:prayag nao

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.


HC-SR04 ultrasonic distance sensor module has four pins :
  • VCC – 5V, input power
  • TRIG – Trigger Input
  • ECHO – Echo Output
  • GND – Ground
Working of HC-SR04

Ultrasonic Module Operation



  1. Provide trigger signal to TRIG input, it requires a HIGH signal of atleast 10μS duration.
  2. This enables the module to transmit eight 40KHz ultrasonic burst.
  3. If there is an obstacle in-front of the module, it will reflect those ultrasonic waves
  4. If the signal comes back, the ECHO output of the module will be HIGH for a duration of time taken for sending and receiving ultrasonic signals. The pulse width ranges from 150μS to 25mS depending upon the distance of the obstacle from the sensor and it will be about 38ms if there is no obstacle.
 

Calibration

For accurate distance readings the output can be calibrated using a ruler. In the below program a calibration of 0.5 cm is added

Python Programming

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

 

 Run above program.

Output

Ultrasonic distance sensor output
Ultrasonic distance sensor output
Distance is measured every two seconds and displayed.

Comments

Popular posts from this blog

Shows full image when you hover over a thumbnail

By:Prayag nao Thumbnail Zoom Plus   It's a firefox add-ons.Shows full image when you hover over a thumbnail. Works with Amazon, Baidu Images, Bing Images, Facebook, Flickr, Google+, Google Images, IMDb, LinkedIn, Netflix, Pinterest, Reddit, Twitter, Yandex, YouTube, Wikipedia, Yahoo Images, & many more.     About this Add-on Thumbnail Zoom Plus is a Firefox plug-in which shows a full-size image popup when you hover over a thumbnail or image link. When you hover your mouse over a thumbnail or a link to an image or YouTube video, the add-on displays the full-size image or video still-frame in a floating window. The image remains visible until you move the mouse outside the thumb, click the mouse, or press Escape. It’s quick and easy to move the mouse from one thumbnail to another to see the corresponding full-size images. For details see  User Manual . Supported sites include Amazon , Baidu , Bing , Facebook , Flickr , Google , Huffington

Here's How This Supercool Hoverboard Works

By:Prayag nao   If you've ever dreamed of cruising around town on a floating skateboard like Marty McFly does in the classic '80s flick "Back to the Future Part II," then you could soon be in luck. A pair of innovators is trying to make the futuristic fantasy of riding a hoverboard into a reality. About two months ago, husband and wife design team Jill and Greg Henderson launched a Kickstarter campaignfor their Hendo Hoverboard, a levitating skateboard that could hit "hoverparks" as early as October 2015. The Kickstarter campaign , which ends Sunday (Dec. 14), has been a resounding success, bringing in well over its initial goal of $250,000 in its first week. With only a couple of days to go in the impressive crowdfunding campaign, the project has already raised nearly $500,000. But with all the hype comes an important question: How in the world does this thing work? The basic premise behind the technology is something called

Real life Jarvis-Talk With Your Computer like Jarvis in Iron Man ....!

By:Prayag nao                                            Code to Make your Computer like Jarvis New Speech macro..>> Choose Advanced and change the code like this.. <speechMacros>   <command>     <listenFor></listenFor>   </command> </speechMacros> You have to add a commands  <listenFor>........</listenFor> - computer listens the words you specify here and respond accordingly. <speak>............</speak> - computer speaks what is written in this field according to the command which it got. Similarly, You can Edit more commands in the same way.   <speechMacros> <command> <listenFor>What's going on dude</listenFor> <speak>Nothing special tony</speak> </command> </speechMacros> This is just a basic command,If you want more advanced commands.you have to use Java Scripts and VB scripts . Tell me Time : This is d