100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > arduino与hcsr04_Arduino 驱动 HC-SR04 超声波测距模块

arduino与hcsr04_Arduino 驱动 HC-SR04 超声波测距模块

时间:2022-03-26 23:16:26

相关推荐

arduino与hcsr04_Arduino 驱动 HC-SR04 超声波测距模块

/*

Arduino Uno 驱动HC-SR04 超声波测距传感器模块

Created

by 太极创客

http://www.taichi-

使用Arduino Uno驱动HC-SR04超声波测距传感器模块

程序运行后,传感器将感应到的距离信息通过Arduino IDE的串口监视器显示。

接线方法:

HC-SR04 引脚 VCC 连接到 Arduino 引脚 +5VDC

HC-SR04 引脚 Trig 连接到 Arduino 引脚 11

HC-SR04 引脚 Echo 连接到 Arduino 引脚 12

HC-SR04 引脚 GND 连接到 Arduino 引脚 GND

获得HC-SR04 超声波测距传感器模块和Arduino的更多信息

请参阅太极创客网站:http://www.taichi-

This example code is in the public domain.

*/

int trigPin = 11; //Trig

int echoPin = 12; //Echo

long duration, cm, inches;

void setup() {

//Serial Port begin

Serial.begin (9600);

//Define inputs and outputs

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

}

void loop()

{

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.

// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(trigPin, LOW);

delayMicroseconds(5);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose

// duration is the time (in microseconds) from the sending

// of the ping to the reception of its echo off of an object.

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = (duration/2) / 29.1;

inches = (duration/2) / 74;

Serial.print(inches);

Serial.print("in, ");

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(1000);

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。