Problem with multiples Parallax PING))) Ultrasonic Sensor

Hi, I'm using ArduinoMega to use multiple ultrasonic sensors but I having some problems.

When I connect only 1 ultrasonic sensor, it works perfectly. However, with two or more ultrasonic sensors, the sensor values become 0.

Can anyone help me?

Thanks in advance.

Here is my code....

const int pingPin[2] = {5,6};

void setup() {
Serial.begin(9600);
}
void loop() {

for(int i =0; i<sizeof(pingPin)/sizeof(pingPin[0]); i++) {
int cm = ping(pingPin*) ;*

  • Serial.print(i); Serial.print(":"); Serial.print(cm); Serial.print(" ");*

  • }*

  • Serial.println();*

  • delay(100 ); // each centimeter adds 10 milliseconds delay digitalWrite(ledPin, LOW);*
    }
    int ping(int pingPin) {

  • long duration, cm;*

  • pinMode(pingPin, OUTPUT);*

  • digitalWrite(pingPin, LOW);*

  • delayMicroseconds(2);*

  • digitalWrite(pingPin, HIGH);*

  • delayMicroseconds(5);*

  • digitalWrite(pingPin, LOW);*

  • pinMode(pingPin, INPUT);*

  • duration = pulseIn(pingPin, HIGH);*

  • cm = microsecondsToCentimeters(duration);*

  • return cm ;*
    }
    long microsecondsToCentimeters(long microseconds) {

  • return microseconds / 29 / 2;*
    }

Yeah I'm having the same problem. I don't have a solution yet but I will need to!

Adding to the delay doesn't work.

It MUST be a code issue, or something where the solution is in code because I have an array of 4 sensors, and they read normally on sensors 1 and 3, but 2 and 4 have issues.

I ran into this problem with a completely different brand of Ultrasonic Rangefinders. I tried increasing the delay but to no avail. It ALWAYS sets the even servos (2 and 4) to 0. It is incredibly persistant. Switching one sensor with another sends the problem over the the new sensor.

Any ideas? I doubt it is sound interference because we tried delaying.

I used a backup sensor and it works.. apparently these things break if you look at them wrong :stuck_out_tongue: lucky radiosack carries them now!

First off, why not try something a little more obvious than what is in your for loop, eg

cm = ping(5);
...
cm = ping(6);
...

Secondly, Parallax Pings require 5V, so I assume you're using a 5V Arduino.

Thirdly, you may need some additional noise-filtering on the 5V buss, like a 47-uF capacitor,
as the Pings draw a pulse of current when firing.

Fourthly, and this is just a stab in the dark, I don't like the idea of having the pingpin floating
as an input most of the time, and prefer to have it as an output sitting at 0V most of the time,
instead. And you might also try a longer trigger pulse, eg

in Setup(), for both pings:

  pinMode(pingPin, OUTPUT); 
  digitalWrite(pingPin, LOW);
....

int ping(int pingPin) {
  long duration, cm;

  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT); 
  duration = pulseIn(pingPin, HIGH);

  pinMode(pingPin, OUTPUT); 
  digitalWrite(pingPin, LOW);

  cm = microsecondsToCentimeters(duration); 
  return cm ;
}

oric_dan:
First off, why not try something a little more obvious than what is in your for loop, eg

cm = ping(5);
...
cm = ping(6);
...

Secondly, Parallax Pings require 5V, so I assume you're using a 5V Arduino.

Thirdly, you may need some additional noise-filtering on the 5V buss, like a 47-uF capacitor,
as the Pings draw a pulse of current when firing.

Fourthly, and this is just a stab in the dark, I don't like the idea of having the pingpin floating
as an input most of the time, and prefer to have it as an output sitting at 0V most of the time,
instead. And you might also try a longer trigger pulse, eg

in Setup(), for both pings:

pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
....

int ping(int pingPin) {
  long duration, cm;

digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);

cm = microsecondsToCentimeters(duration);
  return cm ;
}

Hi, your code worked, but the readings that I get are jumping all around, from high to low, low to high every second. Is there anyway way to make it more stable or did you add a capacitor or something?

I added a 0.47uF capacitor in parallel to +5V and GND, a 100ohm resistor to +5V. Readings seems more stable but increases delay, making the readings inaccurate. Please advise.

Here is my code:

Hi, your code worked, but the readings that I get are jumping all around, from high to low, low to high every second. Is there anyway way to make it more stable or did you add a capacitor or something?

I added a 0.47uF capacitor in parallel to +5V and GND, a 100ohm resistor to +5V. Readings seems more stable but increases delay, making the readings inaccurate. Please advise.