This article is a part of Arduino / ATmega328p Embedded C Firmware Programming Tutorial. Consider exploring the course home page for articles on similar topics.
Also visit the Release Page for Register Level Embedded C Hardware Abstraction Library and Code for AVR.
GPIO (General Purpose Input/Output) is a software controlled interface that is usually found on Microcontrollers and some Microprocessor ICs or interface chipset. Typically, the GPIO is a one or more pins on the IC which have no special purpose in of themselves, but which facilitate an optional ability for device designers to create an interface/connection between the IC and a peripheral component by programming some hardware registers. Thereby, within limitations, GPIO pins can be tailored for use to provide some specific function or purpose within a hardware device design.
System-on-Chip (SOC) processors, Microcontrollers, DSP etc heavily rely on GPIOs. In some cases, every non-dedicated pin can be configured as a GPIO; and most chips have at least several dozens of them. Programmable logic devices (like FPGAs) can easily provide GPIOs; multifunction chips like power managers and audio codecs often have a few such pins to help with pin scarcity on SOCs, and there are also “GPIO Expander” chips that connect using the I2C or SPI serial buses. GPIO Pins are also seen in ICs like PMICs, LED Controllers etc.
Some basic GPIO capabilities are :
- GPIO Pins can be enabled or disabled as needed
- Output values are writable (high=1, low=0)
- Input values are readable (high=1, low=0)
- Inputs can often be used as IRQ (Interrupt Request) signals, Edge triggered or Level triggered. Such IRQs may be configurable to wake the system from a low power state
GPIO Input Output Modes
- High Impedance / Floating / Hi-Z / Tri-Stated
- Input Mode with Pull-Up or Pull-Down
- Output Mode with Open Drain
- Output Mode with Push Pull
High Impedance / Floating / Hi-Z / Tri-Stated
When a GPIO pin is put into High Impedance state it effectively gets disconnected from the external interface. The basic concept of the third state, high impedance (Hi-Z), is to effectively remove the device’s influence from the rest of the circuit. If more than one device is electrically connected to another device, putting a pin into the Hi-Z state is often used to prevent short circuits, or one device driving high (logical 1) against another device driving low (logical 0).
A tristate buffer can be thought of as a switch. If B is on, the switch is closed. If B is off, the switch is open.
It is unsafe to keep pins floating as it will lead to leakage current and in turn cause high power consumption and electrical noise problem.
Input Mode with Pull-Up or Pull-Down
When a GPIO pin is used with an Internal or Extrenal Pull -Up or Pull-Down Resistor to ensure a known state of logic. It is used tipicaly with an switch or input device such that whenever the switch is pressed or input comes it toggles the logic level.
Pull-Up ensures a well-defined Logic Level of High = 1. A Pull-Up resistor usually connects the pin to Vcc. It holds the logic to High when no other active input comes.
Pull-Down ensures a well-defined Logic Level of Low = 1. A Pull-Down resistor usually connects the pin to GND. It holds the logic to Low when no other active input comes.
When the switch is open the voltage of the gate input is pulled up to the level of Vin. When the switch is closed, the input voltage at the gate goes to ground.
When the switch is open, the voltage across a pull-up resistor with sufficiently low impedance vanishes to the effect that it looks like a wire to VCC. On the other hand, when the switch is closed, the pull-up resistor must have
Many microcontrollers have internal programmable Pull-Up or Down resistors that can be enabled or disabled based on the need or application. This works as an advantage as it removes the cost of external resistors and complexity.
Unused pins are recommended to be kept in either Pull-Up or Pull-Down state as this will hold the Logic Level of the pin and stop any lekage current and voltage fluctuations.
Output Mode with Open Drain
An Open Drain output can take two states Low Impedance and High Impedance. In Low Impedance state, it can sink current. It is like a switch that is either connected to ground or open.
In this MOSFET it 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 the pin (i.e. not connected to external hardware). When the Gate is driven, Drain gets connected with the Source and the PIN gets a Logic Low = 0. Otherwise, the PIN remains at High-Z state which is useless until a Pull-Up is used.
In order to achieve a logical high output on the line, a pull-up resistor is used to connect the open-drain output to the desired output voltage level.
I2C Bus uses SDA and SCL Pins in Open Drain Configuration thus it need Pull Up resistors to achieve a logic High. The value of resistance depends on Bus Length, Capacitance, Clock Frequency etc. Usually it is between 4.7K Ohm to 10K Ohm.
Output Mode with Push Pull
A Push Pull output is capable of both sourcing and sinking current.
When the top transistor [PMOS] will be activated, the output PIN will be HIGH and act as a Source. When the NMOS below will be on, the output PIN is driven LOW and act as a Sink. It is a complementary logic and only once MOS will be activated at a time.