I’m trying to recreate an ultrasonic sensor project I saw on YouTube. The wiring is pretty simple and the programming is straightforward. But the serial output is odd, so I’m missed something.
long duration;
int distance;
const int trigPin = 10;
const int echoPin = 11;
void setup() {
// put your setup code here, to run once:
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
Serial.begin(115200);
}
void loop (){
// put your main code here, to run repeatedly;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
}





