0

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

//Trigger
void triggerEcho(){
 digitalWrite(echoTrigPin,LOW);
 delayMicroseconds(5);
 digitalWrite(echoTrigPin,HIGH);
 delayMicroseconds(10);
 digitalWrite(echoTrigPin,LOW);
}
//Trigger, receive and calculate distance (cm)
long getEchoDistance(){
 triggerEcho();
 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)){
 carStop(); 
 servo.write(180); //turn echo sensor left
 delay(250);
 long distanceL = getEchoDistance(); //trigger and read sensor value
 servo.write(0); //turn echo sensor right
 delay(250); 
 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
 delay(250);
 if(distanceR>distanceL) //turn in the direction that was furthest
 carTurnRight(100,1250);
 else
 carTurnLeft(100,1250);
 carStop();
 servo.write(90); //face sensor forward
 delay(250);
 }
 else
 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.

asked May 7, 2017 at 17:51

1 Answer 1

0

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.

answered May 8, 2017 at 11:58
2
  • 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:) Commented May 8, 2017 at 12:17
  • 1
    Ok. Googling a little bit tells me that pin 1 and 2 are somewhat special:) Commented May 8, 2017 at 12:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.