4.1. Working#
Fig. 4.1 GPIO Junction on μC Side vs the Physical Pin#
As the name suggests, a GPIO can broadly be configured either as an Input or as an Output. Keep in mind that the terms ‘Input’ and ‘Output’ here represent the direction of information transfer instead of the direction of the current flow. To make understanding different configurations easier, consider figure to the right.
In the figure, think of the □ as the physical pin where a wire from some external circuit may be connected. While, the ● represents a junction on the μC side. The μC may make different connections at this junction based on the configuration.
4.1.1. GPIO Output#
Fig. 4.2 GPIO Config: Output Low#
Fig. 4.3 GPIO Config: Output High#
In this configuration, a GPIO is either driven LOW or driven HIGH, i.e. it is connected to 0V/GND or 3.3V on the μC side. Figures on the right shows the connections the μC makes to the junction. The Output configuration is also known as a Push-Pull configuration in technical terms. There other output configurations called Open-Drain and Open-Collector which are not discussed here and will not be used throughout this course.
Note that if a GPIO is configured as an output Low and 3.3V is connected to the physical pin then a short circuit is created. Similarly, a short circuit is also created when a GPIO is configured as an output High and 0V/GND is connected to the physical pin. Both of these situations are highly discouraged since the high current flow due to the short circuit may damage the μC.
Attention
Make sure you are not creating a short circuit when a GPIO is configured as an output.
4.1.2. GPIO Input#
In this configuration of the GPIO, the μC measures the voltage at the junction with reference to the GND. To be more specific, a GPIO can have three different input configurations.
Fig. 4.4 GPIO Config: Input Floating#
4.1.2.1. Floating#
In this configuration, the μC measures voltage at the junction without connecting anything else to the junction as shown in the figure to the right. Thus, if the voltage on the physical pin is 3.3V then the μC reads it as 1
or High. Or, if the voltage on the physical pin is 0V/GND then the μC reads it as 0
or Low.
The μC is extremely sensitive to the voltage changes. Thus, if nothing is connected to the physical pin, i.e. the pin is in floating state, then touching the physical pin with your finger can change the voltage reading. Other two configurations exist to tackle this exact problem.
Fig. 4.5 GPIO Config: Input Pull-Down#
4.1.2.2. Pull-Down#
In this configuration, 0V/GND is connected to the junction through a huge resistor (~65kΩ), known as a pull-down resistor, before the μC measures the voltage at the junction, as shown in the figure. Same as the floating input, if the voltage on the physical pin is 3.3V or 0V/GND then the μC reads it as 1
or 0
respectively. However, if the physical pin is floating, then the μC would read 0
. This is because the junction is connected to GND through the resistor.
Note
Pull-Down and Pull-Up input methods work fine only if there is no resistor before the physical pin larger than the pull-down/pull-up resistors. Think about the voltage drop in the case of two series resistors.
Fig. 4.6 GPIO Config: Input Pull-Up#
4.1.2.3. Pull-Up#
Similar to Pull-Down configuration, 3.3V is connected to the junction, instead of 0V, through a huge resistor (~65kΩ), known as a pull-up resistor, before the μC measures the voltage at the junction. Once again, similar to the floating input, if the voltage on the physical pin is 3.3V or 0V/GND then the μC reads it as 1
or 0
respectively. However, if the physical pin is floating, then the μC would read 1
since the junction is connected to 3.3V through the resistor.