目录
四、实验演示
一、实现目的
通过Arduino实现超声波测距,并通过串口显示
二、 硬件连接
三、代码程序
// C++ code
//
int cm;
void setup()
{
Serial.begin(9600); //串口设置波特率9600
pinMode(2, OUTPUT); //2 脚为输出TRIG
pinMode(3,INPUT); //3 脚为输入ECHO
}
void loop()
{
digitalWrite(2, LOW);
delayMicroseconds(2); // Wait for 2 微秒
digitalWrite(2, HIGH);
delayMicroseconds(10); // Wait for 10 微秒
// 20摄氏度空气中声速是343 m/s 34300cm/s 0.0343cm/us 29.15us/cm
cm=pulseIn(3,HIGH)/58.3; //收到的是返回的时间
Serial.println(cm); //串口输出
delay(1000);
}
四、实验演示
超声波测距