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
Hardware Setup
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.
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.
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.
After soldering, the sonar is ready to connect with
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.
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.
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)
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.
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.
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.
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?
Dear Orson,
You can try the one named “MaxbotixI2C” as the range finder type.
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
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
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
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
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.
Dear Orson,
I work with Ardupilot firmware.
It would be better to raise the issue in the PX4 forum.
https://discuss.px4.io
Firoz
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
This is the graph from the sensor:
https://we.tl/t-DEWTAFrFjo
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
Hi,
Can you please share the methide with “UART” and “PMW” mode.
thank you.
Dear bessaoudi abdellah,
I have sent you an email with the maker manual attached. Manual says all methods of operation and connections.
Firoz