user50053: I am using an ultrasonic sensor SR04 with Arduino Uno and a sample program which I got from the library. However, my output reading is only 4 cm even when there is no object in front of it. I even used another sample program which had the same output. Is the problem in sensor or library?
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(115200);
}

void loop() {
  delay(50);
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}




user28569: Looks to me like your powering the SR04 with 3.3v instead of 5v.

50053: <p>I am using an ultrasonic sensor SR04 with Arduino Uno and a sample program which I got from the library. However, my output reading is only 4 cm even when there is no object in front of it. I even used another sample program which had the same output. Is the problem in sensor or library?</p>

<pre><code>#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(115200);
}

void loop() {
  delay(50);
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}
</code></pre>

<p><a href="https://i.stack.imgur.com/A9gIN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A9gIN.jpg" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/EJir7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EJir7.jpg" alt="enter image description here"></a></p>
 28569: <p>Looks to me like your powering the SR04 with 3.3v instead of 5v.</p>
