General purpose input/output (GPIO) pins on microcontrollers have various modes for both input and output. Input modes may include pull-up or pull-down resistors, hysteresis, or some combination. Output modes can be push-pull, high-drive or open-drain.
GPIO input modes typically include high impedance, pull-up, pull-down and repeater. Most input circuitry feature hysteresis which prevents spurious state changes.
Floating, High Impedance, Tri-Stated
When an input pin is in high impedance input mode, its state is indeterminate unless it is driven high or low by an external source. Pins configured as high impedance inputs and not currently driven are called “floating”. Many times “float” or “floating” is used interchaneably with “high impedance” or “tri-stated”. To prevent pins from floating when they are not externally driven, internally pulling resistors are used.
Impedance is very similar to resistance but considers how a circuit resists when a changing voltage is applied. A resistor only has resistance. But inductors and capacitors have impedance. A 1MOhm resistor resists a constant 5V in the same way it does a 1MHz 5V sin-wave. But a capacitor behaves totally different when a constant 5V is applied versus a 1MHz 5V sin-wave. A capacitor with a constant 5V applied will charge up and then act as an open circuit (high impedance). But at high frequency, the capacitor will conduct current and have a voltage drop (low impedance).
If an input is configured with an internal pull-up, it will be high unless it is externally driven low. The converse is true with pull-down inputs (learn more about pulling resistors). Some GPIO pins also support changing the pull-up and pull-down settings dynamically using repeater mode.
When a GPIO is configured in repeater mode, the pull-up is enabled when the pin is driven high, and the pull-down is enabled when the pin is driven low. If nothing is driving the pin, the pin will retain its last known state. In some designs, this can prevent power from leaking on pins in an indeterminate state.
Virtually all GPIO inputs use a principle called hysteresis to prevent spurious changes in state when an input value changes. “Hysteresis is the dependence of a system on not only its current environment but also on its past” (read more). Hysteresis is implemented on GPIO by setting two thresholds for a digital input which are VIH and VIL typically enumerated in the “Electrical Characteristics” section of microcontroller datasheets. VIH is the input-high voltage and represents the minimum voltage that must be applied to drive the pin high. VIL is the maximum input-low voltage needed to drive the pin low. This means if the input voltage is greater than VIL and less than VIH, the value at the pin depends on whether the past input value was above VIH, in which case the pin is high, or below VIL, which means the pin is low. The following graph illustrates the difference between no hysteresis (A) and hysteresis (B) for a given signal (U).
A push-pull output has the ability to both source and sink current. TTL and CMOS logic circuits use push-pull outputs. A CMOS push-pull topology is illustrated in the figure below. The “C” in CMOS stands for complementary because M2 and M3 are complementary devices. M2 is a PMOS device while M3 is an NMOS device. Because M2 and M3 have the same input signal, M3 is on whenever M2 is off and vice versa. An open-drain output is also shown in the figure at the top of the page.
From the figure above, the namesake of “open-drain” is obvious to anyone who is familiar with MOSFETs. A MOSFET has three terminals called: gate, source, and drain. In an open-drain configuration, the source is grounded, the gate is driven internally, and the drain is open (i.e. not connected to anything).
An open-drain output can only sink current. Its two states are low and high-impedance. When M1 is on, the output is set to 0V. When M1 is off, the output is not driven high nor low. Most applications, including I2C buses, use an external pull-up resistor on any open-drain outputs. Another useful application for open-drain outputs is having multiple external devices drive a single, active-low interrupt pin on a microcontroller.
Open drain outputs are great for having multiple sources drive the same input. For example, several open-drain outputs that signal an interrupt condition can drive a single input on a microcontroller. Any of the circuits can drive the line low while not conflicting with other circuits that are in high-impedance mode.
Many ASIC designers will create open-drain outputs to signal an interrupt condition. The microcontroller can then poll each device to see which on is driving the line to zero.
High drive pins are push-pull pins that are capable of higher current.
A typical push-pull output is able to source/sink around +/-8ma where a high drive output can source up to +/-40ma. The electrical characteristics specified in the datasheet enumerate the current capacity of each pin. Understanding the current capabilities of pins is important to reducing circuit board costs and complexity. If a microcontroller is capable of directly driving a motor or LED, there is no need to add external circuitry (such as a MOSFET).
GPIO is the simplest IO available on microcontrollers. However, it is important to understand the intricacies of GPIO such as the input and output modes. Understanding the capabilites of the GPIO can save cost and complexity in an embedded design while boosting flexiblity.