Push-Pull vs Open-Drain Outputs Explained for Firmware Developers
Learn how push-pull and open-drain GPIO outputs differ at the transistor level, when each is right (LEDs, I2C, shared lines), and how to set both on an STM32.

A GPIO pin set to output can behave in two electrically different ways, and the choice is a bit in a register that new firmware developers often leave at its default without knowing it matters. In push-pull mode the pin drives both high and low. In open-drain mode it drives only low, and a resistor outside the chip pulls it high. Pick the wrong one and an I2C bus will not work, two outputs that share a wire will fight each other, or a pin that should drive a logic input at 5 V will be stuck at 3.3 V. This post explains both modes from the transistors up, shows when each is right, and gives the register settings on an STM32 and on an ATmega328P. It needs no background beyond knowing what a pin voltage is.
The output stage
Inside the microcontroller, an output pin connects to two transistors. One connects the pin to the supply (VDD) and one connects it to ground (VSS). A control circuit turns them on and off according to the output register bit.
In push-pull mode both transistors are used:
| Output bit | Upper transistor (to VDD) | Lower transistor (to GND) | Pin |
|---|---|---|---|
| 1 | On | Off | Driven to VDD (about 3.3 V) |
| 0 | Off | On | Driven to GND (0 V) |
The name comes from the pair: one pushes current out of the pin, the other pulls current in. The pin is always driven to a defined level, strongly, in both states.
In open-drain mode the upper transistor is absent or disabled:
| Output bit | Lower transistor (to GND) | Pin |
|---|---|---|
| 0 | On | Driven to GND |
| 1 | Off | Floating (high impedance) |
When the bit is 1 the pin is not driven at all. It sits at whatever voltage the outside circuit gives it. Without anything else connected, that is undefined. With a pull-up resistor from the pin to a supply rail, the resistor pulls the line to that rail's voltage. The name "open drain" comes from the transistor's drain terminal, which is left open (not connected to anything inside the chip) in the high state. On a bipolar-style description the term is open-collector.
What the pull-up gives you
A pull-up resistor makes the open-drain pin a normal logic signal: driven low when the chip says 0, pulled high when the chip says 1. The details differ from push-pull in three ways that decide where to use it.
The high level voltage is chosen by the resistor's supply, not the chip's. An open-drain pin on a 3.3 V microcontroller with a pull-up to 5 V gives a 5 V high level, provided the pin is specified as 5 V tolerant. The STM32 datasheet marks pins as FT (five-volt tolerant) or not; a pin that is not FT must not see more than VDD plus a small margin. This is how a 3.3 V part drives a 5 V logic input without a level shifter, one of the main uses of open-drain.
The rising edge is slow, set by the resistor and the wire's capacitance. The pin drives low through a transistor (fast, strong) but rises through the resistor (an RC curve). A 4.7 kilohm pull-up on a line with 100 picofarads has a time constant of 0.47 microseconds, and the line takes a few time constants to reach a valid high. That limits the speed; it is why I2C at 400 kHz needs strong enough pull-ups and why open-drain is not used for fast signals.
Several drivers can share the wire. If two open-drain outputs connect to the same line with one pull-up, either can pull it low, and the line is high only if all release it. This is a wired AND: the line is 1 only when every driver outputs 1. Two push-pull outputs on the same wire are a disaster: if one drives high and the other low, the two transistors short VDD to GND through the wire, and current flows until something burns out or the pin protection trips. Open-drain makes sharing safe by construction, since no driver ever pushes the line high.
Where each is used
| Situation | Mode | Reason |
|---|---|---|
| Driving an LED through a resistor | Push-pull | Simple, drives current from the pin either way |
| Output to a logic input on the same board and voltage | Push-pull | Fast edges, no extra parts |
| I2C SDA and SCL | Open-drain with pull-up | The bus is shared; multiple devices and clock stretching need wired-AND |
| 1-Wire data line | Open-drain with pull-up | Master and slaves share one wire |
| Interrupt request lines shared by several chips | Open-drain | Any device can assert, none can fight |
| Level shift from 3.3 V to 5 V (limited speed) | Open-drain to a 5 V pull-up | Pin must be 5 V tolerant |
| Reset line with several sources | Open-drain | Any source can force reset |
| Driving a MOSFET gate, a motor driver input | Push-pull | Needs a driven high level |
I2C is the textbook case. Both lines are open-drain. A master or slave can pull either low; the bus is high when idle. A slave that needs time can hold SCL low (clock stretching), which a push-pull master could not allow without damage. That is also why an I2C bus without pull-up resistors does not work at all: nothing ever drives the lines high.
Configuring the modes
On an STM32 the register GPIOx_OTYPER holds one bit per pin: 0 is push-pull, 1 is open-drain. GPIOx_MODER must also set the pin to output (01) or alternate function (10). For PD12 as an open-drain output:
#include <stdint.h>
#include "stm32f407xx.h"
void pd12_open_drain(void)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;
(void)RCC->AHB1ENR;
GPIOD->OTYPER |= (1u << 12); /* 1 = open-drain */
GPIOD->MODER = (GPIOD->MODER & ~(3u << 24)) | (1u << 24); /* output */
GPIOD->PUPDR = (GPIOD->PUPDR & ~(3u << 24)) | (1u << 24); /* 01 = internal pull-up */
}The internal pull-up (PUPDR = 01) is a weak resistor in the tens of kilohms, listed in the datasheet. It is enough for a slow signal and not enough for I2C at speed, which uses external resistors of a few kilohms. After this, writing GPIOD->BSRR = (1u << 12) releases the line (high or floating-high through the pull-up) and GPIOD->BSRR = (1u << (12 + 16)) pulls it low.
The same bit in the HAL is GPIO_MODE_OUTPUT_OD against GPIO_MODE_OUTPUT_PP in the GPIO_InitTypeDef.Mode field.
On an ATmega328P there is no register to choose open-drain, and the AVR achieves it by switching the pin between output-low and input. The DDRx bit decides the direction and PORTx the level:
#include <avr/io.h>
static inline void line_release(void) { DDRD &= ~(1 << DDD2); PORTD &= ~(1 << PORTD2); }
static inline void line_low(void) { PORTD &= ~(1 << PORTD2); DDRD |= (1 << DDD2); }line_release makes the pin an input with the pull-up off (high impedance), line_low makes it an output at 0. With an external pull-up this behaves as open-drain. The order in line_low matters: clear the output latch first, then enable the output driver, otherwise a brief high pulse can appear when the pull-up is enabled by an earlier PORT write.
Reading back an open-drain pin
An open-drain pin can be read as well as driven, and reading it tells you the actual line state, not what you wrote. On an STM32 the input data register IDR reflects the pin voltage even in output mode. That is the basis of I2C arbitration (a master that drives 1 but reads 0 knows another master is pulling the line low and has won) and of checking a stuck bus:
int line_is_high(void)
{
return (GPIOD->IDR >> 12) & 1u;
}If you release the line and line_is_high() returns 0, something else is holding it low, or the pull-up is missing. The same read on a push-pull output returns what you drove (unless something forces it), so it tells you much less.
Mistakes that look like firmware bugs
No pull-up. The line never goes high, or goes high weakly with an internal resistor that is too weak. The fix is a resistor, not code.
Pull-up to the wrong rail. A pull-up to 5 V on a pin that is not 5 V tolerant can damage it over time; check the datasheet's FT markings.
Push-pull on a shared line. A bus with two push-pull outputs works until the day both drive different values. That day the symptom is a hot chip or a corrupted signal that appears only sometimes.
Expecting fast edges from open-drain. The rising time is set by the resistor and capacitance, so a 1 MHz signal on a 10 kilohm pull-up with a long wire will not reach a valid level.


