GPIO handling

Why "General Purpose"?

The general purpose inputs and outputs of microcontrollers do not have a predefined function. It is up to the user to decide what functionality a GPIO should have in a particular program, whether it is a digital output or input, an analog output or input, or a connector for a communication protocol.

Configuration

In each case it must specified how a used GPIO is to be handled by the program, this setting is usually not changed at runtime. The way it is specified and the possible options depend on the IDE and the MCU used.

Pull-up and Pull-down

A digital input is used to detect the high and low levels of signals. The state of such an input is unpredictable, random noise will result in a random behaviour. This phenomenon is called floating pin. For reliable readings, a pull-up or pull-down resistor is required, the difference being the direction of the signal level change when the input becomes active. Pull-up is 1 by default, 0 when active, pull-down is the opposite. Microcontrollers usually include an internal high-value pull-up resistor which can be activated in firmware.

Interrupt

When using microcontrollers, the value of an input can be checked cyclically, just as someone would check their email client continuously to see if a new message has arrived. This is polling, which is not an efficient method. It is much more efficient to receive a notification when a new email has arrived. Then the user can immediately see its contents, with polling it is possible that the user, who checks their messages every five minutes did not open a new email for minutes after it arrived. Similarly an interrupt is responsible for telling the program immediately when an input changes, which was able to do anything else before. An Interrupt Service Routine (ISR) can also be configured on a timer.