1

This is on a Raspberry Pi Zero 2 W

The physical pin 3 and 5 (gpio2 and gpio3) are the input pins I am using and for whatever reason, they are at default 3V and detect a change when they are connected to ground... They need to detect a change when they are connected to 3V, and they need to be at default 0V and basically connected to ground...

This is the script below:

import RPi.GPIO as GPIO
import time

# Set up GPIO mode and pins
GPIO.setmode(GPIO.BOARD)
switch_pin_1 = 3  # Physical pin 3
switch_pin_2 = 5  # Physical pin 5

# Disable warnings about cleanup when no channels are set up
GPIO.setwarnings(False)

# Set up GPIO pins as inputs with pull-up resistors
GPIO.setup(switch_pin_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch_pin_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Function to execute when a switch state changes
def switch_callback(channel):
    state = GPIO.input(channel)
    print(f"Switch {channel} changed to {state}")

# Add event detection for both switches
GPIO.add_event_detect(switch_pin_1, GPIO.BOTH, callback=switch_callback, bouncetime=200)
GPIO.add_event_detect(switch_pin_2, GPIO.BOTH, callback=switch_callback, bouncetime=200)

try:
    print("Press Ctrl+C to exit")
    while True:
        time.sleep(1)  # Keep the program running

except KeyboardInterrupt:
    print("\nExiting program")
finally:
    # Clean up GPIO settings
    GPIO.cleanup()
3
  • Just out of curiosity, how did you come to choose GPIO 2 & 3?
    – Seamus
    Commented Dec 5, 2023 at 21:17
  • @Seamus it just happened to be the starting pins and closest to 3.3V and GND, so I didn’t think that there might be an issue with pull up resistors initially… It was funny though because I kept on trying to change my script and nothing was working… Commented Dec 6, 2023 at 1:57
  • Ah! OK - I wondered if that was it :) Looks like you've got some good answers.
    – Seamus
    Commented Dec 6, 2023 at 2:06

2 Answers 2

3

You have made a bad choice of GPIO.

GPIO 2/3 are I2C SDA/SCL and both have hardwired pull-ups to 3V3 (so they operate properly as an I2C bus). If you must use those GPIO you will need to add stronger pulls to ground. The pulls high are 1k8 ohms so you will need lower valued resistors to pull to ground. I suggest you don't.

Use any other unused GPIO.

3
  • I see... Are there other GPIO pins that I have to worry about? I'm wanting to get some that are close to 3.3V and GND Commented Dec 5, 2023 at 20:20
  • GPIO 2/3 are the only ones with hard wired pulls.
    – joan
    Commented Dec 5, 2023 at 20:32
  • I appreciate the information :) Commented Dec 6, 2023 at 1:55
1

ALL GPIO input pins will be HIGH if >2.2V and LOW if <0.8V.

By default GPIO 0-8 have pullup, so will be HIGH by default. Others have pulldown, so will be LOW by default.

As joan has pointed out GPIO2,3 have 1.8kΩ pullup.

NOTE it is inadvisable to connect a pin to a foreign 3.3V source without some protection.

Striving to get either 0V or 3.3V is unnecessary; in fact it is safer to design circuitry to supply 2.3V.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.