EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: Watth on January 03, 2025, 05:49:08 pm

Title: Switch to Microcontroller digital input: default ground or Vref?
Post by: Watth on January 03, 2025, 05:49:08 pm
Hey everyone !
When designing a circuit where a tactile switch is connected to a microcontroller GPIO, is it preferable to have the signal compared
- to ground (with pulldown resist and the switch activation detected as HIGH) or
- to Vref (with pullup resistor and activation of the switch read as LOW)?
Is this choice in consideration to the µC or PCB design, or other?
Thanks for reading!
Title: Re: Switch to Microcontroller digital input: default ground or Vref?
Post by: PGPG on January 03, 2025, 07:04:52 pm
I had to position such switch under one of holes destined for terminal blocks in DIN-rail case.
I connected its one pin (it was 2 pin tact-switch) with GND and rotated it such that this pin was first from the PCB border assuming that if someone will be doing something near terminal blocks and ESD could happen to be it to that GND connected pad and not the opposite one.
Title: Re: Switch to Microcontroller digital input: default ground or Vref?
Post by: SteveThackery on January 03, 2025, 07:10:25 pm
Most of the circuits I've come across have a pull-up resistor on the pin, and the switch - when operated - connects the pin to ground. So it's an active low input.

I've been told it is considered bad to have Vcc connected directly to the switch, which is how it would be if the switch operated as a pull-up. Something to do with reducing the amount of wiring and exposed connections with Vcc on them, to reduce the chance of accidental shorts.
Title: Re: Switch to Microcontroller digital input: default ground or Vref?
Post by: golden_labels on January 03, 2025, 07:50:14 pm
I don’t know, but consider it may be “inherited” from other situations:
Title: Re: Switch to Microcontroller digital input: default ground or Vref?
Post by: prints-f-darkness on January 03, 2025, 08:17:05 pm
Hi,

In the microcontroller world, VREF is typically used internally for the ADC, DAC or OPAMP peripheral (it serves as a known reference voltage).
The GPIO only sees anything above like .7v or 1.2v (threshold will very) depending on the IC, so it would make the most since to use VCC not VREF.
Some MCU's actually have an internal pull up or pull down on the GPIO.

What I use is a simple circuit found on hackaday (https://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/ (https://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/))
(https://hackaday.com/wp-content/uploads/2015/11/debounce-sch.png?resize=400,395)

I really like this circuit for 3 reasons, its got hardware debounce (less code), its simple, and there is a series resistor on the switch, so any HV transients will be reduced before hitting the GPIO.

(Some MCU's can have ESD protection built into the IC's GPIO)
See page 1 https://www.st.com/resource/en/application_note/an5612-esd-protection-of-stm32-mcus-and-mpus-stmicroelectronics.pdf (https://www.st.com/resource/en/application_note/an5612-esd-protection-of-stm32-mcus-and-mpus-stmicroelectronics.pdf)
Title: Re: Switch to Microcontroller digital input: default ground or Vref?
Post by: DavidAlfa on January 03, 2025, 09:14:24 pm
Generally it's more common to pull to ground, most mcus can enable weak pullups, this way the only required part is the button itself.