What problems can a floating (unconnected) GPIO input cause, and how do you prevent them?
A floating input has no defined voltage — it hovers near the switching threshold of the CMOS input buffer, typically around VDD/2. The consequences are more severe than many engineers realize. The PMOS and NMOS transistors in the Schmitt-trigger input buffer both partially conduct, creating a direct current path from VDD to GND. This shoot-through current can reach several microamps per pin. Across 20-30 unused pins on a microcontroller, the cumulative leakage can add hundreds of microamps to the system's quiescent current — a significant penalty in battery-powered designs where the sleep current budget might be only a few microamps total.
Beyond power waste, a floating pin reads as random values. If any software path reads that pin, the behavior is unpredictable. If the pin is configured as an EXTI interrupt source, noise coupling generates spurious interrupts that waste CPU cycles and can cause subtle timing bugs in the application. In extreme cases, a floating pin connected to a peripheral mux (like an alternate function input) can inject garbage data into a communication peripheral.
Prevention strategies, in order of preference: (1) configure unused pins as analog mode — this disconnects the digital input buffer entirely, eliminating shoot-through current. STM32 reference manuals explicitly recommend this for unused pins; (2) configure as output driven low (but be careful about inadvertent connections on the PCB); (3) enable internal pull-up or pull-down resistors to clamp the pin to a defined level. Analog mode is the lowest-power option and should be the default in your board initialization code for every pin not explicitly used by the application.
Source: GPIO Q&A
