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

该博客介绍了如何使用Arduino Uno与HC-SR04超声波测距传感器模块进行连接和交互。通过提供的代码示例,展示了如何设置接线、初始化端口以及测量并打印距离信息。程序每秒更新一次,距离以英寸和厘米的形式在串口监视器中显示。
摘要由CSDN通过智能技术生成

/*

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

Created 2014

by 太极创客

http://www.taichi-maker.com

使用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-maker.com

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);

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值