I want to find at runtime if a GPIO is floating (nothing connected to it).

I assumed I'd find tons of answers on Google, but maybe I'm searching wrong.

My approach has been to:

  • enable the internal pullup resistor and read the IO
  • disable the pullup, enable the pulldown and read the IO again

If it reads high first and low second, the input is floating.

Since I've not seen a lot f discussion on this online, I'm wondering if I'm doing something unusual, or if there's a better way, or if this approach doesn't work in the general case and I simply lucked out in my application.

If this does work in the general case, I'm curious what happens with GPIOs that don't exist in a package. Presumably, if a particular IO line doesn't come out to a pin, there are no pullup or pulldown resistors for this line either. I'm curious particularly about what STM32 does in this case.

  • 3
    \$\begingroup\$ Your approach makes good sense, and should work. I'd say the reason that you're not seeing anything online is that it's a pretty rare need. The typical reason to check for a float is to look for fault conditions, and most stuff online is not designed for that level of robustness. As for the I/Os that don't come out to a pin, I don't know the specifics of the STM32, but I'd wager that the pull-ups are part of the silicon itself, so they'll still be there even if the I/O isn't bonded out to a physical pin. \$\endgroup\$
    – Mr. Snrub
    Oct 17, 2019 at 2:23
  • \$\begingroup\$ This makes sense about a rare need. My original use case was for reading a three-position switch with one IO line instead of two. Right now, I'm trying to come up with a way of telling different generations of a board apart, and using this "floating" logic gives me 3^n instead of 2^n combinations out of the same number of pins. There are other ways, of course, like different voltage divider ratios on a single ADC pin. How long would you say the code should wait to let pullup/pulldown settle before reading the input register? \$\endgroup\$
    – iter
    Oct 17, 2019 at 4:10
  • 1
    \$\begingroup\$ I would say just wait for 3 times the RC time constant. R will be a pessimistic (high) estimate of the pull-up / pull down resistor's resistance, and C will be the pin capacitance, which should be in the datasheet, plus another few pF to account for the board. Probably will end up being really fast. \$\endgroup\$
    – Mr. Snrub
    Oct 17, 2019 at 4:58
  • 1
    \$\begingroup\$ The highest possible R value needed to toggle state can be selected and bus impedance measured by RC response curve can compute the tri-state R and Vref. right? Using a fixed C and ADC sample rate to match resolution needed. I could not find any STM32 3s bus analog tests but lots of digital self-tests \$\endgroup\$ Oct 17, 2019 at 5:07
  • 1
    \$\begingroup\$ The LTC6804 BMS IC uses a very similar approach, but using an ADC instead of digital pins. Therefore, the idea can't be too far off :) \$\endgroup\$
    – Erlkoenig
    Oct 17, 2019 at 15:16


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.