# Pixhawk - Enabling GPIO output

**URL:** <https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457>\
**Category:** PX4 Autopilot\
**Created:** [February 14, 2017, 11:14am UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457 "2017-02-14T11:14:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ealdaz](https://discuss.px4.io/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@ealdaz](https://discuss.px4.io/u/ealdaz)\
**Post date:** [February 14, 2017, 11:14am UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/1 "2017-02-14T11:14:59Z")

</div>

Dear All

I have searched the forum and elsewhere but haven’t found a clear answer on how to use a PWM pin ( or other?) as a digital output ( would be curious about enabling input too, but not needed at this time)

Would anyone be able to point me to some documentation or send me an example code that does this?.

Is this something that can be done via the configuration files in init.d or is it something that needs to be hardcoded and compiled?

Thanks !  
Eduardo

---

<div class="post-metadata">

**Author:** ![Mark\_Whitehorn](https://discuss.px4.io/user_avatar/discuss.px4.io/mark_whitehorn/32/3533_2.png) [@Mark\_Whitehorn](https://discuss.px4.io/u/Mark_Whitehorn)\
**Post date:** [February 14, 2017, 2:35pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/2 "2017-02-14T14:35:12Z")

</div>

I don’t think there are any user configurable GPIO pins. Look at px4fmu\_led.c to see how to write code which uses outputs pins, and any board\_config.h (in src/drivers/boards) to see how the GPIOs are configured.

---

<div class="post-metadata">

**Author:** ![ealdaz](https://discuss.px4.io/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@ealdaz](https://discuss.px4.io/u/ealdaz)\
**Post date:** [February 14, 2017, 3:56pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/3 "2017-02-14T15:56:50Z")

</div>

Thanks Mark  
If I wanted to use an AUX OUT pin, would it be enough to not enable PMW\_AUX in the init.d/ startup script and then develop an application ( I mean such as [https://dev.px4.io/tutorial-hello-sky.html](https://dev.px4.io/tutorial-hello-sky.html)) that subscribes to whatever channel is meant to drive the digital output?

Or are PWM\_AUX pins always controlled by the main PX4 application, i.e. not available for other use?

Is any of this Pixhawk/Ardupilot info applicable in the PX4? ( Scroll to the very bottom of the page)  
[http://ardupilot.org/copter/docs/common-pixhawk-overview.html](http://ardupilot.org/copter/docs/common-pixhawk-overview.html)

Thanks

---

<div class="post-metadata">

**Author:** ![Mark\_Whitehorn](https://discuss.px4.io/user_avatar/discuss.px4.io/mark_whitehorn/32/3533_2.png) [@Mark\_Whitehorn](https://discuss.px4.io/u/Mark_Whitehorn)\
**Post date:** [February 15, 2017, 4:45pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/4 "2017-02-15T16:45:36Z")

</div>

look in fmu.cpp to see how the AUX pwm output pins are handled. You can probably get away with disabling all AUX pwm outputs, but note that there are other things like camera trigger which can be set up to use those pins also.

---

<div class="post-metadata">

**Author:** ![ealdaz](https://discuss.px4.io/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@ealdaz](https://discuss.px4.io/u/ealdaz)\
**Post date:** [February 15, 2017, 5:47pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/5 "2017-02-15T17:47:12Z")

</div>

Would I disable the AUX pwm outputs by not calling  
set PWM\_AUX in the init script?

Or would I have to modify fmu.cpp so that it calls up\_pwm\_servo\_deinit() ?  
/\* make sure servos are off \*/  
up\_pwm\_servo\_deinit();

thanks

---

<div class="post-metadata">

**Author:** ![Mark\_Whitehorn](https://discuss.px4.io/user_avatar/discuss.px4.io/mark_whitehorn/32/3533_2.png) [@Mark\_Whitehorn](https://discuss.px4.io/u/Mark_Whitehorn)\
**Post date:** [February 16, 2017, 5:04pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/6 "2017-02-16T17:04:35Z")

</div>

You can probably just edit rcS to start fmu.cpp with no (or less than 6) PWM outputs enabled. Look at the mode\_pwm options. That’s assuming you’re using fmu-v2, v3 or v4

---

<div class="post-metadata">

**Author:** ![Tachsche](https://discuss.px4.io/letter_avatar_proxy/v4/letter/t/9de0a6/32.png) [@Tachsche](https://discuss.px4.io/u/Tachsche)\
**Post date:** [March 30, 2017, 2:01am UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/7 "2017-03-30T02:01:12Z")

</div>

Hello together,

I´m also struggeling with this AUX Pin enabling for digital Input&Output on the Pixhawk.  
I currently try to enable it with stm32\_gpio…

But I did not find which Port and which Pin they belong to. According to the pixhawk guide (see also link above), those are virtual Pins 50 to 55.

I would be very happy to get even a hint how to manage this.  
I work with Matlab SFunction (= c-code)

PS: I used stm32\_… comands to set all ports to output and 0 and another time to one. But couldn’t measure any difference at the output pins…

Thanks ahead =)

---

<div class="post-metadata">

**Author:** ![ealdaz](https://discuss.px4.io/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@ealdaz](https://discuss.px4.io/u/ealdaz)\
**Post date:** [March 30, 2017, 10:03am UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/8 "2017-03-30T10:03:59Z")

</div>

@dagar helped me get this working, it turns out it’s quite simple

in the rCS script insert these two lines  
`set FMU_MODE pwm4 set AUX_MODE pwm4`  
after the camera part, i.e. in line 472

> <https://github.com/PX4/Firmware/blob/master/ROMFS/px4fmu_common/init.d/rcS#L471>

These calls reserve the first 4 pins in FMU ( AUX PWM) to be used as pwm and the two remaining to be used as GPIO

And then if you wanted to toggle AUX6 you would first call:  
`px4_arch_configgpio(GPIO_GPIO5_OUTPUT)`

to configure the pin as Output (or GPIO\_GPIO5\_INPUT to configure as input.)

The GPIO0-GPIO5 correspond to AUX1-AUX6.  
[https://github.com/PX4/Firmware/blob/master/src/drivers/boards/px4fmu-v2/board\_config.h#L178](https://github.com/PX4/Firmware/blob/master/src/drivers/boards/px4fmu-v2/board_config.h#L178)

You would place this code in the constructor of a class you’re using (for example) as you only want to configure once.

And then call this where needed in your code  
`px4_arch_gpiowrite(GPIO_GPIO5_OUTPUT,value);`  
or for an input  
`px4_arch_gpioread(GPIO_GPIO5_INPUT);`

Hope this helps

---

<div class="post-metadata">

**Author:** ![Tachsche](https://discuss.px4.io/letter_avatar_proxy/v4/letter/t/9de0a6/32.png) [@Tachsche](https://discuss.px4.io/u/Tachsche)\
**Post date:** [March 31, 2017, 8:13pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/9 "2017-03-31T20:13:45Z")

</div>

Hi and thanks for the answer.

But I did not make it yet to enable it.  
I Think I also have a problem with including the right headers.

```
#include <math.h>
# ifndef MATLAB_MEX_FILE
	#include <stdio.h>
	#include <fcntl.h>
	#include <sys/types.h>
	#include <sys/stat.h>
	#include "stm32_gpio.h"
	#include "drivers/drv_gpio.h"
	//#include "drivers/boards/px4fmu-v2/board_config.h"
# endif

```

I could not manage to include the last boar\_config file, due to some error while building my function.  
I tried to use the definition from drv\_gpio.h  
`# define GPIO_SERVO_6 (1<<5) /**< servo 6 output */`  
but I think it is probably the wrong one.

I also inserted the set FMU and Set AUX\_Mode, but it did not work.  
(It also threw me an error. I think pmw4 was not know. - and I did not have a camera part in this referred code??..)  
But regarding this, as far as I understand the webpage, I should be able to use the aux pins 5 &6 anyway due to default virtual pin setup.

Also the commands px\_arch\_gpio don’t exist for me - why? Is there a way to determine which board I exactly have oir what am I missing? I used:

```
    stm32_gpioinit();
    stm32_unconfiggpio(GPIO_SERVO_6);
    stm32_configgpio(GPIO_SERVO_6);
    stm32_gpiowrite(GPIO_SERVO_6,1);

```

Well… thanks ahead again and I wrote so late because I tried … but yeah. Did not work

---

<div class="post-metadata">

**Author:** ![ealdaz](https://discuss.px4.io/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@ealdaz](https://discuss.px4.io/u/ealdaz)\
**Post date:** [April 3, 2017, 6:19pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/10 "2017-04-03T18:19:38Z")

</div>

That sounds odd… just to be sure, what board are you using?

---

<div class="post-metadata">

**Author:** ![Tachsche](https://discuss.px4.io/letter_avatar_proxy/v4/letter/t/9de0a6/32.png) [@Tachsche](https://discuss.px4.io/u/Tachsche)\
**Post date:** [April 3, 2017, 11:45pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/11 "2017-04-03T23:45:43Z")

</div>

![](https://discuss.px4.io/uploads/default/original/1X/1dcde8785713dfb56d3ca0e00eec4189a0370498.jpg)

This one…

---

<div class="post-metadata">

**Author:** ![ealdaz](https://discuss.px4.io/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@ealdaz](https://discuss.px4.io/u/ealdaz)\
**Post date:** [April 5, 2017, 4:30pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/12 "2017-04-05T16:30:24Z")

</div>

That is the same board I’m using, the Pixhawk.

I didn’t have to include any header files, the default ones worked fine.

Strange also that  
set FMU\_MODE pwm4  
would throw an error and that you can’t find the camera section

Are you using a recent version of source code ?  
Are you building with make make px4fmu-v2\_default ?

---

<div class="post-metadata">

**Author:** ![ealdaz](https://discuss.px4.io/letter_avatar_proxy/v4/letter/e/9dc877/32.png) [@ealdaz](https://discuss.px4.io/u/ealdaz)\
**Post date:** [April 5, 2017, 4:30pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/13 "2017-04-05T16:30:53Z")

</div>

Sorry typo, should be  
`make px4fmu-v2_default`

---

<div class="post-metadata">

**Author:** ![MaEtUgR](https://discuss.px4.io/user_avatar/discuss.px4.io/maetugr/32/2707_2.png) [@MaEtUgR](https://discuss.px4.io/u/MaEtUgR)\
**Post date:** [February 22, 2021, 10:58am UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/14 "2021-02-22T10:58:54Z")

</div>

Just for reference in case someone comes across this thread there’s now parameter configurable GPIO usage e.g. for camera triggering: [https://docs.px4.io/master/en/peripherals/camera.html#trigger-hardware-configuration-hardware-setup](https://docs.px4.io/master/en/peripherals/camera.html#trigger-hardware-configuration-hardware-setup)

---

<div class="post-metadata">

**Author:** ![Murray\_Louw](https://discuss.px4.io/user_avatar/discuss.px4.io/murray_louw/32/7850_2.png) [@Murray\_Louw](https://discuss.px4.io/u/Murray_Louw)\
**Post date:** [February 23, 2021, 7:11am UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/15 "2021-02-23T07:11:44Z")

</div>

For anybody coming across this thread years later like me and need to enable GPIO output, this is what worked for me:

Add these lines to the appropriate board\_config.h file (e.g. boards/px4/fmu-v5/board\_config.h) and modify them for the pins you want to use.

```
#define MAIN_OUT_6 /* PD14 */ (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|GPIO_OUTPUT_SET|GPIO_PORTD|GPIO_PIN14)
#define MAIN_OUT_7 /* PH6 */ (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|GPIO_OUTPUT_SET|GPIO_PORTH|GPIO_PIN6)

```

This defines the output modes of the GPIO pins you are interested in.  
These lines specifically enable GPIO push-pull output on the MAIN\_OUT pins 6 and 7 of the Pixhawk 4 mini.  
According to: [Pixhawk4Mini\_pinout](https://docs.google.com/spreadsheets/d/1-n0__BYDedQrc_2NHqBenG1DNepAgnHpSGglke-QQwY/edit#gid=912976165), these pins correspond to FMU\_CH6 and FMU\_CH7,  
which corresponds to “Port D, Pin 14” and “Port H, Pin 6” according to: [FMUv5\_stm32\_pinout](https://github.com/PX4/px4_user_guide/raw/master/assets/flight_controller/pixhawk4mini/pixhawk4mini_pinouts.pdf)

Now, in your own app like px4\_simple\_app.c from [Hello Sky Tutorial](https://docs.px4.io/master/en/modules/hello_sky.html), add the following code to configure the pins first:

```
px4_arch_configgpio(MAIN_OUT_6);
px4_arch_configgpio(MAIN_OUT_7);

```

Use this line to make the pin HIGH (3.3V):

```
px4_arch_gpiowrite(MAIN_OUT_6, 1);

```

Use this line to make the pin LOW (0V):

```
px4_arch_gpiowrite(MAIN_OUT_6, 0);
```

---

<div class="post-metadata">

**Author:** ![iamczar](https://discuss.px4.io/user_avatar/discuss.px4.io/iamczar/32/7205_2.png) [@iamczar](https://discuss.px4.io/u/iamczar)\
**Post date:** [April 28, 2021, 8:07am UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/16 "2021-04-28T08:07:17Z")

</div>

@Murray_Louw Im actually trying your method now for Pixhawk 4. I think I am configuring it right but not able to control the gpios… hmmm. Im trying to make ti blink

On the board\_config.h

 ![image](https://discuss.px4.io/uploads/default/original/2X/d/da3217d2027bfe0a0196b302a8f9b707e730bccc.png)

And on my App

 ![image](https://discuss.px4.io/uploads/default/original/2X/f/fd1c17edb3b6215221306d62b7db43675ffb8785.png)

EDIT…  
This was actualy correct I was plugging my LED IO PWM OUT instead of the FMU PWM OUT… My Bad

---

<div class="post-metadata">

**Author:** ![chethana](https://discuss.px4.io/user_avatar/discuss.px4.io/chethana/32/9501_2.png) [@chethana](https://discuss.px4.io/u/chethana)\
**Post date:** [March 16, 2022, 11:53am UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/17 "2022-03-16T11:53:39Z")

</div>

Do you have the excel sheet of pinout for pixhawk4 holybro ?

---

<div class="post-metadata">

**Author:** ![PavaniVitor](https://discuss.px4.io/user_avatar/discuss.px4.io/pavanivitor/32/12005_2.png) [@PavaniVitor](https://discuss.px4.io/u/PavaniVitor)\
**Post date:** [March 12, 2023, 10:15pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/18 "2023-03-12T22:15:38Z")

</div>

For anyone trying to reproduce this. The correct GPIO pin and port can be found inside  
`PX4-Autopilot/boards/../../src/timer_config.cpp`

e.g. Using the auxiliary pins of the durandal-v1 flight controller:  
from `PX4-Autopilot/boards/holybro/durandal-v1/src/timer_config.cpp `

```auto
constexpr timer_io_channels_t timer_io_channels[MAX_TIMER_IO_CHANNELS] = {
	initIOTimerChannel(io_timers, {Timer::Timer1, Timer::Channel4}, {GPIO::PortE, GPIO::Pin14}),
	initIOTimerChannel(io_timers, {Timer::Timer1, Timer::Channel3}, {GPIO::PortA, GPIO::Pin10}),
	initIOTimerChannel(io_timers, {Timer::Timer1, Timer::Channel2}, {GPIO::PortE, GPIO::Pin11}),
	initIOTimerChannel(io_timers, {Timer::Timer1, Timer::Channel1}, {GPIO::PortE, GPIO::Pin9}),
	initIOTimerChannel(io_timers, {Timer::Timer4, Timer::Channel2}, {GPIO::PortD, GPIO::Pin13}),

```

now define the desired pins inside board\_config.h

```auto
#define AUX_OUT_1 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_100MHz|GPIO_OUTPUT_SET|GPIO_PORTE|GPIO_PIN14)
#define AUX_OUT_2 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_100MHz|GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN10)

```

---

<div class="post-metadata">

**Author:** ![Nezha\_Zahri](https://discuss.px4.io/user_avatar/discuss.px4.io/nezha_zahri/32/11926_2.png) [@Nezha\_Zahri](https://discuss.px4.io/u/Nezha_Zahri)\
**Post date:** [July 14, 2023, 8:57pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/19 "2023-07-14T20:57:46Z")

</div>

Hello,

I’ve been able to control all FMU GPIO pins successfully, but I was wondering if its possible to control the GPIO pins for the IO processor (px4io). I’m using V6C hardware and I’m getting an error when I’m trying to use these pins.

Here’s what I’ve done:

I’ve added this definitions in boards/px4/io-v2/src/board\_config.h to make it more readable:

```auto
#define IO_CH1 GPIO_PWM1
#define IO_CH2 GPIO_PWM2
#define IO_CH3 GPIO_PWM3
#define IO_CH4 GPIO_PWM4
#define IO_CH5 GPIO_PWM5
#define IO_CH6 GPIO_PWM6
#define IO_CH7 GPIO_PWM7
#define IO_CH8 GPIO_PWM8

```

I tried to include board\_config for the io processor in the iridium module:

```auto
#include <boards/px4/io-v2/src/board_config.h>

```

This is where I’m getting the error when I’m trying to build:

 ![image](https://discuss.px4.io/uploads/default/original/2X/5/59a1843adb1ebb8712504ec859f65bc5fea7fb1a.png)

I’ve added the configgpio in main:

```auto
	px4_arch_configgpio(FMU_CH1);
	px4_arch_configgpio(FMU_CH2);
	px4_arch_configgpio(IO_CH1);

```

Then since I’m playing with the iridium driver I added a toggle like feature in one of the function:

```auto
        px4_arch_gpiowrite(FMU_CH1, 1);
	px4_usleep(2000000);
	px4_arch_gpiowrite(FMU_CH1, 0);

	px4_arch_gpiowrite(FMU_CH2, 1);
	px4_usleep(1000000);
	px4_arch_gpiowrite(FMU_CH2, 0);

	px4_arch_gpiowrite(IO_CH1, 1);
	px4_usleep(10000);
	px4_arch_gpiowrite(IO_CH1, 0);

```

You can see my code for FMU\_CH1 and CH2 that was working quite well:

![image](https://discuss.px4.io/uploads/default/original/2X/7/749d54006b995aebf9341aec1fca2387225b0b3a.png)

---

<div class="post-metadata">

**Author:** ![Igor\_Misic](https://discuss.px4.io/user_avatar/discuss.px4.io/igor_misic/32/13536_2.png) [@Igor\_Misic](https://discuss.px4.io/u/Igor_Misic)\
**Post date:** [July 19, 2023, 3:39pm UTC](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457/20 "2023-07-19T15:39:37Z")

</div>

This is firmware that runs at IO MCU:

> <https://github.com/PX4/PX4-Autopilot/tree/main/src/modules/px4iofirmware>
>
> //github.com/PX4/PX4-Autopilot/tree/main/src/modules/px4iofirmware

And this is how FMU controls it:

> <https://github.com/PX4/PX4-Autopilot/tree/main/src/drivers/px4io>
>
> //github.com/PX4/PX4-Autopilot/tree/main/src/drivers/px4io

> **[Igor Misic is PX4 Autopilot Contributor](https://www.buymeacoffee.com/igormisic)**
>
> If you found this helpful you can thank him by buying him a coffee.

[Next page](https://discuss.px4.io/t/pixhawk-enabling-gpio-output/2457.md?page=2)
