Of course! Working with I/O (Input/Output) on an Orange Pi using Python is a common task for hobbyists and developers. The process is very similar to working with a Raspberry Pi, as most Orange Pi boards run Linux and use the sysfs interface for GPIO control.

Here’s a comprehensive guide covering the essentials.
The Foundation: Physical Pinout
Before you can control anything, you must know which pins on your Orange Pi's GPIO header are which. The pinout varies significantly between different Orange Pi models (e.g., Orange Pi Zero, Orange Pi PC, Orange Pi 5).
Action: Find the correct pinout diagram for your specific Orange Pi model.
- Great Resource: OrangePi.io Pinout Database has a searchable database.
- Example: For an Orange Pi Zero, the GPIO 2-pin is physical pin 7, and GPIO 3-pin is physical pin 11.
The Two Main Methods for Python I/O
There are two primary ways to interact with GPIO pins in Python:

- Direct
sysfsMethod: Writing directly to files in the/sys/class/gpio/directory. It's built into the Linux kernel and requires no extra libraries. It's great for understanding the underlying process but can be verbose. - Python Library Method: Using a user-friendly library like
OPi.GPIO(the most common for Orange Pi) orgpiozero. This is the recommended approach for most projects as it's simpler, safer, and less error-prone.
Method 1: Direct sysfs (The "Hard Way" but Educational)
This method manually controls the GPIO by writing commands to special virtual files.
Core Steps:
- Export the Pin: Tell the kernel to take control of a specific GPIO pin.
- Set the Direction: Configure the pin as an input (
in) or output (out). - Read/Write:
- Read: Read the value from the
valuefile (0for LOW,1for HIGH). - Write: Write
0or1to thevaluefile.
- Read: Read the value from the
- Unexport the Pin: Release the pin when you're done.
Example: Blink an LED using sysfs
Let's blink an LED connected to GPIO 2 (Pin 7) on an Orange Pi Zero.
import time
import os
# --- Configuration ---
# Use the GPIO *number*, not the physical pin number.
# For Orange Pi Zero, GPIO 2 is on physical pin 7.
GPIO_PIN = 2
GPIO_DIR = "/sys/class/gpio"
GPIO_PIN_DIR = os.path.join(GPIO_DIR, f"gpio{GPIO_PIN}")
def setup_gpio():
"""Export the GPIO pin and set its direction."""
# Check if the pin is already exported
if not os.path.exists(GPIO_PIN_DIR):
print(f"Exporting GPIO {GPIO_PIN}...")
with open(os.path.join(GPIO_DIR, "export"), "w") as f:
f.write(str(GPIO_PIN))
# Wait for the directory to be created
while not os.path.exists(GPIO_PIN_DIR):
time.sleep(0.01)
# Set the direction to 'out' for output
print(f"Setting GPIO {GPIO_PIN} direction to 'out'")
with open(os.path.join(GPIO_PIN_DIR, "direction"), "w") as f:
f.write("out")
def cleanup_gpio():
"""Unexport the GPIO pin."""
print(f"Unexporting GPIO {GPIO_PIN}")
with open(os.path.join(GPIO_DIR, "unexport"), "w") as f:
f.write(str(GPIO_PIN))
# --- Main Program ---
try:
setup_gpio()
print("Blinking LED. Press Ctrl+C to stop.")
while True:
# Write '1' to turn the LED ON
with open(os.path.join(GPIO_PIN_DIR, "value"), "w") as f:
f.write("1")
time.sleep(0.5)
# Write '0' to turn the LED OFF
with open(os.path.join(GPIO_PIN_DIR, "value"), "w") as f:
f.write("0")
time.sleep(0.5)
except KeyboardInterrupt:
print("\nExiting program.")
finally:
cleanup_gpio()
Method 2: Using the OPi.GPIO Library (The "Easy Way")
This is the standard library for Orange Pi GPIO. It mimics the popular RPi.GPIO library from the Raspberry Pi world.
Step 1: Install the Library
You'll need to install it first. Open a terminal on your Orange Pi and run:

sudo apt-get update sudo apt-get install python3-opi # Or for Python 2: # sudo apt-get install python-opi
Step 2: Basic Syntax
import OPi.GPIO as GPIOGPIO.setmode(GPIO.BCM): Use the Broadcom GPIO numbers (e.g., 2, 3, 14...). This is highly recommended.GPIO.setup(pin, GPIO.OUT): Set a pin as an output.GPIO.setup(pin, GPIO.IN): Set a pin as an input.GPIO.output(pin, GPIO.HIGH): Set pin to HIGH (1).GPIO.output(pin, GPIO.LOW): Set pin to LOW (0).GPIO.input(pin): Read the state of a pin.GPIO.cleanup(): Reset all GPIO pins to their default state. Always call this at the end!
Example: Blink an LED using OPi.GPIO
This is much cleaner and more readable.
import OPi.GPIO as GPIO
import time
# --- Configuration ---
# Use the Broadcom GPIO number, e.g., 2 for Pin 7 on Orange Pi Zero
GPIO_PIN = 2
# --- Setup ---
# Set the GPIO mode to Broadcom (BCM) numbering
GPIO.setmode(GPIO.BCM)
# Set up the GPIO pin as an output
GPIO.setup(GPIO_PIN, GPIO.OUT)
try:
print("Blinking LED. Press Ctrl+C to stop.")
while True:
# Turn LED on
GPIO.output(GPIO_PIN, GPIO.HIGH)
print("LED ON")
time.sleep(0.5)
# Turn LED off
GPIO.output(GPIO_PIN, GPIO.LOW)
print("LED OFF")
time.sleep(0.5)
except KeyboardInterrupt:
print("\nExiting program.")
# --- Cleanup ---
# This ensures a clean exit
GPIO.cleanup()
Advanced I/O Examples
Example: Reading a Button (Input)
Connect a button between GPIO 3 (Pin 11) and GND. Use a pull-up resistor (or the internal one) to prevent floating.
import OPi.GPIO as GPIO
import time
# --- Configuration ---
BUTTON_PIN = 3 # BCM pin number for physical pin 11
# --- Setup ---
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Enable internal pull-up
try:
print("Press the button. Press Ctrl+C to exit.")
while True:
# Read the state of the pin
button_state = GPIO.input(BUTTON_PIN)
# When the button is pressed, it connects to GND, reading will be LOW (0)
if button_state == GPIO.LOW:
print("Button Pressed!")
# Wait for the button to be released to avoid repeated printing
while GPIO.input(BUTTON_PIN) == GPIO.LOW:
time.sleep(0.1)
time.sleep(0.1) # Small delay to reduce CPU usage
except KeyboardInterrupt:
print("\nExiting program.")
finally:
GPIO.cleanup()
Example: Controlling an RGB LED
An RGB LED has three pins (Red, Green, Blue). You control them individually.
import OPi.GPIO as GPIO
import time
# --- Configuration ---
# BCM pin numbers for your Orange Pi
RED_PIN = 2
GREEN_PIN = 3
BLUE_PIN = 14
# --- Setup ---
GPIO.setmode(GPIO.BCM)
GPIO.setup(RED_PIN, GPIO.OUT)
GPIO.setup(GREEN_PIN, GPIO.OUT)
GPIO.setup(BLUE_PIN, GPIO.OUT)
# Function to set a specific color
def set_color(r, g, b):
GPIO.output(RED_PIN, r)
GPIO.output(GREEN_PIN, g)
GPIO.output(BLUE_PIN, b)
try:
print("Showing RGB colors. Press Ctrl+C to exit.")
while True:
print("Red")
set_color(GPIO.HIGH, GPIO.LOW, GPIO.LOW)
time.sleep(1)
print("Green")
set_color(GPIO.LOW, GPIO.HIGH, GPIO.LOW)
time.sleep(1)
print("Blue")
set_color(GPIO.LOW, GPIO.LOW, GPIO.HIGH)
time.sleep(1)
except KeyboardInterrupt:
print("\nExiting program.")
finally:
set_color(GPIO.LOW, GPIO.LOW, GPIO.LOW) # Turn LED off
GPIO.cleanup()
Important Considerations & Best Practices
-
Permissions: Your Python script needs permission to access the GPIO hardware. The easiest way is to run it with
sudo:sudo python3 your_script.py
A better, more secure long-term solution is to add your user to the
gpiogroup:sudo usermod -a -G gpio $USER, then log out and log back in. -
Kernel Modules: On some Orange Pi OS images, you may need to enable the GPIO kernel module. You can usually do this with:
sudo modprobe gpio_sunxi
To make it permanent, add this line to a file in
/etc/modules-load.d/. -
Physical Connection:
- Always double-check your pinout! Connecting to the wrong pin can damage your board or peripheral.
- Use a current-limiting resistor (e.g., 220Ω - 330Ω) in series with LEDs to prevent burning them out.
- Be careful with
3Vand5Vpins. Connecting a5Vsignal to a3VGPIO pin can destroy it.
-
gpiozeroAlternative: If you prefer a more object-oriented, high-level library,gpiozerois an excellent choice that also supports Orange Pi. You might need to install it withpip3 install gpiozero. It abstracts away a lot of the low-level details.
