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


void setup() {

void loop() {
  Serial.print("Ping: ");

enter image description here

enter image description here

  • Which sensor dude, we are not clairvoyants – Atizs Oct 26 '18 at 7:32
  • sorry, its ultrasonic sensor SR04 – Sultan Morbiwala Oct 26 '18 at 7:34
  • Are you 100% positive you did not mix up trigger and echo pins? I know it is just 2 pins, but still. The example has 11 and 12, you got 12 and 13. Maybe you mixed them up. If not,try 11 and 12 as in example. here is the reason why your input echo might not work - arduino.cc/en/Tutorial/DigitalPins Look up digital pin 13, it has LED on it. It is described in the link. – Atizs Oct 26 '18 at 8:01
  • Yup, I changed the pins to 11 and 12. It works better than before but still shows the reading around the 4cm. Somethings it goes till 100cm. Its not accurate, is it normal or my sensor is defective? – Sultan Morbiwala Oct 26 '18 at 9:00

I recommend, if you want reliability, that you upgrade to the HY-SRF05 sensor. The problem with the SR04 is that when there is nothing in front of the sensor (or the target is beyond the limit) the sensor sometimes "locks up," and you have to reset it to get it working correctly again. The SR04's are a few cents cheaper than the others; there's a reason for that.

Also, there's more than one library available for these sensors. I recommend the NewPing library by Tim Eckel available from here: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home

I am not affiliated with Tim in any way, but he's got a good library.

| improve this answer | |

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.