Connecting ultrasonic rangefinder (Sonar) to Cleanflight

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.

dav

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:

HC-SR04

dav

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.

dav

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.

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

5 thoughts on “Connecting ultrasonic rangefinder (Sonar) to Cleanflight

  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?

    Thanks!

  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?

Leave a Reply

Your email address will not be published.