|
在本篇文章中,我们将使用Arduino开发板连接超声波传感器测量距离,然后将结果显示在LCD1602显示屏上。
所需的硬件 ● Arduino Uno开发板 ● 超声波传感器(HC-SR04) ● 液晶显示屏1602 ● 10K电位器 ● 连接导线
超声波距离传感器是如何工作? 超声波传感器发出高频声音脉冲,然后计算声音回声反射所需的时间。传感器正面有2个开口。一个开口发射超声波(类似于微型扬声器),另一个接收它们(类似于微型麦克风)。
空气中声速约为每秒341米(1100英尺)。超声波传感器使用该信息以及发送和接收声音脉冲之间的时间差来确定到物体的距离。它使用以下数学方程式: 距离=时间 x 声速 / 2
为什么/何时使用超声波传感器? 1. 非常适合在正常和困难环境中进行精确的自动距离测量。 2. 特别适用于光学传感器无法使用的环境,如烟雾、灰尘等。 3. 非常准确、稳定、可在大范围内使用。
超声波传感器可以在不接触待测介质的情况下测量以下参数: ◾ 距离 ◾ 水平 ◾ 直径 ◾ 是否存在 ◾ 位置
工作过程 超声波传感器发射高频声音脉冲,然后根据回波信号从期望目标反射之后返回所花费的时间来计算距离。空气中声速为每秒341米。计算距离后,它将显示在LCD显示屏上。
超声波传感器的时序图。
以下是代码: - /*LCD circuit:
- * LCD RS pin to digital pin 12
- * LCD Enable pin to digital pin 11
- * LCD D4 pin to digital pin 5
- * LCD D5 pin to digital pin 4
- * LCD D6 pin to digital pin 3
- * LCD D7 pin to digital pin 2
- * LCD R/W pin to ground
- * LCD VSS pin to ground
- * LCD VCC pin to 5V
- * 10K resistor:
- * ends to +5V and ground
- * wiper to LCD VO pin (pin 3)*/
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Interface pins of the LCD
- const int trig_pin=8;
- const int echo_pin=9;
- long distance,duration;
- void setup() {
- lcd.begin(16,2);
- lcd.setCursor(0,0); //set the cursor to column 0 and line 0
- pinMode(8,OUTPUT);
- pinMode(9,INPUT);
- }
- void loop() {
- digitalWrite(8,HIGH);
- delayMicroseconds(20);
- digitalWrite(8,LOW);
- delayMicroseconds(20);
- duration = pulseIn(echo_pin, HIGH); //To receive the reflected signal.
- distance= duration*0.034/2;
- lcd.setCursor(0,1); //set the cursor to column 0 and line 1
- lcd.print(distance);
- lcd.print("cm");
- delay(100);
- }
复制代码
|