Digital I/O: Components

A digital I/O pin is a sophisticated small circuit that allows many configurations. The inputs to this circuit come from the registers mentioned in the previous section. A GPIO pin on our MCU consists of the following components:


A few Multiplexers and logical gates such as AND and OR

These components are used to create a specific path for data depending on different configurations. In electronics, a multiplexer (or mux) is a device that selects one of several input signals and forwards the selected input into a single line. A multiplexer of 2n inputs has n select lines, which are used to select which input line to send to the output. The below figure shows a 2-to-1 multiplexer. The logic corresponding to the pictured MUX is Z = ((!S0) & A)  | (S0 & B). In other words, z is equal to A when S0 is 0 and B when S0 is 1. 

Tri-state buffer

In digital electronics, three-state, tri-state, or 3-state logic buffer allows an output port to assume a high impedance state in addition to the 0 and 1 logic levels, effectively removing the output from the circuit. As shown in figure, the tri-state buffer can be thought of as a switch. If B is 1, the switch is closed. If B is 0, the switch is open. In this figure, the enable signal is “active high.” This means when B is high (1), the switch is closed. A small circle at the input of the enable signal will represent an “active low” enable signal. For the figure, the Boolean representation of C can be written as: C = (B & A) | (!B & Z), where Z represents a high impedance state. 

This specific buffer is used to disconnect the output of gates from the I/O line when the pin is configured as an input. The basic idea behind this is that in hardware design it is not OK to tie the output of two gates as it may cause a short circuit (boom!). When a GPIO pin is configured as an input, we are assuming an outside entity is driving the input wire. If the output of the GPIO gates from inside the chip also drives the same wire, we are essentially connecting two outputs, which is not allowed. It is noteworthy to recall that unlike connecting the outputs, which is a big no-no, it is OK to connect as many inputs of gates as you like. Although it is OK to drive many inputs, we have to keep in mind that this slows down the circuit.

Schmitt trigger

This is used as basic Analog to Digital converter to prepare an input signal to be used in the digital world of the chip. Schmitt trigger uses hysteresis (dual threshold) to create its output. We use hysteresis as a protection against noise. If we used only a single threshold, then an input level that constantly fluctuates above and below the threshold (due to the noise) will also cause the output to fluctuate. Having two thresholds (one for a high-to-low transition and another for a low-to-high transition) keeps the output stable even if the input is not. 

Pull-up/down resistor

Some inputs have only one proper state (either 0 or 1). For the other state, instead of the opposite logic (either 1 or 0), they are floating, and their input voltage is undetermined. To help these inputs, GPIO includes a resistor. If the input needs help to become 1, it needs a pull-up resistor, which is a resistor between input and voltage source. If the input needs help to become 0, it needs a pull-down resistor, which is a resistor between input and ground. The GPIO has only one resistor that can be configured either as pull-up or pull-down. Some inputs do not need either of them. GPIO disconnects the resistor from the input line in those cases. Additionally, this resistor can prevent creating a direct connection from power to ground (thus, a short circuit, which can damage your board). 

(Image Source: https://www.elprocus.com/pull-up-and-pull-down-resistors-with-applications/

Pull-up resistor

Pull-down resistor

Combine all the components together, and the following circuit is formed. In the next section, we will discuss how these component help configure the pin.