Arduino - 超声波传感器

Arduino - 超声波传感器 首页 / Arduino入门教程 / Arduino - 超声波传感器

HC-SR04超声波传感器像蝙蝠一样,使用SONAR来确定物体的距离。它采用2厘米至400厘米或1英寸至13英尺的易于使用的包装,提供了卓越的非接触范围检测,具有高精度和稳定的读数。

该操作不受阳光或黑色Materials的影响,尽管在听觉上,柔软的Materials(如布)可能很难检测到,它带有超声波发射器和接收模块。

Ultrasonic SensorUltrasonic Sensor Radiations

技术指标

  • Power Supply - +5V DC
  • Quiescent Current - <2mA
  • Working Current - 15mA
  • Effectual Angle - <15°
  • Ranging Distance - 2cm – 400 cm/1″ – 13ft
  • Resolution - 0.3 cm
  • Measuring Angle - 30 degree

所需组件

您将需要以下组件-

无涯教程网

  • 1×面包板
  • 1×Arduino Uno R3
  • 1×超声波传感器(HC-SR04)

电路图

按照电路图进行连接,如下图所示。

Ultrasonic Circuit Connection

Arduino代码

const int pingPin=7; //超声波传感器触发引脚
const int echoPin=6; //超声波传感器的回波引脚

void setup() {
   Serial.begin(9600); //启动串行终端
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration=pulseIn(echoPin, HIGH);
   inches=microsecondsToInches(duration);
   cm=microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds/74/2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds/29/2;
}

注意代码

超声波传感器具有如下连接的四个端子-+ 5V,触发器,回波和GND-

  • 将Arduino板上的+ 5V引脚连接到+ 5v。
  • 将触发器连接到Arduino板上的数字引脚7。
  • 将Echo连接到Arduino板上的数字引脚6。
  • 在Arduino上将GND连接到GND。

在我们的程序中,我们已经通过串行端口显示了传感器测量的距离,以英寸和厘米为单位。

链接:https://www.learnfk.comhttps://www.learnfk.com/arduino/arduino-ultrasonic-sensor.html

来源:LearnFk无涯教程网

输出结果

您将在Arduino串行监视器上看到以英寸和厘米为单位的传感器测得的距离。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

深入浅出云计算 -〔何恺铎〕

系统性能调优必知必会 -〔陶辉〕

Linux内核技术实战课 -〔邵亚方〕

爆款文案修炼手册 -〔乐剑峰〕

如何读懂一首诗 -〔王天博〕

编程高手必学的内存知识 -〔海纳〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

Serverless进阶实战课 -〔静远〕

好记忆不如烂笔头。留下您的足迹吧 :)