Playing with Raspberry Pi (14) - Ultrasonic Ranging

Playing with Raspberry Pi (14) - Ultrasonic Ranging

Today, the development of the automotive industry can be described as changing with each passing day. The concept of smart cars is also getting hotter. Autonomous driving will also be used more and more widely. I don’t know if you have ever thought that when we drive a car manually, we mainly use vision to perceive distance. For artificial intelligence, how does it obtain surrounding environment information by perceiving distance? This requires the use of related ranging sensors.

1. Ultrasonic ranging sensor

We know that the ultrasonic frequency is high, compared with ordinary sound waves, its energy consumption is slow, the propagation distance is long and the directivity is strong. Ideal for distance measurement. In this experiment, we use the HC-SR04 ultrasonic ranging module for distance measurement. The components are shown in the figure below:

As shown in the picture above, the HC-SR04 module has an ultrasonic generator (T side) and an ultrasonic collector (R side) and 4 pins. It is easy to buy this module on the Internet. It is cheap, accurate, easy to use and very cost-effective. The hardware schematic diagram of HC-SR04 is as follows:

Although the internal circuit structure of the module looks very complicated, we do not need to understand its internal complete working principle to use it to measure the distance. To measure the distance, the formula _ s = v * t _ is inseparable. If we want to calculate the distance s, we need to know the speed v and time t, the speed v of the sound wave is fixed, we can take 340m/s, and the time t is what the HC-SR04 module can help us get.

The core principle of ultrasonic ranging is to emit ultrasonic waves through the generating source. When the ultrasonic waves encounter obstacles, they will be reflected. The reflected ultrasonic waves can be sensed by the sensing source. The time it takes to pass a round-trip between obstacles. As shown below:

For the HC-SR04 module, its Trig pin is the trigger pin. Sending a high level of more than 10us to it can trigger the sending of ultrasonic commands. The HC-SR04 module will automatically send 8 square waves of 40kHz. The Echo pin will output a high level, and after receiving the reflected sound wave, the Echo will return to a low level again. Therefore, when using the HC-SR04 module, we only need to trigger the measurement command by adding a high level to the Trig pin, and then monitor the duration of the high level of the Echo pin to complete the ranging work.

2. Connection and coding

We choose GPIO17 under the BCM code to control the Trig pin for ultrasonic generation, and use GPIO18 to obtain the level of the Echo pin. The connection is as follows:

HC-SR04 module raspberry pie
VCC +5V power supply
GND GND
Trig GPIO17 (BCM code)
Echo CGIO18 (BCM code)

Write the following code:

#coding:utf-8

import RPi.GPIO as GPIO
import time

# 触发声波引脚
trig = 11
# 监听信号
echo = 12

def getDistance():
	# 输出高电平
	GPIO.output(trig, GPIO.HIGH)
	# 持续15us高电平 触发超声波
	time.sleep(0.000015) 
	# 停止加高电平
	GPIO.output(trig, GPIO.LOW)

	# 开始检测信号引脚的电平为高电平时开始计时
	while GPIO.input(echo) == 0:
		pass
	t1 = time.time()
	# 信号引脚的电平为低电平时计算时间间隔
	while GPIO.input(echo) == 1:
		pass
	t2 = time.time()
	# 计算距离
	s = (t2 - t1)*340/2
	return s

GPIO.setmode(GPIO.BOARD)
GPIO.setup(trig, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(echo, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

while True:
	s = getDistance()
	print("当前距离前方障碍物:%fm"%(s))
	time.sleep(1)

Run the above code on the Raspberry Pi to achieve ultrasonic ranging, the effect is as follows:

Focus on technology, understand love, be willing to share, be a friend

QQ:316045346

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/2340880/blog/5254387