Using two Ping sonars with an Arduino

Hi ~!

like above title, I want to use dual ping sonar with arduino leonardo .
We are making source codes using Arduino’s two serial ports at the same time and Arduino codes provided by Blue Robotics.
However, as in the code below, the value of serial 1 appears to be only 0.
Since data values appear when you connect to tx and rx alone, I don’t think it’s a problem with board pins, and I think there’s a problem with source code, can you help me?

/**
 *  This example is targeted toward the arduino platform
 *
 *  This example demonstrates the most simple usage of the Blue Robotics
 *  Ping1D c++ API in order to obtain distance and confidence reports from
 *  the device.
 *
 *  This API exposes the full functionality of the Ping1D Echosounder
 *
 *  Communication is performed with a Blue Robotics Ping1D Echosounder
 */

#include "ping1d.h"

#include "SoftwareSerial.h"

// This serial port is used to communicate with the Ping device
// If you are using and Arduino UNO or Nano, this must be software serial, and you must use
// 9600 baud communication
// Here, we use pin 9 as arduino rx (Ping tx, white), 10 as arduino tx (Ping rx, green)
static const uint8_t arduinoRxPin1 = 10;
static const uint8_t arduinoTxPin1 = 11;
static const uint8_t arduinoRxPin2 = 8;
static const uint8_t arduinoTxPin2 = 9;
SoftwareSerial pingSerial1 = SoftwareSerial(arduinoRxPin1, arduinoTxPin2);
SoftwareSerial pingSerial2 = SoftwareSerial(arduinoRxPin2, arduinoTxPin2);
static Ping1D ping1 { pingSerial1 };
static Ping1D ping2 { pingSerial2 };
static const uint8_t ledPin = 13;

void setup()
{
    pingSerial1.begin(115200);
    pingSerial2.begin(115200);
    Serial.begin(115200);
    pinMode(ledPin, OUTPUT);
    Serial.println("Blue Robotics ping1d-simple.ino");
    /*while (!ping1.initialize()) {
        Serial.println("\nPing device failed to initialize!");
        Serial.println("Are the Ping rx/tx wired correctly?");
        Serial.print("Ping rx is the green wire, and should be connected to Arduino pin ");
        Serial.print(arduinoTxPin1);
        Serial.println(" (Arduino tx)");
        Serial.print("Ping tx is the white wire, and should be connected to Arduino pin ");
        Serial.print(arduinoRxPin1);
        Serial.println(" (Arduino rx)");
        delay(2000);
    }*/
}

void loop()
{
    if (ping1.update()) {
        pingSerial1.listen();
        Serial.print("Distance1: ");
        Serial.print(ping1.distance());
        Serial.print("\tConfidence1: ");
        Serial.println(ping1.confidence());
    } else {
        Serial.println("No update received!");
    }
    if(ping2.update()){
      pingSerial2.listen();
        Serial.print("Distance2: ");
        Serial.print(ping2.distance());
        Serial.print("\tConfidence2: ");
        Serial.println(ping2.confidence());
    }
    // Toggle the LED to show that the program is running
    digitalWrite(ledPin, !digitalRead(ledPin));
}

Hi @boroneo, welcome to the forum :slight_smile:

I assume the issue is that you’re using the same transmit pin (TxPin2) for both serial connections?

It’s worth noting that using two acoustic devices at the same time can cause interference. The best approach when using two Ping1Ds is to disable pings of one while you read from the other, then switch which one is enabled :slight_smile:


By the way, I’ve formatted your code in a code block, so it’s easier to read, and can be copied for testing. You can read about how to do that in the Formatting a Post/Comment section of the How to Use the Forums post :slight_smile:

Thanks your reply, I change my source code in pin number… but still same situations…
and how can I disable pings ?? is it reference code?

I’ve looked a bit more into this, and there are a couple of issues with your current approach:

  1. You’re calling pingX.listen(); after you try to get it to update (you need to switch the active port before you try to communicate with a different one)
  2. You’re using software serial for your pingSerial connections with 115200 baudrates, which can have problems (115200 only works consistently with hardware serial, which the Leonardo doesn’t have). With software serial it’s best to use 9600 baud instead.

Do you know which firmware your Ping Sonars are running? Our Using the Ping Sonar with an Arduino guide has steps on how to update the firmware to one that uses/supports 9600 baud. The latest firmware (V3.28_auto) supports both 115200 and 9600, but to use 9600 you need to add pingSerial.write(0b01010101); just after pingSerial.begin();. Our ping-arduino library should soon be getting an update so that extra line is done automatically, but it’s necessary for now.

Disabling pings isn’t in our basic examples, because most users don’t need to do it. set_ping_enable (which I linked you to in my previous comment) is the relevant Ping Protocol message that controls when to enable/disable pings. The corresponding ping-arduino function is documented here. You can use it by ping.set_ping_enable(true); to enable, and ping.set_ping_enable(false); to disable.