Suketu Shah
7 min readOct 22, 2020

In this article, you will learn about the construction behind GPIO pins, its types, and their application and will also discuss about current sinking and sourcing in GPIO’s.

GPIO is a set of hardware that is used to connect microcontroller/ microprocessor to other components like LED’s, 7 Segment display, EEPROM, Temperature sensors, encoders, and many more.

Every microcontroller is shipped with a fixed set of GPIO pins and these pins are grouped in 8/16 depending on the manufacturer. Each group of these GPIO pins is termed as PORT. So if you observe in the STM32 microcontroller, it has 5 ports and each port has 16 GPIO pins. Modern microcontrollers have GPIO pins that support multifunctionality. It means that a single pin can be either used as an oscillator pin or UART pin, or for basic input-output. It depends on users to choose appropriate functionality for their GPIO pin.

These GPIO pins can be configured only as a digital, ie either digital output or digital input. For analog applications, users have to interface ADC/DAC. But modern microcontrollers have taken care of this and have inbuilt ADC/DAC thus reducing the overall hardware overhead.

What’s inside a GPIO?

A GPIO pin has two buffers mainly input and output which are controlled by the Enable pin. If Enable is 1 then GPIO is configured as input and if 0 then it is configured as an output. This can be better understood from the below diagram.

What’s inside an input and output buffer?

Each input/output buffer has a PMOS and NMOS transistor and a NOT gate. These two transistors are connected in such a way that their Gate and Drain pins are shorted as shown below.

Okay now let’s understand how these buffers work

Input buffer

If there is a logic high input at the pin, then the PMOS transistor turns ON and NMOS turns OFF, leading to connect Vcc to the microcontroller.

Similarly, if there is logic low input then NMOS turns ON and PMOS turns OFF leading to connect microcontroller directly to the ground.

By default, all GPIO pins are configured in input mode. So the user has to set some registers to enable it as output.

Also, some manufacturers keep these GPIO pins in floating or high impedance state by neither connecting it to Vcc nor ground. The user has to connect it with Vcc or ground otherwise, it would lead to high power consumption as of leakage current.

What happens if the pin is in floating or Hi-Z state?

Due to circuit noise, there could be some voltage on the pin which could potentially turn ON both the transistors and can provide a direct path from Vcc to Gnd to produce leakage current.

Output buffer

The output buffer is as opposite of the input one. If a logic high is given to the pin then the PMOS transistor turns ON and NMOS turns OFF leading to connect Vcc to the output pin. This phase is called as a push phase resembling as it is pushing current.

For logic low output, NMOS turns ON and PMOS turns OFF leading to connect the output pin to ground and as it looks like it is pulling current it is called as pull phase.

Modes of an output buffer

1. Push-pull

Push-pull is the same configuration as of normal output buffer.

It supports both current sinking and sourcing.

FYI, current flowing into ground of buffer is called current sinking and conversely, current generating from the buffer is called current sourcing. Current sourcing happens always through PNP or PMOS transistors and sinking through NPN or NMOS. Sourcing current is much larger than the sinking current as a result, microcontrollers pin can drive more than 10 GPIO pins.

2. Open-drain

In this configuration, there is no PMOS transistor and the drain of the NMOS transistor is kept in a floating state. As a result, this configuration is named as open drain.

When we apply logic LOW at buffer, NMOS will turn ON and will connect the pin to the ground. But by applying a HIGH signal to buffer, NMOS doesn’t turn ON which leads the pin to be in floating state.

So in this configuration, we can either apply logic low at the output pin or can keep the pin in floating state. In other words, this mode supports only current sinking. To avoid this floating state, we can connect a pull-up resistor to the drain of NMOS.

Now when we apply a HIGH signal to buffer, NMOS remains OFF but due to the pull-up resistor pin gets connected to Vcc.

When to use push-pull and open drain?

Push-pull is used where there is no need for multibus communication. Protocols like UART, SPI can be performed using push-pull as it uses different pins for transmission and reception. Conversely, open-drain can be used for a pin performing bidirectional communication like I2C protocol.

Modes of an input buffer

1. Floating/ Hi -z state

As I told you earlier a pin is said to be in a floating state if it is neither connected to Vcc nor ground. Users should never keep the pin in a floating state as it will lead to high power consumption and in the worst case, it might reset the controller. To avoid this condition, user can either use a pull-up or pull-down resistor. I have shown below how to add pull up and pull down resistor to a floating pin.

In input buffer with pull-up configuration, if the switch is kept open then Vcc gets connected to buffer and turns ON PMOS transistor to source current to microcontroller. So if the switch is open then the microcontroller reads it as logic 1 using pull up.

If the switch is pressed, it will provide a direct path to current to flow from Vcc to GND via a pull-up resistor. This will turn ON NMOS transistor to sink current from microcontroller. Thus microcontrollers reads logic 0 signal when switch is pressed.

Similarly for input buffer with pull-down configuration, everything works in opposite direction. When a switch is kept open, the microcontroller will read it as logic 0 and when the switch is pressed, the controller will read it as logic HIGH.

2. Schmitt triggered input

This type of input is different from the normal input buffer as we discussed. It just has a Schmitt trigger rather than the PMOS and NMOS transistors and a NOT gate. As per application, users must add pull up or pull down resistors in it. This type of input could be found in STM microcontrollers.

But what is schmitt trigger?

A Schmitt trigger is a voltage comparator that turns ON only when the input voltage gets pass an upper threshold and once running it will turn OFF only when the input voltage goes below the lower threshold. This property exhibited by the Schmitt trigger is called hysteresis.

Below I have shown a comparison between a normal input buffer and a Schmitt trigger w.r.t a noisy signal.

As you might have observed, the output of the normal input buffer keeps fluctuating in accordance with noise. But if you observe the output of the Schmitt trigger, it has suppressed a lot of noise and gives a steady output. So a Schmitt triggered input is useful in noisy environments.

I hope now you must have understood the concept behind GPIO’s. Write down in the comment box if you have faced difficulties while interfacing modules with microcontroller.