← Back to list

How to set STM32 pin high be default?

To make an STM32 pin HIGH by default, you have 3 common options. The “right” one depends on whether you mean right after reset/power-up…

Ampheo · 2026-01-07 08:48 · 0 claps · 1.6 min read
#stm32 #gpio #pull-up-resistor #mosfet #series-resistor
Open on Medium ↗

How to set STM32 pin high be default?

To make an STM32 pin HIGH by default, you have 3 common options. The “right” one depends on whether you mean right after reset/power-up (before firmware runs) or as soon as your code starts.

1) Hardware default (HIGH immediately at power-up/reset)

STM32 GPIOs typically come up as Hi-Z inputs after reset, so you cannot guarantee HIGH purely in software until your code executes.

To guarantee HIGH from the first moment:

  • Add an external pull-up resistor (typ. 4.7k–100k) from the pin to VDD.
  • Keep the pin as input/Hi-Z during reset; the resistor forces it HIGH.
  • If it drives something sensitive (EN pin, MOSFET gate, etc.), add a series resistor (like 100–1k) for safety and to limit ringing.

Best for: enable pins, chip selects, “must be safe during boot”.

2) CubeMX / HAL method (HIGH as soon as GPIO is initialized)

This is the most common “default high when my firmware starts”.

Key idea: set the output level before switching the pin to output mode, so you avoid a low glitch.

HAL code (recommended sequence)

// Example: PA5 default HIGH
__HAL_RCC_GPIOA_CLK_ENABLE();

// 1) Preload output level HIGH (writes to BSRR/ODR)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
// 2) Configure pin as output
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;          // or GPIO_PULLUP if needed (still not "guaranteed" during reset)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

CubeMX setting

In CubeMX, set the pin to GPIO_Output, and set GPIO initial output level = High (it generates similar preload behavior in MX_GPIO_Init()).

Best for: “default high after init”, avoiding glitches.

3) Internal pull-up (HIGH when configured as input)

If you only need it HIGH while it’s an input, you can enable internal pull-up:

  • GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  • GPIO_InitStruct.Pull = GPIO_PULLUP;

Not a true power-up default: it only applies after you configure the pin (and pull-ups are weak, variable, and not ideal for gates/enable pins).

Which should you choose?

  • Need HIGH during reset/bootloader time?External pull-up resistor (Option 1).
  • Need HIGH as soon as firmware starts with no glitch?Preload HIGH then init output (Option 2).
  • Just want it HIGH as an input?Internal pull-up (Option 3).

메타데이터
post_id
a75a0aced6cf
slug
how-to-set-stm32-pin-high-be-default-a75a0aced6cf
url
https://medium.com/@pqshedy33/how-to-set-stm32-pin-high-be-default-a75a0aced6cf
canonical_url
https://medium.com/@pqshedy33/how-to-set-stm32-pin-high-be-default-a75a0aced6cf
author_url
https://medium.com/@pqshedy33
status
ok
fetched_at
2026-06-21 12:17:11