HC-SR04 outputs confusing chars ?

I am using this HC-SR04-DistanceSensor. I have wired a 10nF-capacitor between Vcc and GND for decoupling.

seems to work somehow on the first sight, but it outputs me some strange chars in the SerialMonitor.

12
15
20
23
28
30
38
&33
!35
#40
(43
+48
051
353
556
858
:61
=58
:53
551
343
+40
(38
&43
+28
23
20
17
20
17
20
25
30
33
!43
+40
(46
.51
356

also the reading of the distance is not as linear as I try to move my hand over the sensor.

How can I fix that? what causes those chars in the serial-output? is it simply a shitty sensor?

 const int trigPin = 9;
    const int echoPin = 10;

    long duration;
    int distance;
    int distanceOld;



    void readSensor() {
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = duration*0.034/2;
      

      if(distance <= 25 && distanceOld != distance) {        
        Serial.println(map(distance, 0, 25, 0, 64));
        Serial.write(map(distance, 0, 25, 0, 64));
        distanceOld = distance;
      }
    }
    

void setup() {

  // SERIAL
    Serial.begin(9600);      
    while ( !Serial && (millis()<4000) ) {
      // ..  
    } 
    Serial.println("SERIAL LOADED..");
    
  // SENSOR_PINS
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input

}

void loop() {
  readSensor();
  delay(30);

}

remove ‘serial.write’. incr ‘delay()’ in main loop

Why are you trying to use a capacitor in this way?

Did you test the input / output Check your Declarations, they might need improvement.

Gates:
Why are you trying to use a capacitor in this way?

because of decoupling … doesn’t it work this way?

Gates:
Check your Declarations, they might need improvement.
[/quote
what do you mean?

knut_ny:
remove ‘serial.write’. incr ‘delay()’ in main loop

this totaly fixed it! :slight_smile:

what exactly is the difference between write/print() ? … I mean I HAVE to use print() in a other projects to work properly(?)