用 Arduino uno + HC-SR04 做一个超声波测距仪

Arduino, LCD1602A, HC-SR04 configuration

LCD1602 display

引脚 4线连线:

PinConnect toExplaination
VSSground接地
VDD5V power接电源
V0电位器输出对比度调整端(5V对比度最弱,接地对比度最高;可通过10kΩ电位器调整对比度)
RS12口寄存器选择,高电平——数据寄存器;低电平——指令寄存器
R/Wground读写信号线,高电平——进行读操作;低电平——进行写操作
E或EN11口使能(enable)端,下降沿使能
D45口数据
D54口数据
D63口数据
D72口数据(D0~D7为数据总线,通常只接D4~D7)
A5V power背光电源
Kground背光接地

控制LCD1602的代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//LiquidCrystal (register select, enable, data pin d0, data d1, data d2, data d3);

void setup() {
lcd.begin(16, 2); // set the row and column number
lcd.print("hello, world!");
}

void loop() {
lcd.setCursor(0, 1); // set cursor position (col, row)
lcd.print(millis()/1000); // show the time since boot.
}

HC-SR04 引脚连线

PinConnect toExplaination
GndGnd接地
Echo8口(任意 uint8_t 口)然后从Echo读取输出
Trig9口(任意 uint8_t 口)先向Trig发送长度为8微秒的脉冲信号
VccPower电源

控制HC-SR04 代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <LiquidCrystal.h>

#define SR04_TRIG 9
#define SR04_ECHO 8

void setup(void){
lcd.begin(16, 2); // set up the LCD's number of columns and rows
pinMode(SR04_TRIG, OUTPUT);
pinMode(SR04_ECHO, INPUT);
}

void loop(void){
// send a 8 microseconds pulse to TRIG
digitalWrite(SR04_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(SR04_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(SR04_TRIG, LOW);

// `pulseIn` function waits until ECHO turn to HIGH, starts accumulating time
// until ECHO turn to LOW.
float temp = pulseIn(SR04_ECHO, HIGH);
float distance_cm = (temp * 17) / 1000;

lcd.setCursor(0, 1);
lcd.print("DIS:");
lcd.print(distance_cm);
lcd.print("cm");
lcd.print(" ");

}

最终连线效果…


用 Arduino uno + HC-SR04 做一个超声波测距仪
https://www.billhu.us/2021/17_arduino_ultrasonic/
Author
Bill Hu
Posted on
September 7, 2021
Licensed under