HC-SR04 Sonar & APM 2.8

I have seen mixed answers on whether or not I can use HC-SR04 Sonar with my APM 2.8. It is not in the list of supported sensors but I’m pretty sure it gives out an analog voltage so shouldn’t it work? I would like to use it for forward facing anti-collision. I have attached a wiring diagram that I made and would like to know if it is correct. Also, what settings would I have to change to enable it? Thanks,

The HC-SR04 doesn’t work with the APM for some reasons:

  1. It doesn’t return an analog voltage.
  2. It works by sending a signal at trigger and awaiting a return at echo. And here is the second problem: You would have to use the pulseIn() function which stops the code until the signal is received, so the copter wouldn’t react to changes and crash.

There are boards with a small arduino that convert the signal from the HC-SR04 to a analog voltage, but in my experience this don’t work really well…

1 Like

Those questions pop up time and time again. The HC-SR04 is cheap, but that is about all that speaks for it. You will have to buy additional hardware to make it work with an autopilot that is not supported anymore. So you are investing in a dead end. There are low cost flightcontrollers available today, which will support the latest firmwares and lidar sensors, which are also not as expensive as they used to be.


1 Like

Yeah, I already have the APM 2.8 and I would like to keep using as it is a decent board. I have seen a few videos and articles of people using them on APM though? I am just hoping to be able to use some cheap gear I already have. Thanks,
Ex, https://www.dronetrest.com/t/using-sonar-hc-sr04-to-avoid-ground-collision/6000
Ex2, https://www.youtube.com/watch?v=aNZLQPg6CEE

The performance is so bad as to not be worth the effort. But for $4 it will make a interesting Christmas tree ornament.

@ Carter7868, yes your wiring diagram works, and the pin number of A0 is 54, (Archived:APM 2.5 and 2.6 Overview — Copter documentation, the section: Digital output pins (Archived:APM 2.5 and 2.6 Overview — Copter documentation)

You can use the following code to (scan pins) verify:

// Include NewPing Library
#include "NewPing.h"

// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10
#define TRIGGER_PIN 54
#define ECHO_PIN 54

// Maximum distance we want to ping for (in centimeters).
#define MAX_DISTANCE 400  

// NewPing setup of pins and maximum distance.

void setup() {

int pin = ECHO_PIN;

void loop() {
  NewPing sonar(pin, pin, MAX_DISTANCE);
  float dist = sonar.ping_cm();
  //if (dist != 0) {
  Serial.print(" Distance = ");
  Serial.println(" cm");
//  pin = (pin + 1) % 128;  // scan pins


this is a 4 year old post, and he wasnt talking about using it as a sensor board, the standard sr04 is not supposed in any way by the apm when running ardupilot. the code you posted is just example code for the sr04.

the videos he showed are not using a standard SR04 sonars but a modified version with an backpack that converts it to analogue.

It works with standard SR04, but you just need to code it yourself.

From the doc: the analog pins can be used as digital pins, by adding 54 to the pin number:

Digital output pins(Archived:APM 2.5 and 2.6 Overview — Copter documentation)

The APM2 uses the same set of 9 analog input pins as digital output pins. They are configured as digital output pins automatically when you start to use them as digital outputs.

Pin 54 to 62: You need to add 54 to the pin number to convert from an analog pin number to a digital pin number. So pin 54 is digital output pin on the A0 connector. Pin 58 is A4 etc.

your not just running a rangefinder in arduino, you have to integrate it into the flight control software in a way that doesnt interfere with any other function and in a way that doesnt cause the cpu to lag. my understanding is that because the sr04 is so basic it takes a lot of resources like timers to run it thats why it was never included with the apm firmware.

Not sure when that decision is made, now you can use ( teckel12 / Arduino New Ping / wiki / Home — Bitbucket ) which is faster (no pulseIn)

NewPing :
Works with many ultrasonic sensors, can communicate using only one pin, very low lag, fast (up to 30 pings per second), timer interrupt method for event-driven sketches, light code, and much more.

are you just posting chat GPT again?

your assuming that the timers are not being used already. also the later versions of ardupilot compatible with an APM are not arduino compatible, so much had been hand optimised to try and get it to fit. even earlier versions needed a custom version and custom versions of libraries to get it to work, its not as simple as just opening it in arduino and clicking build.


Trying make it work. As I said “you just need to code it yourself”.