user60672: 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: [
#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);

}


user10285: 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);
}


user66249: Where they are plugged in they won't work as the rows (like the other side) are connected. Example: http://wiring.org.co/learning/tutorials/breadboard/imgs/breadboard-02.jpg
They aren't connected to the other pins, and they are connected themselves.


user70328: change the MCU, that arduino is a chinese version.
Use a original or other original board.
I had the same problem with two diferents projects, and it happend the same to me. One with ultrasonic and other with RF24 module. In both projects, some really extrange things happens. But when i change for one original arduino, everything works perfect.

user48909: 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


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

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

<p>Test 1: 
Real distance: 10 cm 
Sensor 1: 0.00cm
Sensor 2: 82.42</p>

<p>Test 2:
Real distance: 10cm
Sensor 1: 92.11cm
Sensor 2: 95.03</p>

<p>Against the wall with no book:</p>

<p>Test 1:
Real distance: 10 cm
Sensor 1:0.00cm
Sensor 2: 13.20</p>

<p>Test 2:
Real distance: 10 cm
Sensor 1:24.37cm
Sensor 2:26.75</p>

<p>The numbers constantly fluctuate. I am trying to get accurate results. </p>

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

<p>Here is my set up: [<img src="https://i.stack.imgur.com/X0ACl.jpg" alt="My setup[1]"></p>

<pre><code>#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);

}
</code></pre>
 10285: <p>In the loop you trigger trgPin1 then trigPin2 but in the first range find you read echoPin2 instead of echoPin1.</p>

<pre><code>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);

}
</code></pre>

<p>must be </p>

<pre><code>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);
}
</code></pre>
 66249: <p>Where they are plugged in they won't work as the rows (like the other side) are connected. Example: <a href="http://wiring.org.co/learning/tutorials/breadboard/imgs/breadboard-02.jpg" rel="nofollow noreferrer">http://wiring.org.co/learning/tutorials/breadboard/imgs/breadboard-02.jpg</a></p>

<p>They aren't connected to the other pins, and they are connected themselves.</p>

<p><a href="https://i.stack.imgur.com/7xeLq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7xeLq.png" alt="Look at the connection"></a></p>
 70328: <p>change the MCU, that arduino is a chinese version.
Use a original or other original board.</p>
<p>I had the same problem with two diferents projects, and it happend the same to me. One with ultrasonic and other with RF24 module. In both projects, some really extrange things happens. But when i change for one original arduino, everything works perfect.</p>
 48909: <p>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</p>

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

<p>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
<a href="https://i.stack.imgur.com/ou5Mz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ou5Mz.png" alt="enter image description here"></a></p>
