Skip to main content
1 of 3

Arduino + UltraSensor - Serial Monitor outputs Garbage

I am trying to connect the HC - SR04 to the Arduino Uno. For some reason when I run this code on the serial monitor it outputs wingdings in a straight line,

ýòøÿüÿøÿüòý÷ÿ÷ýÿÿúýÿýúÿòý÷ûòûòû÷ýÿøÿøÿÿ÷øÿüÿýÿþòøòÿÿøûüÿøÿÿòÿ÷ûòûòý÷ûòûòûöû÷ÿûû÷øÿÿ÷øÿüÿýÿüÿûöþÿý÷ûòûòûòûöûûûûûò

no breaks. Sometimes when I hit "Tools/Fix Encoding and Reload" it outputs

Ping: 0 cm Ping: 0 cm Ping: 0 cm

followed by more wingdings.

I've looked up some similar problems and it was usually fixed by changing baud to a lower value, 9600. I've done this and I still have problems.

Would you be able to help me out? I'd very much appreciate it.

    const int trigpin = 9;
    const int echopin = 10;



    long duration;
    int distance;


 void setup() {
 Serial.begin(9600);

              
              }

 void loop() {
  pinMode(trigpin, OUTPUT);
  pinMode(echopin, INPUT);

  digitalWrite(trigpin,LOW);
  delayMicroseconds(2);

  digitalWrite(trigpin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigpin,LOW);

      duration = pulseIn(echopin,HIGH);
      distance = duration *0.034/2;

  Serial.print("Distance: ");
  Serial.println(distance);
}