Go Down

Topic: Problem with Ultrasonic sensor- HC SR-04 (Read 1 time) previous topic - next topic

1804nr

Apr 17, 2015, 01:33 pm Last Edit: Apr 17, 2015, 02:05 pm by AWOL
I'm writing a code in which I'm supposed to take two readings (pings) for the ultrasonic sensor HC-SR04. My objective is to take two samples consecutively and measure the difference and slow down my robot vehicle if it goes beyond a certain threshold. The first reading is coming back fine, but the second reading gives me very weird values unless you're reasonably far away. I didn't put the wheels on my robot so it's stationary but the motors are still functional. This is my code:
Code: [Select]

#include "AFMotor.h"
#define trigPin 12
#define echoPin 13
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
 
void setup() {
  Serial.begin(9600); // begin serial communitication 
  Serial.println("Motor test!");
   pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  motor1.setSpeed(180); //set the speed of the motors, between 0-255
  motor2.setSpeed(180); 
}
 
void loop() {

  long duration1, distance1,distance2,duration2; // start the scan
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration1 = pulseIn(echoPin, HIGH);
  distance1 = (duration1/2) / 29.1;// convert the distance to centimeters.
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is
  digitalWrite(trigPin, LOW);
  duration2 = pulseIn(echoPin, HIGH);
  distance2 = (duration2/2) / 29.1;
  if (abs(distance1-distance2) > 1 && distance1 < 25){
  Serial.println(distance1); Serial.println("\n"); Serial.println(distance2); Serial.println("\n");
  Serial.println(abs(distance1-distance2));
Serial.print ( " CM! \n");// print out the distance in centimeters.

    motor1.setSpeed(220);
    motor2.setSpeed(220);
    motor1.run(FORWARD); 
    motor2.run (FORWARD);
    delay(3500);
}
  else {
   Serial.println ("No obstacle detected. going forward");
   delayMicroseconds(15);
   motor1.setSpeed(180);
   motor2.setSpeed(180);
   motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward!
    motor2.run(FORWARD); 
  } 
}

This is my output in run time when I put my hand in front of the sensor and my hand stays in the same place:

First time when I put my hand at somewhere around 25 cm:

24

50

26

CM!

When I put my hand at 5 cm:

First sample:

5

12

7

CM!

Now it gets really messed up. Second sample:

7

3587

3580

CM!

Only sometimes I get normal values, like this:

24

29

5

CM!

This is when my hand is at 25 cm.

I suspect it's the delay in the code between each sample the sensor takes which is causing this problem. I feel as if the sensor doesn't completely finish taking the first sample and simply adds it to the second sample at the hardware level. I need to be able to get good values. But I don't know how to get around this. I need reasonably accurate values. Please help. I'm using only one sensor.

Moderator edit: CODE TAGS DAMMIT!

AWOL

#1
Apr 17, 2015, 02:06 pm Last Edit: Apr 17, 2015, 02:15 pm by AWOL
Put a 20ms delay between pings - you're probably detecting echoes of the first ping.
Actually - put a longer delay there.

Also hint:
Code: [Select]

long rangeCM (byte trigPin, byte echoPin)
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  return (duration / 2) / 29.1;
}

1804nr

I'm still getting the same problem, even with a 50 ms delay. I've also used functions like you suggested. I'm still getting bizarre values. Note that my hand is in the same place and these are the values in my serial monitor.

12

3786

3774

CM!

14

33

19

CM!

zaxarias

Try to connect only the HC-SR04 to your Arduino.. What are the results ?
z.p

Go Up