need help with ultrasonic sensor

hello, im using 3 ultrasonic sensor on my robot. is it possible to get 3 sensor reading in Setup() function?? the problem i get is its only get the reading from the first sensor and the other 2 return 0. Picture below show the code that i made with the serial output.

thank you

I would start by reading each sensor individually (comment out the other 2) to make sure its not hardware or wiring.

If all 3 work individually then maybe its a problem with the library- there appears to be several other libraries available so maybe try one of those.

rw950431:
I would start by reading each sensor individually (comment out the other 2) to make sure its not hardware or wiring.

If all 3 work individually then maybe its a problem with the library- there appears to be several other libraries available so maybe try one of those.

hi, thankx for the reply.. yes, i already try to read each sensor individually and code it without using any library.. the result still same.

//here is my code

#define trigPin 23
#define echoPin 22

#define trigPin1 24
#define echoPin1 25

#define trigPin2 26
#define echoPin2 27

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
pinMode(trigPin1, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin1, INPUT);// set the echo pin to input (recieve sound waves)
pinMode(trigPin2, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin2, INPUT);// set the echo pin to input (recieve sound waves)

long duration, distance; // 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);
duration = pulseIn(echoPin,HIGH,10000);
distance = (duration/2) / 29.1;// convert the distance to centimeters.

Serial.print ("Distance is " );
Serial.println ( distance);

delay(2000);

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

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1,HIGH,10000);
distance1 = (duration1/2) / 29.1;// convert the distance to centimeters.

Serial.print ("Distance1 is " );
Serial.println ( distance1);

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

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2,HIGH,10000);
distance2 = (duration2/2) / 29.1;// convert the distance to centimeters.

Serial.print ("Distance2 is " );
Serial.println ( distance2);
}

void loop() {
// put your main code here, to run repeatedly:

}

if you wrap your code with "

" "

" it will display a lot better in the window on the forum.

instead of distance1, duration1, maybe try distanceA, durationA, distanceB, etc etc

i already rearrange the code in setup() and the other two sensor still return the value 0.

here another example:

#include <HCSR04.h>
HCSR04 LeftS = HCSR04(22,23,false);
HCSR04 RightS = HCSR04(24,25,false);
HCSR04 centerS = HCSR04(26,27,false);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Setup()");
int ccc = centerS.Distance();------------------------->>>> Only get reading from this sensor
int aaa = LeftS.Distance(); ------------------------->>>> return 0
int bbb = RightS.Distance();------------------------->>>> return 0
delay(2000);
Serial.println(aaa);
Serial.println(bbb);
Serial.println(ccc);
delay(5000);
}

no problem in loop().. All sensor get readings

void loop() {
// put your main code here, to run repeatedly:
Serial.println("Loop()");
int ccc = centerS.Distance();
int aaa = LeftS.Distance();
int bbb = RightS.Distance();
Serial.println(aaa);
Serial.println(bbb);
Serial.println(ccc);
delay(5000);
}

here is the output
Setup()
0
0
15
Loop()
11
20
15
Loop()
11
13
15

Below is library im using

#include "HCSR04.h"
#include <inttypes.h>
HCSR04::HCSR04(uint8_t echo,uint8_t trigger,bool isInches)
{
_echo = echo;
_trigger = trigger;
if(isInches) _factor = INCHES_FACTOR;
else _factor = CENTIMETERS_FACTOR;

pinMode(_echo,INPUT);
pinMode(_trigger,OUTPUT);
digitalWrite(_echo,LOW);
}

double HCSR04::Distance()
{
digitalWrite(_trigger,LOW);
delayMicroseconds(2);
digitalWrite(_trigger,HIGH);
delayMicroseconds(10);
digitalWrite(_echo,LOW);
long duration = pulseIn(_echo,HIGH,10000);
return duration / _factor / 2;
}

its hard to tell why since you only post your code here.
You should check individual sensors first.
But from my experiences:
1/ its better using HC-SR05.
2/ using a very good library called NEW PING (google here)
3/ Keep in mind that HC-SR04 will report 0 if out of range, and after that it will keep report 0 even if something in range. You need to reset Echo pin everytime when the value = 0 ( Turn it OUTPUT, Turn High wait few microsecond, Turn INPUT again).

Minh.