How to use analog-pwm rangefinder (sonar) on Pixracer

Is there any analog or PWM input pins available for rangefinder connection on the PixRacer?
I only find 2 analog input pins, but they are for voltage and current measurements.

You should be able to make two of the PWM pins usable for input by setting the BRD_PWM_COUNT to 4 or 5. This will leave the last two (or one) pwms for use as input. I think they’re only usable as pwm inputs, not analog inputs as far as I know.

Hi, thank you for the information, helps a lot.
I assume this means, i can use 4 outputs for motors, 1 for controlling a tilt servo for my camera and a pwm rangefinder on 6th at the same time.
Great news!

I havent find this information in the documentations available. May i help by improving these docs somewhere? If so, can you please point me to the right page to add this?


Hi Randy,
BRD_PWM_COUNT=5 leads to my servo not getting any signal on the 5th pin.
Is this supported by the code? MP says:

0:No PWMs 2:Two PWMs 4:Four PWMs 6:Six PWMs 7:Three PWMs and One Capture
With 6, servo works, 5 may be not accepted.

I’m using 3.4-RC2.

You are correct, 5 isn’t an accepted value.

Decided to give a try, and add the possibility to have 5PWM.
I’m still not familiar with the codebase, and git submodule dependencies.
After a short investigation, it seems i need to change the PX4 firmware, which im started, but i see some major problems implementing this.
Current master of ardupilot is using an older version of PX4Firmware, which changed a lot since than. To have fix, I will need to fix and create pull requests to PX4Firmware repository. Am I right?
The latest PX4Firmware does not build, there is some makefiles missing.

Yes, ArduPilot has it’s own fork of PX4Firmware and any change needs to be a PR directed to it. From time to time we pull upstream changes in and you might want your changes to go there so you are free to also open a PR directed at them.

The latest ArduPilot PX4Firmware builds perfectly since we are using it…

The latest ArduPilot PX4Firmware builds perfectly since we are using it…

Yes, it does. I was mistakenly trying to build with the PX4/Firmware master. Sorry for the misleading info… :wink:

1 Like

Hi again,
So to be nce, should i create 2 separate PRs? One for Ardupilot’s current state and another to PX4 to let this feature be alive, even after having PX4 changes in Ardupilots fork.

Or you will take care of this when merging upstream changes?
What is the preferred for you, Ardupilot devs?

It is up to you. We should take care of it when we merge upstream changes, but the less patches we have to put on top, the best. So, if you can make a PR for upstream that’s great, but it’s not mandatory.

I actually thought the restriction on 2, 4, 6 as valid values was because of underlying hardware restrictions. Anyway, I guess you’ll find out when you test the code.

Yeah, I think there are hardware restrictions in Pixhawk, in Pixracer I have no idea.

I was thinking about those limitations, but cant check by reading HW specs. That’s yet out of my capabilities :slight_smile:

Here is an initial ‘solution’ for testing, patching Ardupilot/PX4Firmware:

Beside of this, I’ve just added one line to the AP_BoardConfig/px4_drivers.cpp, mode table:
{ 5, PWM_SERVO_MODE_5PWM, 1 },

Now, to test this properly I will need to disassemble my racer again, that will take a bit of time.

By the way, what kind of hardware restriction do you mean?
Are those pins on the same timer, or having interference between them, or they can only be set to input / output by pairs?
I haven’t change ioctl, assuming it will handle this well.

I’m not really sure what the hardware restrictions are exactly. I know that we can enable the pwm in/out with a lot more flexibility on the linux boards. I’m pretty sure that difference is just because the underlying hardware allows it.

As for a status update, this is a compiled master (few days old) with the changes mentioned:

This never flied, so please DO NOT flash and fly blindly.

Test results:
5 is accepted as BRD_PWM_COUNT, 5th output works as before (camera tilt for me).
I really dont know what to set for:

  • sonar type (analog or PX4 pwm)
  • pin number <- this one is something i cant just try. i like to use the 6th pin as a pwm input.

Do you guys know what should i set there? (basically i dont know how to use a PWM sonar on pixracer)

I’ve found this so far:
To use the PWM input, have to find the pin to use.
In from line 236:

/* PWM input driver. Use FMU AUX5 pins attached to timer4 channel 2 */

Does this mean i cannot change PWM input pins?
If so, my intention is to use 5 output, therefore 5th pin is not available.
Is this a dead end?
Pixracer is soo cool, I thought everyone have at least one, already maxed out its features. :slight_smile:

Ps: maybe can i use on the RSSI analog input?

Status update: Analog/pwm sonar usage on PixRacer is canceled, due to conflicts mentioned.
Although couldnt manage to use pwm sonar with 5 pwm, im going to create a PR for accepting 5 as pwm count for pixracer. This may be useful for someone.

Beside of givin up on pwm/analog, i managed to convert my sonar to an I2C sonar. This is how its done:

Sonar I have, is a HC-SR04 bundled with an ATMega328 board and a 12bit I2C DAC, to provide analog signal (turned out this is not pwm at all).

First of all, I desoldered the 6pin DAC to let me use I2C without it. Instead of the DAC, I soldered a small wire, connecting to the PixRacer I2C splitter.
Flashed the sonar module with a custom firmware measuring with NewPing library, then sends it over I2C when requested by the FMU.
The code is derived from what can be found here:
This has a mode filter for sonar data. (I would like to create a small tutorial, github code, etc… but for now please see this gdrive link: )

Initial testing shows great results, although you need to know that HC-SR04 is

  • probably the cheapest and worst performing sonar of all.
  • its range is 0-200cm
  • you wont get any reading when the floor is not hard enough to reflect sound OR you have about more than 10deg tilt.

Aaaaand thank you guys for the help provided so far.

Thanks for your effort on this in any case!

Hi,I’m using a rangefinder which needs two 40kHz PWM to drive the sensor, and then the range information was output in a form of analog voltage. So, what the PX4 needs to do is generate two PWM signal and use an AD to sample the rangefinder output signal. Now the question is how can I config the PX4 board ? Any help would be much appreciated !