目录

Arduino - 超声波传感器( Ultrasonic Sensor)

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

操作不受阳光或黑色材料的影响,虽然声学上很难检测到布料之类的柔软材料。 它配有超声波发射器和接收器模块。

超声波传感器

超声波传感器辐射

技术规格

  • 电源 - + 5V DC
  • 静态电流 - <2mA
  • 工作电流 - 15mA
  • 有效角度 - <15°
  • 测距距离 - 2厘米 - 400厘米/ 1英寸 - 13英尺
  • 分辨率 - 0.3厘米
  • 测量角度 - 30度

组件的要求 (Components Required)

您将需要以下组件 -

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

过程 (Procedure)

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

超声波电路连接

草图 (Sketch)

在您的计算机上打开Arduino IDE软件。 用Arduino语言编码将控制你的电路。 单击“新建”打开新的草图文件。

草图

Arduino代码 (Arduino Code)

const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}
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;
}

Code to Note

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

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

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

结果 (Result)

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

↑回到顶部↑
WIKI教程 @2018