Poll
Question: How to use multiple ultrasonic sensor in Arduino?
?? - 0 (0%)
??? - 1 (100%)
Total Voters: 1

Pages: [1]   Go Down
Author Topic: Problem with multiples Parallax PING))) Ultrasonic Sensor  (Read 1020 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
}



« Last Edit: February 26, 2012, 10:11:13 pm by ZacOng » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 159
Posts: 2916
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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
 
Code:
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 ;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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
 
Code:
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:
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.
Logged

Pages: [1]   Go Up
Jump to: