Raspberry Pi GPIO Basics
The General-Purpose Input/Output (GPIO) pins on a Raspberry Pi are one of its most powerful features. They allow the Raspberry Pi to interact with various electronic components, sensors, motors, and other hardware. Understanding how these pins work is crucial for developing innovative Raspberry Pi projects.
What is GPIO?
The GPIO is a set of pins found on Raspberry Pi boards that can be controlled programmatically. These pins act as electronic switches, capable of sending and receiving electrical signals, allowing the Raspberry Pi to interface with external devices.
Each GPIO pin can be programmed to act as either:
- Input (to read signals from sensors or buttons)
- Output (to control devices like LEDs, motors, and relays)
Raspberry Pi GPIO Pinout Diagram
Modern Raspberry Pi boards, such as the Raspberry Pi 4, feature 40 GPIO pins arranged in two rows of 20 pins. Each pin has a specific function, which can be categorized as follows:
- Power Pins: Provide 3.3V and 5V output for powering devices.
- Ground (GND) Pins: Essential for completing circuits.
- GPIO Pins: Programmable I/O pins for interfacing with various devices.
- Special Function Pins: Support communication protocols like I2C, SPI, and UART.
Pinout Reference for Raspberry Pi 4 (40-Pin Header)
3.3V (1) (2) 5V
GPIO2 (3) (4) 5V
GPIO3 (5) (6) GND
GPIO4 (7) (8) GPIO14
GND (9) (10) GPIO15
GPIO17 (11) (12) GPIO18
GPIO27 (13) (14) GND
GPIO22 (15) (16) GPIO23
3.3V (17) (18) GPIO24
GPIO10 (19) (20) GND
GPIO9 (21) (22) GPIO25
GPIO11 (23) (24) GPIO8
GND (25) (26) GPIO7
GPIO0 (27) (28) GPIO1
GPIO5 (29) (30) GND
GPIO6 (31) (32) GPIO12
GPIO13 (33) (34) GND
GPIO19 (35) (36) GPIO16
GPIO26 (37) (38) GPIO20
GND (39) (40) GPIO21
Key GPIO Pin Definitions
- Power Pins: Supply 3.3V and 5V power to connected devices.
- Ground Pins (GND): Required for completing electrical circuits.
- GPIO Pins: Programmable digital input/output pins.
- I2C Pins: Used for I2C communication.
- SPI Pins: Used for SPI communication with sensors and peripherals.
- UART Pins: For serial communication.
GPIO Pin Numbering Systems
Raspberry Pi supports two numbering systems for GPIO:
- Broadcom (BCM) Numbering: Refers to the internal GPIO numbers assigned by the chip manufacturer. (Preferred for coding)
- Physical Pin Numbering: Refers to the pin’s physical position on the Raspberry Pi’s header.
How to Control GPIO Pins with Python
Python’s RPi.GPIO library is widely used for GPIO programming on the Raspberry Pi. Below is a simple guide to control an LED using GPIO pins.
Step 1: Install RPi.GPIO Library
sudo apt update
sudo apt install python3-rpi.gpio
Step 2: Connect LED to GPIO
- Connect the positive leg (anode) of the LED to GPIO18 (pin 12).
- Connect the negative leg (cathode) to a 220-ohm resistor, and then to GND.
Step 3: Write Python Code
import RPi.GPIO as GPIO
import time
# Set GPIO mode to BCM
GPIO.setmode(GPIO.BCM)
# Configure GPIO pin 18 as output
LED_PIN = 18
GPIO.setup(LED_PIN, GPIO.OUT)
# Blink LED
try:
while True:
GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED ON
time.sleep(1)
GPIO.output(LED_PIN, GPIO.LOW) # Turn LED OFF
time.sleep(1)
except KeyboardInterrupt:
print("Program stopped")
GPIO.cleanup() # Release GPIO resources
Step 4: Run the Code
python3 led_blink.py
Using GPIO as an Input (e.g., Reading a Button Press)
For input devices like buttons, we configure GPIO as an input pin with a pull-up or pull-down resistor.
Example Code to Read Button Input
import RPi.GPIO as GPIO
import time
BUTTON_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while True:
if GPIO.input(BUTTON_PIN) == GPIO.LOW:
print("Button Pressed")
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()
Important GPIO Safety Tips
- Always use current-limiting resistors with LEDs to prevent burning them out.
- Avoid connecting devices that exceed the 3.3V logic level.
- Use appropriate transistors, relays, or optocouplers for high-power devices.
- Always clean up GPIO settings after program execution using
GPIO.cleanup()
.
Practical Applications of GPIO Pins
- Home Automation: Control lights, fans, or appliances.
- IoT Projects: Interact with sensors for temperature, humidity, etc.
- Robotics: Control motors, servos, and actuators.
- Security Systems: Build motion sensors, door alarms, and surveillance setups.
Conclusion
Understanding the GPIO pins on the Raspberry Pi unlocks endless possibilities for creative projects. Whether you’re controlling LEDs, reading sensor data, or building complex home automation systems, mastering GPIO programming is crucial for Raspberry Pi enthusiasts. By following best practices and safety tips, you can efficiently develop and expand your hardware-based projects with ease.
You may like also:
Pingback: Raspberry Pi GPIO Basics — IoTbyHVM – Explore TechBytes – hashstacks
Pingback: Setup Docker on Raspberry Pi - IoTbyHVM - Bits & Bytes of IoT
Pingback: Raspberry Pi Alternatives in 2019 - IoTbyHVM - Bits & Bytes of IoT
Pingback: How to Run Ubuntu 18.04 or 18.10 on Raspberry Pi
Pingback: Setup Docker on Rock Pi 4 - IoTbyHVM - Bits & Bytes of IoT
Pingback: Raspberry Pi 4 with faster CPU, dual-4K display support launched
Pingback: Install sgminer-arm on Rock Pi 4 - IoTbyHVM - Bits & Bytes of IoT
Pingback: Raspberry Pi Introduction and Tutorials — OnionLinux