How to Control GPIO Pins on Raspberry Pi with Python
# How to Control GPIO Pins on Raspberry Pi with Python The Raspberry Pi GPIO (General Purpose Input/Output) pins let you control LEDs, relays, sensors, motors, and any other electronics directly from Python code. ## Pin Numbering The Pi has two numbering systems — always be clear which you're using: - **BCM (Broadcom)** — uses chip pin numbers (GPIO17, GPIO27 etc.) — recommended - **Board** — uses physical pin position numbers (pin 11, pin 13 etc.) Run `pinout` in the terminal for a visual GPIO map. ## Install gpiozero (Easiest Library) gpiozero is pre-installed on Raspberry Pi OS. If not: ```bash sudo apt install python3-gpiozero -y ``` ## Blink an LED Connect LED → 330Ω resistor → GPIO17 → GND. ```python from gpiozero import LED from time import sleep led = LED(17) # BCM pin 17 while True: led.on() sleep(1) led.off() sleep(1) ``` ## Read a Button Connect button between GPIO2 and GND (gpiozero enables internal pull-up). ```python from gpiozero import Button from signal import pause btn = Button(2) def on_press(): print("Button pressed!") btn.when_pressed = on_press pause() # Keep script running ``` ## Button Controls LED ```python from gpiozero import LED, Button from signal import pause led = LED(17) btn = Button(2) btn.when_pressed = led.on btn.when_released = led.off pause() ``` ## PWM (Dimming an LED) ```python from gpiozero import PWMLED from time import sleep led = PWMLED(17) # Fade in and out while True: for brightness in range(0, 101, 5): led.value = brightness / 100 sleep(0.05) for brightness in range(100, -1, -5): led.value = brightness / 100 sleep(0.05) ``` ## Control a Relay Relays are just LEDs electrically — same wiring logic, higher current switching. ```python from gpiozero import OutputDevice from time import sleep # Most relay modules are active LOW relay = OutputDevice(17, active_high=False) relay.on() # Activate relay sleep(2) relay.off() # Deactivate ``` ## Read a Sensor (DHT22 Temperature) ```bash pip3 install adafruit-circuitpython-dht sudo apt install libgpiod2 -y ``` ```python import adafruit_dht import board import time dht = adafruit_dht.DHT22(board.D4) # GPIO4 while True: try: temp = dht.temperature humidity = dht.humidity print(f"Temp: {temp:.1f}°C Humidity: {humidity:.1f}%") except RuntimeError: pass # DHT22 occasionally misreads — retry time.sleep(2) ``` ## Run Script at Boot ```bash sudo nano /etc/rc.local # Add before 'exit 0': python3 /home/pi/myscript.py & ``` Or use systemd for better control: ```bash sudo nano /etc/systemd/system/myscript.service ``` ```ini [Unit] Description=My GPIO Script After=network.target [Service] ExecStart=/usr/bin/python3 /home/pi/myscript.py Restart=always User=pi [Install] WantedBy=multi-user.target ``` ```bash sudo systemctl enable myscript sudo systemctl start myscript ``` ## Safety Rules - GPIO pins are 3.3V — never connect 5V directly - Max 16mA per pin, 50mA total for all GPIO - Always use a resistor with LEDs - Use a transistor or relay for motors and high-current loads - Never connect inductive loads (motors) directly — use flyback diodes🔧 Related tool: Pinout Reference
Related Guides
- Raspberry Pi GPIO: Complete Beginner Guide with Python Examples
- Controlling GPIO Outputs with Python — LED, Relay, and Buzzer
- Automated Plant Watering System with Raspberry Pi
- Flipper Zero GPIO: Reading Sensors and Controlling LEDs
- Getting Started with ROS2 on Raspberry Pi for Robotics
- How to Set Up OpenCV Machine Vision on a Raspberry Pi
- How to Run a Timelapse Camera with Raspberry Pi
- Reading Sensors over I2C with Raspberry Pi
Loading_