0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use Pull Up and Pull Down on Raspberry Pi GPIO Pins

On Raspberry Pi GPIO pins, you can enable internal pull-up or pull-down resistors by specifying the pull_up_down parameter when setting up a pin with the GPIO.setup() function in Python. Use GPIO.PUD_UP for pull-up and GPIO.PUD_DOWN for pull-down to ensure the pin reads a stable high or low state when not connected.
📐

Syntax

To set a GPIO pin with a pull-up or pull-down resistor in Python using the RPi.GPIO library, use the GPIO.setup() function with the pull_up_down argument.

  • GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP): Sets the pin as input with an internal pull-up resistor.
  • GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_DOWN): Sets the pin as input with an internal pull-down resistor.
  • GPIO.PUD_OFF disables internal pull resistors.
python
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)  # Use BCM pin numbering
pin = 17

# Setup pin 17 as input with pull-up resistor
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
💻

Example

This example shows how to read a button connected to GPIO pin 17 using a pull-up resistor. The button connects the pin to ground when pressed, so the input reads False when pressed and True when released.

python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
pin = 17

# Setup pin with pull-up resistor
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
        if GPIO.input(pin) == False:  # Button pressed
            print("Button Pressed")
        else:
            print("Button Released")
        time.sleep(0.5)
except KeyboardInterrupt:
    GPIO.cleanup()
Output
Button Released Button Released Button Pressed Button Released ... (repeats every 0.5 seconds depending on button state)
⚠️

Common Pitfalls

  • Not specifying pull_up_down can leave the pin floating, causing unreliable readings.
  • Using the wrong resistor type for your circuit can invert logic (e.g., using pull-up when expecting pull-down behavior).
  • Forcing external resistors and internal pull resistors simultaneously can cause damage or unexpected behavior.
  • Remember to call GPIO.cleanup() to reset pins after your program ends.
python
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
pin = 17

# Wrong: No pull resistor specified, pin may float
GPIO.setup(pin, GPIO.IN)

# Right: Specify pull-up or pull-down
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
📊

Quick Reference

ParameterDescription
GPIO.PUD_UPEnable internal pull-up resistor (pin reads HIGH when unconnected)
GPIO.PUD_DOWNEnable internal pull-down resistor (pin reads LOW when unconnected)
GPIO.PUD_OFFDisable internal pull resistors (pin may float)
GPIO.setup(pin, GPIO.IN, pull_up_down=...)Set pin as input with specified pull resistor

Key Takeaways

Always specify pull-up or pull-down resistors on input pins to avoid floating states.
Use GPIO.PUD_UP for pull-up and GPIO.PUD_DOWN for pull-down when calling GPIO.setup().
Do not mix internal pull resistors with external resistors on the same pin.
Call GPIO.cleanup() at the end of your program to reset GPIO pins safely.
Pull-up means the pin reads HIGH when not connected; pull-down means it reads LOW.