HC-SR04 Mesafe Sensörü

HC-SR04 Mesafe Sensörü

Bağlantı Şeması

                                                               Örnek Yazılmış Kod

const int trigPin = 7;
const int echoPin = 8;
long duration;
float dist;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the dist
dist= (duration/2)/29.1;
// Prints the dist on the Serial Monitor
Serial.print("dist: ");
Serial.println(dist);
}

Paylaş:

Benzer Konular

Önceki Konu
Sonraki Yazı »