I have a HC-SR04 sensor mounted on a stepper motor, mounted on a car. The idea is this:

  • The car moves forward with the ultrasonic sensor facing the front
  • If the sensor reads a small distance the car do the following:
    • Stop
    • Turn the sensor to the left and get a new distance
    • Turn the sensor to the Right and get a new distance
    • Drive slightly backward
    • The car turns the to direction the distance was the highest
    • Drive forward again

I think I must have wrongly understood how the echo/ultrasonic sensor works. Because when I read values to the left and right, it will always return zero, but reading values when driving forward works very well.

Below is the relevant code

void triggerEcho(){

//Trigger, receive and calculate distance (cm)
long getEchoDistance(){
  pinMode(echoRecPin, INPUT);
  long echoDuration = pulseIn(echoRecPin, HIGH);

  return echoDuration / 58;

void loop(){

  long distance = getEchoDistance();

  //If front obstacle is encountered
  if((distance < 5) && (distance != 0)){
      servo.write(180);                     //turn echo sensor left
      long distanceL = getEchoDistance();   //trigger and read sensor value
      servo.write(0);                       //turn echo sensor right
      long distanceR = getEchoDistance();   //trigger and read sensor value

      //this will always show distance as 0
      Serial.print("  LEFT :"); Serial.print(distanceL); Serial.print("\n");
      Serial.print("  RIGHT:"); Serial.print(distanceR); Serial.print("\n");

      carDriveBackward(135, 1.185, 1.0);     //drive backwards
      if(distanceR>distanceL)                //turn in the direction that was furthest
      servo.write(90);                        //face sensor forward
      carDriveForward(135, 1.185, 1.0); //R - L

I bet something is wrong with how I'm implementing the triggerEcho() and getEchoDistance(). I excluded anything on the wiring and pins, as the reading of the distance is very correct when reading the facing the front.


I doubt you have misunderstood the way ultra sound works. You send a pulse and then go quiet waiting for a reply. The time it takes to send the pulse is the minimum distance you can detect things at.

If you are not getting returns left and right then it could be there is nothing there or turning the head is breaking a connection.

Firstly jack the vehicle up off the floor so it doesn't go anywhere. Place a solid object (book) in front of the vehicle and make sure it detects it. Now place the same object to the right of the vehicle, do it detect it? Stop the US sensor when it is looking right (by killing the power) and check that it is looking horizontally. Check the connections, are they being pulled loose?

Your code looks fine, you US works forwards so I think it is a mechanical issue with your car.

  • So I found out that it had to do with my pins. I was using pin 1 and 2 for trigger and echo. For some reason, I really don't understand why, but using Serial.print() function had an effect on using pin 1 and 2 properly (is that even possible? Do these pins have a relation to that?). Anyway, I switched to other pins, and it works in all direction. Except that I get some messed up results sometimes, but I think my sensor is too near the floor. I mark your answer as correct, as you were right, its a mechanical issue. Thank you:)
    – remi000
    May 8 '17 at 12:17
  • 1
    Ok. Googling a little bit tells me that pin 1 and 2 are somewhat special:)
    – remi000
    May 8 '17 at 12:23

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.