0

I am having trouble getting two of my ultra sonic sensors to read accurate results in cm. I am using a HC-SR04 currently.

I get random numbers at different distances with an object in front (smooth cover book)

Test 1: Real distance: 10 cm Sensor 1: 0.00cm Sensor 2: 82.42

Test 2: Real distance: 10cm Sensor 1: 92.11cm Sensor 2: 95.03

Against the wall with no book:

Test 1: Real distance: 10 cm Sensor 1:0.00cm Sensor 2: 13.20

Test 2: Real distance: 10 cm Sensor 1:24.37cm Sensor 2:26.75

The numbers constantly fluctuate. I am trying to get accurate results.

I am not sure if the sensors are broken or if my code is incorrect.I appreciate any help.

Here is my set up: [My setup[1]

#define trigPin1 13
#define echoPin1 12
#define trigPin2 11
#define echoPin2 10

float duration, distance, duration1, distance1, duration2, distance2;

void setup() {
  Serial.begin (9600);
  pinMode (trigPin1, OUTPUT);
  pinMode (echoPin1, INPUT);
  pinMode (trigPin2, OUTPUT);
  pinMode (echoPin2, INPUT);

}

void loop() { 

  FindRange(trigPin1, echoPin1);
  duration1 = duration;
  distance1 = distance;
  FindRange(trigPin2, echoPin2);
  duration2 = duration;
  distance2 = distance;
  Serial.print(distance1);
  Serial.print(" cm  ");
  Serial.print(distance2);
  Serial.println(" cm");
  delay(100);

}

void FindRange(int trigPin, int echoPin) {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0345;
  delay(100);

}
  • Comments are not for extended discussion; this conversation has been moved to chat. – VE7JRO Nov 6 at 3:52
1

I am not sure but may be it helps just by checking principle of working of this sensors as it is depicted for low range distance you must be carefull about reflection from other source of ultrasonic. I think it would be a good Idea to place a delay between these two module so to make sure it get less noise from each other

  FindRange(trigPin1, echoPin1);
  duration1 = duration;
  distance1 = distance;
  delay(1000);//add delay
  FindRange(trigPin2, echoPin2);
  duration2 = duration;
  distance2 = distance;
  delay(1000);//add delay

the other reason for getting zero may be your controller could not measure reflection from wall due to lowspeed sampling (if you are polling) or reading time with low precision so I have a suggestion for you if you are familiar with STM32 there is a built-in hardware implementation for measure time of pulse using timers called one-pulse mode it may helps you get precision you need enter image description here

0

In the loop you trigger trgPin1 then trigPin2 but in the first range find you read echoPin2 instead of echoPin1.

void loop() { 

  FindRange(trigPin1, echoPin2);
  duration1 = duration;
  distance1 = distance;
  FindRange(trigPin2, echoPin2);
  duration2 = duration;
  distance2 = distance;
  Serial.print(distance1);
  Serial.print(" cm  ");
  Serial.print(distance2);
  Serial.println(" cm");
  delay(100);

}

must be

void loop() 
{ 
  FindRange(trigPin1, echoPin1);
  duration1 = duration;
  distance1 = distance;

  FindRange(trigPin2, echoPin2);
  duration2 = duration;
  distance2 = distance;

  Serial.print(distance1);
  Serial.print(" cm  ");
  Serial.print(distance2);
  Serial.println(" cm");

  delay(100);
}
  • Thank you I just changed and compile it but, when I use smooth book, The real distance: 15cm sensor 1: 0.00cm and sensor 2: 0.00cm. – Emme S Nov 5 at 17:57
  • What happens if you run the program in the loop and you move the book to and from the sensor. Do the values in the serial terminal smoothly change as you move the book or do they randomly flip around? In the latter case, I would say, there is something wrong with your setup. (broken wires, wrong connection, missing ground or vcc. Can you please add a picture of your setup? (I hope you have access to a smartphone with camera or a digital camera) Without seeing the setup I can not say much more. – Peter Paul Kiefer Nov 5 at 18:16
  • When I move the book to and from the sensor the serial terminal it gave me 0cm for both sensors then it only read sensor 1 in random numbers while sensor 2 was still 0cm. – Emme S Nov 5 at 18:22
  • Look at your picture you connected Vcc and GND to the Vcc rail of you breadboard. Sensor one is not grounded. So it's transmitting behavior is not specified. Perhaps the trigger or echo pin acts as ground when off and the sender speaker sends some weird signals the second sensor misinterprets. – Peter Paul Kiefer Nov 6 at 8:44
  • @EmmeS I forgot to precede my last comment with you name. So you might not get informed. Once again: Look at your picture you connected Vcc and GND to the Vcc rail of you breadboard. Sensor one is not grounded. So it's transmitting behavior is not specified. Perhaps the trigger or echo pin acts as ground when off and the sender speaker sends some weird signals the second sensor misinterprets – Peter Paul Kiefer Nov 7 at 11:42
0

The working principle of HC-SR04 is based the reflection of ultrasonic wave. Therefore, It measures distance inaccurately in the following case:

  • The obstacle is something that can adsorbs the ultrasonic wave. For example, cotton wool.
  • The obstacle is faced in non-90-degree. In this case, the obstacle may reflect the ultrasonic wave to another direction
  • The obstacle's surface can diffuse the ultrasonic wave

You can see the working principle of HC-SR04 here.

One more thing: two ultrasonic sensor may interfere to each other if they work at the same time. Try to give delay(100); between two reads

void loop() 
{ 
  FindRange(trigPin1, echoPin1);
  duration1 = duration;
  distance1 = distance;

  delay(100);

  FindRange(trigPin2, echoPin2);
  duration2 = duration;
  distance2 = distance;

  Serial.print(distance1);
  Serial.print(" cm  ");
  Serial.print(distance2);
  Serial.println(" cm");

  delay(100);
}
  • An 100ms delay is included in the FindRange function – Dorian Nov 6 at 8:11
0

I tested your code as it currently is with my own HC-SR04s and a clone Arduino Nano (since that is, what I have here currently). It works like a charm. The values are rather good, maybe accurate about +-1cm. I checked in the change between 10 and 70cm with my desk plate and a wall as obstacle. (I just looked, if the values are plausible. I didn't investigate thoroughly, how accurate the values are).

So I guess, that your sensors are broken.

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.