← Writing

GPIO (General Purpose Input / Output)

2025-06-12 rust · embedded · basic · gpio


🟠 What is a general purpose input output (GPIO) of the MCU


GPIO

GPIO stands for general purpose input/output. It is a type of pin found on an integrated circuit that does not have a specific function.
While most pins have a dedicated purpose, such as sending a signal to a certain component, the function of a GPIO pin is customizable and can be controlled by the software.

It can work as: 📥 Input (reads logical signals, such as from a button)
📤 Output (controls something - LED, relay, motor)
🔄 Sometimes - both as an input and as an output (push-pull)

Pin

Pin Mode : Each port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in several modes:

GPIO Design

Each GPIO pin of the microcontroller consists of three key components:

  1. Enable Line
  1. Output Buffer

When the signal 0 comes out, control goes through the NMOS transistor and no logical one is supplied to the pin.
When the signal 1 comes out, control goes through the PMOS transistor and a logical one is supplied to the pin.

  1. Input Buffer

When a 0 signal is received on the pin, control passes through the NMOS transistor and goes to GND
When a 1 signal is received on the pin, control passes through the PMOS transistor and is powered from +VCC

⚙️ GPIO Modes

ModeDescriptionExample
InputReading signal from outsideButton, sensor
Input Pull-UpInput with pull-up to Vcc (internal resistor)Button to GND
Input Pull-DownInput with pull-up to GNDButton to Vcc
Output Push-PullNormal output (0 or 1)LED, relay
Output Open-DrainOnly “0”, “1” — via external resistorI²C, external bus
AnalogADC/DAC is used on the outputTemperature sensor, audio
Alternate FunctionUsed by peripherals (SPI, PWM, UART)SPI, I²C, PWM

If we wanted to use some Pin as an input, we would use either .into_pull_up_input(), .into_pull_down_input() to tell the chip to expect the system voltage (3.3 volts) to be present or absent, and react if that changes.

// RP2040 (Rust embedded-hal, hi-Z)
    _pin_28.into_pull_down_input(); // Set GPIO 28 as input with pull-down
    _pin_28.into_floating_input(); // Set GPIO 28 as floating input
    _pin_28.into_pull_up_input(); // Set GPIO 28 as input with pull-up

    _pin_28.into_pull_up_disabled(); // Disable pull-up on GPIO 28
    _pin_28.into_pull_down_disabled(); // Disable pull-down on GPIO 28

    _pin_28.into_push_pull_output(); // Set GPIO 28 as push-pull output

Output

Push-Pull

Open-Drain

Useful when When need a common bus with many devices connected to it. (All participants can only “pull down” to avoid conflict)