Lesson 5 - Integrating the ultrasonic sensor

Introduction

An ultrasonic sensor is an electronic sensor to measure the distance to an object using sound waves. The HC-SR04 Ultrasonic (US) sensor is a 4 pin module which consists of three parts- transmitter, receiver, and a control circuit.

Image result for ultrasonic sensor"

Applications

  • Distance Measurement
  • Obstacle Detection
  • Robotic Sensing

Connections

Ultrasonic Sensor is a digital sensor using 4 pins to operate. Two power pins including Ground and Vcc requires a potential difference of 5 volts. The other two pins are used to read data. The trigger pin takes an input that starts the sonic process, and Echo outputs a high pulse for a particular duration of time proportional to the distance of the object.

  • Vcc -> 5 VDC
  • Trigger -> GPIO
  • Echo -> GPIO
  • Ground -> Ground

Image result for ultrasonic sensor hc-sr04 pin discription"

Parameters:

  • Operating Voltage- 5 VDC 
  • Operating Current - 15mA 
  • Operating Frequency - 40Hz 
  • Max Range - 4m 
  • Min Range - 2cm 
  • MeasuringAngle - 15 degree 
  • Trigger Input Signal - 10uS TTL pulse
  • Echo Output Signal - TTL level signal, proportional to the distance
  • Dimension - 45*20*15mm 

Working Principle

The sensor consists of one transmitter and one receiver. The transmitter transmits an ultrasonic wave, this wave travels in the air and when it gets objected the wave is reflected back toward the receiver of the sensor.

Image result for ultrasonic sensor hc-sr04 working"
  • Make connections with Raspberry Pi
  • Set trigger pin high for 10 micro sec.
  • The Module automatically sends eight bursts 40 kHz and detect whether there is a pulse signal back. 
  • Set the echo pin to read the rising edge of the high pulse. Note the duration of the high pulse.

Distance calculation

The sensor works with the formula 

Distance = (Speed of Sound × Time)/2

Since the sound has to travel back and forth, therefore the total distance is divided by “2”. Speed here is the speed of the sound.

*Speed of sound at sea level = 343 m/s or 34300 cm/s

Connections with PiArm

To power the sensor, connect the Vcc pin of the sensor with the +5V pin of the Pi and the ground pin to the ground. Trigger and echo pins are internally connected to the pin 31 and 29 of the Pi respectively. Simply connect the sensor on the shield.

Trig -> Raspberry Pi Board Pin 31

Echo -> Pin 29

On the shield, an ultrasonic sensor header is designed to connect the sensor easily.      

    

   

To start the measurement, the trigger pin has to be made high for 10μs and then turned off. This action will trigger an ultrasonic wave at a frequency of 40kHz from the transmitter and the receiver will wait for the wave to return. On getting reflected by any object the wave is returned and the Echo pin goes high for a particular amount of time which will be equal to the time taken for the wave to return back to the sensor. Using this time the distance is measured.

Programming

Go to the PiArm directory, you’ll find a “sensor.py”  or 'ultrasonic_pick_n_place.py' file. Run the program using python3 IDLE. Go to the following links for ultrasonic PiArm integration python file-

https://github.com/sbcshop/PiArm/blob/master/ultrasonic_pick_n_place.py  OR

https://github.com/sbcshop/PiArm/blob/master/sensor.py


You can edit this for your own action a finding an object in front of PiArm. Use the ‘ultrasonic’ class, the distance_check function will return the distance of the object for you after doing all the calculations.

Leave a comment

All comments are moderated before being published