I am working on a project in which I have made a robot powered by 4 Johnson DC motors (300 rpm) and controlled by Arduino UNO R3. Now in my project I'd used ultrasonic sensor (HC-SR04) for detecting obstacles.

What I want is that as soon as my an obstacle is detected robot should turn by 180° but what actually happening is that sometimes robot takes turn very late (though I'd kept range of sensor approximately 50cm) and sometimes it just collides with obstacles.

As I'm new to Arduino I just want to know if an ultrasonic sensor is fast enough to sense obstacles and signal the Arduino about it, before it gets close enough?

Here's my code

int sense()
     analogWrite(trigPin, 0);
 // instead of digitalWrite(trigPin,LOW);

/* as I'd used my digital pins for  motors and all i have to use pwm pin for  trigger pin of sensors i guess it doesn't make any difference*/

     analogWrite(trigPin, 255);
  // instead of digitalWrite(trigPin, HIGH);

     analogWrite(trigPin, 0);
     duration = pulseIn(echoPin, HIGH);

     distance = duration/58.2;

      if (distance > maximumRange)
          return 0;
          return 1;
  • Why are you using analogWrite()?
    – Majenko
    Commented Dec 9, 2015 at 13:35
  • Use digital mode for the pins - analog pins are those with analog input capability but still digital outputs. If it is fast enough depends on things like how far you move in the time spent waiting for out of range echoes to die. Your more serious problem may be if objects are hard to detect due to size or angle. You may also have algorithm problems. Commented Dec 9, 2015 at 14:09
  • i am using wall or my bag as obstacle i dont think it has any problem detectiñg such object and if you think problem is with pins then it is not, as i had tried blinking LED using those pwm pins and regarding algorithm i dont know anything about it because i copied this code from internet and it worked so i used it in my project but now i am façing problems in response time of sensor Commented Dec 10, 2015 at 19:01
  • @NISHANTDESAI Try attaching a USB cable and print out the value all the time and see, maybe the object is indeed not detected.
    – Avamander
    Commented Dec 14, 2015 at 21:11

2 Answers 2


On it's max speed, how far is it's braking distance? Then you should start braking atleast from before that distance of the object.

Let's say your braking distance is 25cm, you should start braking >25cm. But this is only when you would instantly brake at the moment you're within 25cm reach.

So we have to know how many distance we will travel during the calculation, before we start braking. You could just try it out, set the distance a little higher everytime you crash into it (Assuming it doesn't fail and is always at the same time).

We have to know the top speed of your motor, RPM doesn't say a lot in this story, we'll need to know how big your wheels are to be able to determine how many cm's you'll travel per revolution, and doing that times the RPM, you know your top speed. (Or you can also measure this, but it can change depending on how full your battery is)

Also, your code should be real-time you'll likely want to use timers to trigger the trigPin and use an interrupt on the echoPin to be sure none of your code will delay your sensor. This will improve execution speed aswell as reliability/deterministic timing. Also, it will make it possible to run it aside from your regular code.

So if you know, your top speed = 10cm/second.

And you know that 1 calculation+reaction-time is 200ms.

You can safely assume that you need >2cm extra.

To be honest, you should add a little for when it's slippery/another surface or just so you won't touch the object.

So. The ping sensor is almost always fast enough, only if the calculated range is outside your max range, it will not work. You could however, use an IR proximity sensor which will trigger at a set distance. This way you don't have to do any calculations and just brake whenever you receive the interrupt. But it won't tell you the distance to the object.

The cool thing on the distance sensor is that you can also scale the brake distance on your driving speed, so that it will brake later when you're driving on half your speed. That way you can come very close to an object, but still be 100% sure that you won't crash into it.


Try using NewPing library. Ping sensors consistently and reliably at up to 30 times per second.

  • You can't achieve 30 times per second if you use the ping_median which basically does error correction. Best you can do is 1000/(29*5), or 6 readings (assuming using default 5 pings per median reading)
    – hyankov
    Commented Mar 4, 2020 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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