Connecting ultrasonic rangefinder (Sonar) to Cleanflight and INAV

Keeping constant altitude with a drone is not a trivial task. Specially, if drone is supposed to keep give altitude very precisely few meters above the ground. One of the reasons for it is difficulty of reading precise altitude. Barometer can drift when atmospheric pressure changes and can produce a lot of noise. GPS is very inaccurate when dealing with altitude. One of the options is to use some kind of rangefinder. Ultrasonic for example. Cleanflight and its derivatives supports exactly one kind of those: cheap HC-SR04 sonar. There are plans to integrate different sonars, but none of official builds supports it yet.


HC-SR04 has 4 pins:

  • Vcc – 5v power supply
  • Trig – input signal in this pin sends ultrasonic wave
  • Echo – when ultrasonic wave bounces from solid surface and returns to sonar, logical level HIGH is sent on this pin. So, to measure distance, one must measure time between Trig and Echo
  • Gnd – this one is self describing
    “Problem” with HC-SR04 and modern flight controllers based on STM32 is that while this sonar uses 5V logic, usually FC’s inputs are not 5V tolerant. They are 3.3V. Applying 5V to them might burn flight controller. This is why inline 1kOhm resistors have to be used on Trig and Echo lines. Like this:
Related article:   Soldering, rosin, flux and a Stannol x32-10i flux pen



Depending on flight controller hardware, HC-SR04s Trig and Echo has to be connected to different pins:

  • Naze32 and Flip32 when parallel PWM radio is connected (this is the only case when resistors are not need. Those pins are 5V tolerant):
    • Trig: Motor 5
    • Echo: Motor 6
  • Naze32 and Flip32 when PPM or Serial RX is connected
    • Trig: RC7
    • Echo: RC8
  • CC3D
    • Trig: RC4
    • Echo: RC5
  • SPRacingF3
    • Trig: RC4
    • Echo: RC5

Since the same pins can be used by other features like SoftSerial or Parallel PWM RC, depending on hardware, some of those function can not be used with Sonar.


When hardware setup is done, enabling sonar in Cleanflight Configurator is pretty simple:

  1. Open Configuation page and enable SONAR configurator sonar enable
  2. After saving, sonar icon in top bar should go green enabled sonar
  3. Sonar readouts will be visible in Sensors tab sonar sensor readouts

OK, I have sonar, what now?

Well, this is very good question. Since FC now has a way to measure its altitude above ground, altitude hold Sonar mode will be available. In theory, it should work very good and allow altitude stabilization with centimeter accuracy. Unfortunately, this is not that simple, and there are a few reasons for that:

  1. In perfect conditions, HC-SR04 can measure distance up to 4 meters. Conditions onboard drone are not perfect. Disturbed air and noise can lower this range to less than 1 meter
  2. Measurement cone is only about 45 degrees. That means, if surface (ground) is tilted above 22.5 degrees, measurement will probably fail
  3. Accuracy and range above grass or any not very solid surface (like concrete or asphalt) will be greatly reduced
    Bottom line is: HC-SR04 onboard UAV is more less useless. The only real life usage is assistance in last phase of automated landing. Since Cleanflight does not supports automated landing, sonar is more less only a gadget.
Related article:   Working solution for USBasp driver in Windows 10 64bit

BTW, do you want to know what flight controller software supports automated landing? iNav.

14 thoughts to “Connecting ultrasonic rangefinder (Sonar) to Cleanflight and INAV”

  1. Doing my best to replicate what you have documented here. I have an SPRacing F3 MOF v1 Flight Controller, and I have a version of Cleanflight pre 2.0. I have the same Sonar sensor model, and am connected as per your SPRacing configuration above. I can see that the sonar icon on the top bar is illuminated, and I can see the scrolling chart in the sensors tab. I also have the mode tab configured to enable sonar. The problem is that the sonar value on the chart does not change when I put my hand over the sensor. I assumed I would at least see a change inthe scrolling value. That said, if I pick up the quad and tilt it, the sonar chart changes, and then goes back when I level the quad.

    Did you change anything in the UART configurations? Any other changes that are not documented here?


  2. Hi,

    I know where to connect the Sonar to trigger and echo in my SP Racing F3 Deluxe, but where will I connect the Ground and VCC into my board please?

  3. I have noticed you don’t monetize your website, don’t waste
    your traffic, you can earn additional cash every month because
    you’ve got hi quality content. If you want to know how to make extra $$$, search for:
    Boorfe’s tips best adsense alternative

  4. Hi,

    I have a Naze32 v6, and for the life of my I cant get it to work.
    I’ve connected to the ESC pins of the motor, Pins 5&6, Gnd to Gnd, 5v to Vin, but nothing.
    Done RC7 & 8 with 1kO resistors, nothing.
    Done RC 4&5, nothing.

    I even soldered to the trig and echo pins on the bard directly, nothing.

    Here’s some photos of it:

    Any ideas why this is not working?

  5. Hi,
    I’m new and have a lot of questions.

    I will connect sonar with FC Matek F405 STD with iNav 1.9 downloaded from

    nav_1.9.0_MATEKF405-Softserial Softserial1_TX on TX4 pad, softserial1_RX on RX4 pad

    If sda/scl and rx4/tx4 are used by GPS bn880,
    how can i connect the sonar HC SR04

    I found this:…/docs/

    Now i need to know if and where i can do the same on the f405std board

    And if it need this too :…ster/

    Can i use softserial? And if i can on what sort of pins?


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.