Arduino与超声波模块

2.超声波模块
VCC接电源5V,Tring接D2,Echo接3,Gnd接Gnd。
代码:

const int TrigPin=2;
const int EchoPin=3;
float distance;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
Serial.println("ULtrasonic sensor:");
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(TrigPin,LOW);
delayMicroseconds(2);
digitalWrite(TrigPin,HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin,LOW);
distance=pulseIn(EchoPin,HIGH)/58.00;
Serial.print(distance);
Serial.print("cm");
Serial.println();
delay(1000);
}



你可能感兴趣的:(Arduino与超声波模块)