During the daily device driver development routine, we often need to understand the behavior of the GPIO pins to understand if there is I2C traffic or if our driver is handling the coming interrupts and so on.
Sometimes is not enough to mount the debugfs interfaces and read out the values or cat/grep the /proc/interrupts file because we may loose the instant when the GPIO changes its status or perhaps we need to keep different terminals open.
Having the possibility to immediately visualize the GPIO status using an LED could be of great help.
The LED is a diod that when stimulated with a current emits light.
If we try we would be disappointed by noticing that the LED emits a weak light, such that we may need to be in complete darkness to understand whether the GPIO is high or low. In any case, depending on the internal resistence of the LED, we risk to short circuit the GPIO pin affecting his behavior.
For having a more secure and efficient circuit, we should keep the GPIO pin as far as possible from the ground and use it just as enabler for switching on and off the PIN. For doing this we need to use a transistor that is controlled by the pin we want to debug.
The circuit above shows how to enable a transistor with the GPIO pin keeping it away from the ground. The 3.3V power source represents the GPIO voltage and is connected to the base of an npn transistor through a 2.2kΩ resistor. When GPIO is high the transistor is active and connects the LED pin to ground so that it emits light. While when 0 the transistor behaves as an open circuit so that the pin of the LED is not connected anywhere and the LED is disabled.
The R2 resistor is needed to disconnect the GPIO from all the parassitic capcitance of the transistor that may affect the frequency on the pin itself. Anyway the transistor needs low voltage to be enabled.
The R1 resistor is optional, needed to not overcharge the LED. Remember that higher is the value of the resistor and lower will be the LED’s light.
For more detail: Debug GPIO with LED