HC-SR04 connection and usage
« on: May 01, 2017, 08:19:27 am »
Hi all,

I am looking for info on how to connect the HC-SR04 to the Revo and use it with LibrePilot.

Can someone point me in the right direction please ?



  • *
  • 808
Re: HC-SR04 connection and usage
« Reply #1 on: May 01, 2017, 08:31:02 am »
This looks like WIP
See here https://forum.librepilot.org/index.php?topic=767.0
and here https://librepilot.atlassian.net/browse/LP-225

Code use to be working and Cliff was flying with sonar already, but you would need to merge that branch and test it.
From what I remember his sonar had a very wide beam and wasn't very good sensor, but it worked ok flying slowly over stairs, copter was adjusting altitude (he showed youtube link year ago). Probably some better results can be achieved with more expensive sonar with more narrower beam (maybe some of these http://www.maxbotix.com/Ultrasonic_Sensors/MB1010.htm).

Maybe ask under first link ? Devs interested in that part of the code should get a notification if you quote them.

Re: HC-SR04 connection and usage
« Reply #2 on: May 02, 2017, 06:15:25 am »
Using a sonar requires that it tell you when it is out of range.  There is a standard way of it saying it is out of range.  The HC-SR04 and similar inexpensive sensors fail to do this when there is prop noise in the environment.

Supposedly the MaxBotix units handle this correctly.  I bought one (a 1240) after finding out that the HC-SR04 worked when you hand carried it, but not when it was flying.  I never even plugged it in.  I would start with a Maxbotix or at least prove to yourself that the cheap ones don't do the out of range correctly (before wasting a lot of time like I did).

Quote from Maxbotix: If you have a powerful quadcopter and would like to range the ground, MaxBotix Inc., recommends the MB1340, or the MB1240 XL-MaxSonar-EZ sensors. If you prefer using the I2C output, the recommended sensor is the MB1242.

Re: HC-SR04 connection and usage
« Reply #3 on: May 02, 2017, 06:39:22 am »
Oh wow. No wonder it's hard to getting info.

The maxbotix sensors are like $100 a piece. The cheep Hc-sr04 is only $1.

I Will read a bit more from the links provided. But it's rather more than I expected.


Re: HC-SR04 connection and usage
« Reply #4 on: May 02, 2017, 07:24:44 am »
The 1240 is the one you want because it has high power, narrow beam, continuous calibration, noise rejection.

I  $40for mine.  eBay.

They are $45 shipped now.  That is about what Maxbotix quotes ($39.95 plus shipping).

Re: HC-SR04 connection and usage
« Reply #5 on: May 11, 2017, 04:44:50 pm »
Hi Guys, my first post in forum..  :)

Cliff, did you have any success yet with that sensor? I would like to try also.


Re: HC-SR04 connection and usage
« Reply #6 on: May 11, 2017, 07:41:18 pm »
I wrote some code that modifies the baro altitude whenever sonar is in range, but only tested it with HC-SR04 series of sonar.  I ordered and received a MaxBotix 1240 sensor, but have not played with the MaxBotix since I received it.

When flying altitude hold within sonar range, it follows terrain so going up a hill it will climb and going down a hill it descends to maintain the same height above the ground.  There is a lag in response that you must tune out (altitude PID) or flying moderately fast up hill it will hit the ground before responding.  Likewise flying moderately fast down hill it will get so much distance from the ground that it looses sonar signal and switches to baro (and thus simply maintains absolute height above sea level).

It smoothly manages transitions from baro to sonar and back.  If you are flying altitude hold within sonar range of the ground and suddenly fly off the top edge of a cliff, it switches from sonar to baro and the altitude remains the same.  Likewise if you are flying altitude hold outside of sonar range (on baro) and suddenly you are in sonar range (you came back after flying off the cliff), it starts to track the sonar range and follow ground terrain.

If you know git and build your own code, the old code is available in git at: theothercliff/LP-225_sonar_support
Don't bother trying it with HC-SR04, you will just get frustrated...  ;)


Re: HC-SR04 connection and usage
« Reply #7 on: May 06, 2018, 04:54:37 pm »
So, using HC-SR04 is totally bad idea for Position hold?

Re: HC-SR04 connection and usage
« Reply #8 on: May 06, 2018, 05:43:00 pm »
HC-SR04 hears prop noise and thinks it is 0.75m above ground for anything above that (with motors running).  It should work to 3m with motors running and it should say "out of range" when it can't see the ground any more.  It does this just fine as long as motors are not running.

PositionHold is a 3D position hold GPS flight mode.  Baro works fine for that.  Sonar would not be very helpful.  On the other hand, VelocityRoam with Sonar in place of baro (and in place of GPS altitude) when within sonar range would be nice.

There are thrust modes called AltitudeHold and AltitudeVario where the sonar data can be merged.  That is what you see working in the video.