Ping Sensor Frequency

I have a few ping sensors facing the same direction and was wondering if there is a way to change the frequency the sensors are chirping so they don't interfere with each other.

It might be easier to just sample them in sequence.

You could do neat things if the interfered....like

  • setting up an ultrasound communication channel - beeping the bits over by ultrasound

  • have the two pings at a known interval (e.g. 25 cm / 10 inch behind each other) and measure the dfference of arrival times.
    I think you can increase accuracy of the measurement.

  • or one receiver to the left and one to the right of the source.

  • making an interferometer, look for the border between interference and not. could possibly detect minimal movements.

just thinking out loud

just trigger one, then the other. Could someone advise whether bringing the pingpin back to low when the pingpin is in input mode is necessary? I figured it would be so that it still isn't reading while the next sensor is working. I added that into this code. This is untested, but it compiles. Let us know how it works.

const int pingPinLeft = 7;
const int pingPinRight = 8;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in centimeters:
  long duration;
  int cmL, cmR;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPinLeft, OUTPUT);
  digitalWrite(pingPinLeft, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinLeft, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinLeft, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPinLeft, INPUT);
  duration = pulseIn(pingPinLeft, HIGH);

  // convert the time into a distance

  cmL = microsecondsToCentimeters(duration);
  Serial.print(cmL);
  Serial.print("cmLEFT");
  Serial.println();
  delay(100);
  digitalWrite (pingPinLeft, LOW);   // I don't know if this command is necessary, but it would make sense so it doesn't continue
                                                      // to read while the next sensor is working                                                              
  //////////////////////////////////////////////////////
  delay(1000);  //Time between sensor readings to "clear the airwaves"... so to speak. Could be much less or maybe is not needed.
  ///////////////////////////////////////////////////////
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPinRight, OUTPUT);
  digitalWrite(pingPinRight, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPinRight, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPinRight, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPinRight, INPUT);
  duration = pulseIn(pingPinRight, HIGH);

  // convert the time into a distance

  cmR = microsecondsToCentimeters(duration);
  Serial.print(cmR);
  Serial.print("cmRIGHT");
  Serial.println();
  delay(100); 
  digitalWrite (pingPinLeft, LOW);   // I don't know if this command is necessary, but it would make sense so it doesn't continue
                                                      // to read while the next sensor is working                           

}



long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

If you are using shared ping and measure pins then you need to bring the pin low just to measure. Thus that seems necessary...
Or are you talking about the low period before the trigger pulse?

I made comments in the code where I was wandering whether the command was needed or not . The commands/comments are just after the distance gets printed to the serial monitor for each sensor.

No, that write is not necessary. In fact, all it will do, is turn off the internal pull-up resistors, if they are on, because the pinMode hasn't changed since the pin was turned to input.

Also, because you already do a delay(100), there is no need to do a delay(1000) as well. Take out that second, longer, delay and you'll probably be fine.

Printing lots to the serial port may end up slowing you down. Make sure to use a high baud rate (57600, 115200) to minimize the impact of that, and make printed strings be sufficiently short.

I would be concerned that the first sensor is still in input mode ready to receive while the second sensor is firing pulses.