Connecting Low-Cost Sonar Range finder with Pixhawk Quadcopter

Connecting Low-Cost Sonar Range finder with Pixhawk Quadcopter

I want to share the procedure to install a low-cost sonar range finder or ultrasonic sensor in a pixhawk quadcopter. I bought this sonar from Aliexpress. It costs around USD 5 or 7. You can go to the website and search for “GY-US42 i2c Pixhawk Flight Control Ultrasonic Distance Measurement Module”. I don’t want to post any links as it may get outdated.

Hardware Setup

ultrasonic sensor pixhawk
Ultrasonic Sensor

It’s a simple plug and play sensor. We need to connect this sonar in the I2C port of pixhawk. We need a 4 pin DF13 cable of around 20 cm long. First, cut and remove one end of the DF13 connector.

df13 cable
4 Pin DF13 Cable

Solder this end of the cable with the sonar with the help of pinout and connection diagram as shown below. For better understanding, I am following the same colour code of cables for all the connection and pinout diagrams. But white colour cable is shown as pink in other diagrams.

ultrasonic sensor pinout diagram
Ultrasonic Sensor Pinout Diagram

We need only first 4 connections for I2C port. Apart from I2C mode, the sensor can work in PWM and UART mode also. That is beyond the scope of this article.

df13 cable soldered with sonar
Ultrasonic Sensor

After soldering, the sonar is ready to connect with pixhawk.

DF13 Cable Soldered with Ultrasonic Sensor
DF13 Cable Soldered with Ultrasonic Sensor

Now connect sonar in the I2C port. There is only one I2C port in pixhawk. If it is already occupied, you can use an I2C splitter. The working voltage of the sensor is 3-7V. So the output from pixhawk is sufficient for continuous operation. The hardware setup is complete now.

Ultrasonic Sensor Connection Diagram with Pixhawk
Ultrasonic Sensor Connection Diagram with Pixhawk

In my pixhawk, I2C port is occupied by an external compass. So I used an I2C splitter as mentioned before and fixed sonar pointing down so that I get distance measured from ground.

Sonar Connected to Pixhawk
Sonar Connected to Pixhawk

Software Setup

I am using mission planner as ground station software in windows operating system and ArduCopter V3.6.9 firmware. The working range of this sonar is 20-720 cm. That means it cannot measure distance below 20cm and above 720cm. So the range and type of the sonar to be instructed to the pixhawk flight controller. Changing these settings can be done through Mission Planner. First, connect pixhawk with the Mission Planner and then open the Config/Tuning >> Full Parameter List page and set the following parameters:

  • RNGFND_MIN_CM = “20” (i.e. 20Cm min range)
  • RNGFND_MAX_CM = “700” (i.e. 7m max range)
  • RNGFND_TYPE = “2” (MaxbotixI2C sonar)
Parameter Settings in Mission Planner
Parameter Settings in Mission Planner

Next goto Initial Setup >> Optional Hardware >> Range Finder. From the dropdown menu, select MaxbotixI2C as the range finder type. After restarting pixhawk, we can read the distance also from this location.

Selecting Sonar Type
Selecting Sonar Type

Now we have finished with the software setup also. Restart the pixhawk and we’re good to go.

Testing Sonar

After restarting pixhawk, goto Initial Setup >> Optional Hardware >> Range Finder. We can see the distance measured there. A screenshot is attached below. Here the distance is shown in meters. Also, note that the minimum distance measured is 20 cm or 0.2 m. So any distance less than 20 cm also will be displayed as 20 cm only.

Distance Measured by Sonar
Distance Measured by Sonar

Distances measured by the sonar sensor can also be read from the Mission Planner’s Flight Data screen’s Status tab. Look for “sonarrange” (show reading in meters) or “rangefinder1” (show reading in centimetres) in the status window. Please refer to the figure below.

Pixhawk sonar range finder
Distance Measured by Sonar

13 thoughts on “Connecting Low-Cost Sonar Range finder with Pixhawk Quadcopter

  1. Hi, I got the exact same range finder and connected it with an I2C splitter as you did to the Pixhawk 4 mini, but the selection menu is completely empty.
    Initial Setup >> Optional Hardware >> Range Finder. From the dropdown menu

    I see that you can choose a lot of different rangefinders but I see nothing there, can you help please?

      1. Hello Firoz,

        Thanks for the reply!
        The problem is that I cannot select anything in the dropdown box because it’s empty.
        When I look at your picture I see a whole lot of rangefinders to choose from.
        Have you got any idea what’s wrong?

        Regards,

        Orson

        1. Dear Orson,

          Sorry for the mis-understanding. What about the GCS, which one are you using? Is it mission planner? Also what about the flight controller?

          Firoz

          1. Hi Firoz, I’m using Mission planner 1.3.74 and QGC v4.0.10 on a Pixhawk 4 mini with PX4 fw 1.11.0.
            In QGC there is no possibility at all to add a sensor, and in Mission planner I cannot select anything at the range finder setting.

            Regards,

            Orson

  2. Hi Firoz, I’m using Mission planner 1.3.74 and QGC v4.0.10 on a Pixhawk 4 mini with PX4 fw 1.11.0.
    In QGC there is no possibility at all to add a sensor, and in Mission planner I cannot select anything at the range finder setting.

    I enabled SENS_EN_MB12XX from 0 to 1

    mb12xx status
    INFO (SPI_I2C] Running on I2C Bus 2
    12xx_sample_perf: 0 events
    12xx_comms_error: 0 events, 0us elapsed, 0.00us avg, min 0us max 0us 0.000us rms
    INFO imbl2xx] poll interval: 100ms
    INFO imbl2xx] sensor: 0, address 112
    nsh>

    But no distance sensor showing at the sensors menu

    Regards,

    Orson

  3. Update in Mission planner when I open Proximity from the advanced menu, I see the sonar actually working correctly, but I still can’t see it in the sensor menu.

  4. Hello Firoz,

    I got the sensor working, it was actually easier than I thought, I only had to set the SENS_EN_MB12XX from 0 to 1 and in the safety menu and I enabled collision prevention.

    I think the sensor is very sensitive or inaccurate when in POS HOLD mode, do you have a guide to tune this? I noticed a
    potentiometer on the back, can you please share your experience how to do this?

    Regards,

    Orson

    1. Dear Orson,

      From my experience the sensor worked without any tuning or adjustment. I hope you are aware of 3 modes of working with the sensor. I am using I2C mode, which does not require any adjustments. But for ‘UART’ and ‘PWM’ mode you need to solder the ‘PS’ accordingly. This is for the sensor with me. I have sent a detailed product manual of the same from Chinese maker to your email id. Content is in Chinese, but you can use ‘Google translate’ to read it in English. Hope it helps.

      Firoz

    1. Dear bessaoudi abdellah,

      I have sent you an email with the maker manual attached. Manual says all methods of operation and connections.

      Firoz

Leave a Reply

Your email address will not be published. Required fields are marked *

    HP P