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。

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

无涯教程网

输出结果

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

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

来源:LearnFk无涯教程网

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

技术教程推荐

从0开始学架构 -〔李运华〕

持续交付36讲 -〔王潇俊〕

OpenResty从入门到实战 -〔温铭〕

透视HTTP协议 -〔罗剑锋(Chrono)〕

SQL必知必会 -〔陈旸〕

小马哥讲Spring核心编程思想 -〔小马哥〕

Go 语言项目开发实战 -〔孔令飞〕

手把手带你搭建秒杀系统 -〔佘志东〕

结构沟通力 -〔李忠秋〕

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