本网页已闲置超过3分钟,按键盘任意键或点击空白处,即可回到网页

超声波传感器和Arduino的基本设置
发布时间:2021-02-17
分享到:
超声波传感器和Arduino的基本设置
发布时间:2021-02-17
分享到:

在本项目中,我们将使用超声波传感器来测量距离,检测物体并以常用的测量单位提供O / P。

硬件组件:

  • Arduino UNO    ×    1个    
  • 超声波传感器-HC-SR04(通用)×    1个    
  • 面包板(通用)    ×    1个    
  • 跳线(通用)×    1个    
  • USB-A至B电缆×    1个   

软件应用程序和在线服务:

  • Arduino IDE   

可以在超声波传感器和Arduino之间直接建立连接(或),也可以使用Breadboard实现相同的功能。
•    将超声波传感器的Vcc引脚连接到Arduino上的5V
•    将超声波传感器的GND引脚连接到Arduino上的GND
•    将超声波传感器的Trig引脚连接到Arduino上的D6
•    将超声波传感器的回波针连接到Arduino上的D5

带有Arduino的超声波传感器-使用面包板的电路连接:

超声波传感器具有广泛的应用,例如距离测量,物体检测,避障等。

该项目的设计牢记要寻找使用Arduino的超声波传感器的基本设置的人员。任何类型的Arduino均可用于此项目。

工作非常简单,按照电路图进行连接并打开电源后,超声波传感器就会发送声波。当波与物体接触时,它们会反弹回传感器。因此,通过将速度乘以时间来计算距离。此处的速度是指声音的速度,即每秒340米。以厘米/微秒为单位为0.034,以英寸/微秒为单位0.0133858。我们必须将速度除以2,因为声波在撞击物体后向前传播并返回。

另一方面,时间是声波向前传播和向后反弹所花费的持续时间。因此,很容易确定物体与超声波传感器的距离。

您可以在下面的代码中找到计算。

在串行监视器中看到的输出:

相关代码:

/*
 * Credits to all sources on Google, acting as reference to this code below. This code is not
 * TechValer's own creation. TechValer has referred to different projects and websites to
 * find an easy code for beginners to get started with Ultrasonic Sensor HC-SR04 and Arduino.
 * TechValer does not claim this code to be its own. TechValer is greatly thankful for original
 * creaters of this code and also all others who acted as reference.
 */

/*
 *  About TechValer
 *  
 *  What comes to mind when you think of tech...hmm, I'm sure you're thinking of iPhone,
 *  Alexa, Boston Dynamics robotic dog etc., at least that's what I would have thought of.
 *  My point here is, when you look inside this tech...you'll find weird boards with
 *  components attached to it. This stuff is electronics and we at Techvaler deeply appreciate
 *  this piece of tech. As the name suggests, we are Tech Enthusiasts who know the Worth and
 *  Potential of these amazing tech stuff! So, check out our website techvaler.com to find out
 *  more. I'm sure you will not regret...
 */

/*
 *  Note: Any model of Arduino can be used for this project. Just keep in mind the digital pins.
 *  The readings available in the serial monitor are almost accurate.
 *  Refer to the documentation for better understanding of this particular code and project.
 */

/*
 * Thanks to Drona Automations Pvt.Ltd for Sponsoring this project!
 */
 
#define trigPin 6 // Connect trigpin to D6 on Arduino
#define echoPin 5 // Connect echopin to D5 on Arduino

// defines variables

long duration; // It's a variable for the duration of sound wave travel
int inchdistance; // It's a variable for measurement of distance in inches
int cmdistance; // It's a variable for measurement of distance in centimeters

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // For Serial Communication
}

void loop() {
 
  digitalWrite(trigPin, LOW); // Clears the trigPin condition
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds

  // Calculating the distance
 
  inchdistance = duration * 0.0133858 / 2; // To calculate the distance in inch
  cmdistance = duration * 0.034 / 2; // To calculate the distance in cm
 
  // Displays the distance on the Serial Monitor
 
  Serial.print("Distance: ");
  Serial.print(inchdistance);
  Serial.println(" inch");
  Serial.print(cmdistance);
  Serial.println(" cm");
}

加入微信技术交流群

技术交流,职业进阶

关注与非网服务号

获取电子工程师福利

加入电路城 QQ 交流群

与技术大牛交朋友

讨论