Pixhawk HC-SR04 sensor Connection

Hi All,

I am trying to connect HC-SR04 ultrasonic 4 pin sensor to pixhawk flight controller for obstacle detection.
HC-SR04 has 4 pins: 5V Supply, Trigger pulse Input, Echo pulse output, Ground.
Can we connect this sensor on “Serial 4/5 port” as follows:
Connecting the RX line of the serial 4/5 to the Echo pin of HC-SR04 and TX line of
the serial 4/5 to the trigger pin of HC-SR04 and connecting the GND and
5V lines.

The echo pin of HC-SR04, sends 5V to Rx of serial4/5.
As per the specification of pixhawk, in serial4/5 TX/Rx pins are +3.3V,
so when the Rx receives the +5V from the echo pin, will pixhawk be able
to tolerate +5V.
As per “https://pixhawk.org/modules/pixhawk” under the section
"Console Port:-
The system’s serial console runs on the port labeled SERIAL4/5. The pinout
is standard serial pinout, to connect to a standard FTDI cable (3.3V, but its 5V tolerant). "

So kindly confirm is this connection of HC-SR04 and serial 4/5 correct so
that I can proceed with writing the driver for the same.

Thanks and Regards,
Saloni

Not sure if this helps you but there seems to be a driver for it on PX4: https://github.com/PX4/Firmware/tree/master/src/drivers/hc_sr04

Unfortunately there aren’t as far as I know any schematics for the pixhawk in any easily accessible format, but if TX( out of pixhawk) is driven by the stm32 direct which runs at 3.3 V , then it wont be 5V tolerant for CMOS inputs which require 3.6 V or so for a logic high level, assuming a 5.5 v power supply as worst case. CMOS logic includes many modern 5V ic’s).
Some may say otherwise and it It may work or may seem to work, but you only need to drop a few bits to have problems

The 5 V tolerance is on the STM32 inputs, most of which can accept a 5V input without issues.

@gregd72002, Thanks for the response.
I think, the driver written for hc_sr04 is not for px4fmu_v2, as I have compiled the code by doing the entry for hc_sr04 in cmake/configs/nuttx_px4fmu-v2_default.cmake, but it is giving the error, ‘GPIO_GPIO6_OUTPUT’ was not declared in this scope.
I guess it is written for mindpx board as I could see its entry in cmake/configs/nuttx_mindpx-v2_default.cmake

Thanks and Regards,
Saloni

@skyscraper, Thanks for the response.
Can you please confirm, if I can connect hc_sr04 with pixhawk through its GPIO pins as follows:
AUX row 5 (bottom) Trigger pin for sensor
AUX row 6 (center) Power supply
AUX row 6 (top) Ground
AUX row 6 (bottom) Echo of the sensor
But in this connection I have a doubt that since by default AUX pins are digital output pins, so how I can convert AUX row 6 (bottom) in INPUT pin so as to read ECHO signals from the sensor.

Or alternatively I can connect,
AUX row 5 (bottom) Trigger pin for sensor (output pin)
Power Supply, Ground and Echo(Input) with ADC3.3V

Please help me in understanding the connection.
Thanks and Regards,
Saloni

@salonijain12

It really needs a schematic of the FC to know whether it is possible. What range of pulse lengths does the sensor output?

Sensor output - Echo pulse: 5V
Please find attached link of datasheet:
http://www.micropik.com/PDF/HCSR04.pdf

OK, looking at the data sheet, you need to

a) send a pulse on one pin and start a timer
b) wait for a return pulse and read the timer.

There is a somewhat similar rangefinder in the ArduPilot code.

https://github.com/ArduPilot/ardupilot/blob/master/libraries/AP_RangeFinder/AP_RangeFinder_PX4_PWM.cpp

You could try attaching a flip flop to the trigger and echo leads, then sending a pulse periodically on the trigger lead to set the flip flop and attach the output of the flip flop to the input expected by that PWM driver. I dont know what it should connect to on the PixHawk though, you need a schematic of the FC pins for that

Actually, you could have fun making a circuit to make this work using a 555 timer and a flip flop as described above. The device would run itself then, so all you would need would be a single input to the Pixhawk. Alternatively to an analog circuit, you could do similar with an Arduino or other simple microcontroller to turn the signal into something the Pixhawk understands

Hi, I am currently thinking of connecting it directly to Pixhawk instead of using the Arduino Board UNO.
Have you found a way to connect it directly?
As currently I am in the midst of thinking to connecting both Trig & Echo Pin to Aux 6 (bottom pin) with additional diodes to each pin, and using coding to send digital pulse to trigger the sensor while receiving from echo pin.

Both Trig & Echo Pin are TTL pulses they are digital pulse thus, I think it should be possible to connect them to GPIO pin (AUX)

Best Regards,
Kevin

i think there is no other way of connecting sr04 than using an additional controller. There are plenty of howto’s on how to build sr04 to i2c controller:
http://projectsfromtech.blogspot.co.uk/2014/01/i2c-hc-sr04-sonar-module-attiny85-i2c.html
https://gist.github.com/ajfisher/1d57c5f845c376f04fbb

Then we need px4 to support it. But this should not be difficult.

Hi Greg, as i want install the HC-SR04 already soledered with the board with the cable for apm, i need know if i must use it with pixhawk 2.4.6 and the settings on mission planner.

Hello! i just found this link; hope it helps! https://www.aliexpress.com/item/PIXHAWK-PIX-APM-Flight-Controller-UltrasonicPosition-Module-Digital-I2C-Module/32812060782.html

please, I want to Know how can edit PX4 Confg. after Connecting more than one of HC-SR04 ultrasonic for obstacle Avoidance.

You are in the wrong forum for PX4.