Wednesday, December 28, 2016

Interfacing UltraSonic Sensor with Raspberry Pi

Ultrasonic Sensor HC-SR04 is widely used by hobbyists for distance measurement, we already have a post on our blog in which Arduino is used to measuring the distance of the object using the HC-SR04 sensor and we have also posted distance measurements using a Sharp IR sensor, so you can look that post as well.
We have started our journey with Raspberry Pi and in the first post we showed you guys how to use Raspberry Pi GPIO to control multiple LEDs after that, in our second post, we showed you guys a little complex example of controlling the brightness of Led using PWM.
In this post, we make things a little more complex and use the HC-SR04 UltraSonic Distance sensor to measure the distance of objects using Raspberry Pi.

Introduction
Ultrasonic sensors use sound waves to measure the distance of the object from the source. The module which we are going to use is HC-SR04 as shown in the image below.
HC-SR04 Sensor Module
As you can see this module has four pins as follow:
  • Vcc, +5V Supply Input Pin
  • TRIG, Trigger Input Pin
  • ECHO, Echo Output Pin
  • GND, Ground
You can watch this video for demo, and read the post given below for more information.

Working of Ultrasonic Sensor
The ultrasonic sensor emits sound waves, which travel through the air and if there is an object, these sound waves are reflected back. We know distance equals speed in time, and in this expression, we know the speed of Ultrasonic waves and we just got the travel time, hence we can calculate the object distance.

Steps:
  1. Raspberry Pi/Arduino provides 10us second pulse to the Trigger Input of the Module.
  2. After receiving the pulse module will transmit the eight 40Khz Ultrasonic burst signals.
  3. These Ultrasonic signals will reflect back if there is an object in the path.
  4. If the signal is reflected back, the ECHO output pin of the module will remain HIGH for the time of flight duration i.e time taken for sending and receiving Ultrasonic signals.
The above steps can be illustrated with the help of the following diagram.

Based on the above information we can now start working on connecting the Raspberry Pi with the HC-SR04 Ultrasonic module, but there is a small program, as we know Raspberry Pi is a 3.3V device and HC-SR04 is a 5V device, so we need to take care of this factor.
We can connect Raspberry Pi GPIO directly with the Trigger Pin of the Module but we can't connect the echo pin of the module with the Raspberry Pi GPIO, as this can damage the GPIO pin of Raspberry Pi. To solve this problem, we can make a simple voltage divider circuit for converting 5V into 3.3V.
So our Vin = 5V and Vout = 3.3V, now based on this information we need to evaluate the values of the resistor to be used.
Voltage Divider Circuit
Vout = Vin * R2/(R1+R2)
3.3*R1+3.3*R2 = 5*R2
3.3*R1 = 5*R2 - 3.3*R2
3.3*R1 = 1.7*R2
R2 = 1.94*R1
So based on the above expression we can choose the value of R1 and R2.
If R1 = 1.8kOhm then R2 =3.5KOhm
Voltage Divider
Connection Diagram

Connection Diagram
So now we have the connection diagram, let's start with the Python program to get the Object Distance and it is as follows:
'''
 Pin Out Of Raspberry Pi Model B
 01 - 3.3V    02 - 5V  
 03 - SDA(2)   04 - 5V  
 05 - SCL(3)   06 - GND  
 07 - (4)    08 - TXD(14)  
 09 - GND    10 - RXD(15)  
 11 - (17)    12 - (18)  
 13 - (27)    14 - GND  
 15 - (22)    16 - (23)  
 17 - 3.3V    18 - (24)  
 19 - MOSI(10)  20 - GND  
 21 - MISO(9)  22 - (25)  
 23 - SCKL(11)  24 - (8)  
 25 - GND    26 - (7)  
 '''  
 import RPi.GPIO as GPIO  
 import time  
 # Trigger Pin  
 Trigger = 15  
 Echo = 16  
 GPIO.setmode( GPIO.BOARD)  
 GPIO.setup( Trigger, GPIO.OUT) # Trigger Pin is Output for RPi  
 GPIO.setup( Echo, GPIO.IN)   # Echo Pin is Input for RPi  
 print "Starting program"  
 try:  
   while True:  
     GPIO.output( Trigger, False)  # Reset pin State to Low  
     time.sleep(2)          # 2 Second Delay  
     # Send Trigger Pulse to Module  
     GPIO.output(Trigger, True)   # Trigger High  
     time.sleep(0.00001)       # 10usec  
     GPIO.output(Trigger, False)   # Trigger Low  
     while GPIO.input(Echo)==0:   # Wait for Return Pulse  
       start_time = time.time()  # Save Start Time  
     while GPIO.input(Echo)==1:   # Wait for Pulse to End  
       end_time = time.time()   # Save the Pulse End Time  
     tof = end_time - start_time  # Time of Flight  
     print "Time of Flight = ", tof, " sec"  
     # Speed of Sound 34300 cm/sec  
     distance = (tof * 34300.0) / 2.0  
     # The above expression is divided by two,  
     # because sound waves has travelled twice distance  
     # from Module to Object and then back.  
     distance = round(distance, 2)  
     print " Object Distance = ", distance, " cm"      
 except KeyboardInterrupt:  
   print "Exiting Program"  
 except:  
   print "Error Occurs, Exiting Program"  
 finally:  
   GPIO.cleanup()

The above program is self-explanatory, as comments are present against each line of the program, but if still anyone faces any problem, please comment below.

No comments:

Post a Comment