Lesson 5 - Integrating the ultrasonic sensor
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.
- Distance Measurement
- Obstacle Detection
- Robotic Sensing
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
- 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
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.
- 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.
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.
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-
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.