Ultrasonic sensor

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





 Serial.begin(115200);

in the sketch
image
in the Serial monitor

They must match

1 Like

1 Like

That worked. Thanks guys.

The most common problem is the serial monitor speed not set to the same of the Serial.begin(115200). Check the monitor speed, set it to 115200:

1 Like