Portenta H7 GPIO goes high even when not connected

Hello, I am using the portenta gpio D4 and D5 for external input to trigger switching. I have them connected to the 3.3v output of the portenta using a switch. When the switch is in OFF position the portenta still considers it a HIGH signal and goes on with the trigger switching. Unless I explicitly connect the pins to ground it stays high regardless of if the switch is in ON or OFF state. I even disconnected the switch and the 3.3v connection and it's still HIGH. How do I make the default state of the pin LOW? Should I inverse my logic and connect the switch to the ground instead?

Any help regarding this would be appreciated.

When OFF is the GPIO connected to anything? If not then is will "float" and could have any value. That is why you use pull-up or pulldown resistors... so it always has a known value.

If you define as pinMode(pin, INPUT_PULLUP); and connect to GND, it will always have a value.

The GPIO is not connected to anything when the switch is OFF, If I connect the GPIO pin directly to GND and turn the switch ON won't the GND and the 3.3v be shorted? Should I use a resistor? What should the value of the resistor be?

Doesn't the portenta have internal pullup/pulldown resistors?

Just connect the switch to GND and the pin.

Then use INPUT_PULLUP. This will connect in internally via a resistor.

When the switch in on it will be LOW when off it will be HIGH

Hi, thank you so much. It works!