RPi.GPIO is a popular Python library used on Raspberry Pi platforms to control GPIO pins.
In addition to GPIO control, it is also used by many other libraries to query the Raspberry Pi hardware version as header pin layouts differed between certain versions. And, currently, it also provides some useful software PWM functionality on all GPIO pins.
As the UP board has a similar header pin layout to the Raspberry Pi 2, we have created a port of the RPi.GPIO library for UP. This allows many existing Python scripts developed for Raspberry Pi to be used on UP also.
More information about RPi.GPIO is available here: https://pypi.python.org/pypi/RPi.GPIO (Note: version numbers are not quite in sync yet - something that will be resolved in the near future.)
RPi.GPIO for UP is already installed by default with ubilinux. To ensure that the most up-to-date version is installed, please run the following commands:
sudo apt-get update sudo apt-get install python-rpi.gpio
The following link contains information and examples for using RPi.GPIO: http://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/
Note that the GPIO pins are numbered according to the RPi2 hardware ("BCM") GPIO numbering scheme (see Linux GPIO numbers here for reference: http://www.up-community.org/UpWiki/index.php/40-pin_IO_Header)
Here are some simple examples replicated here:
The following Python script will blink an LED connected to GPIO 4 at a rate of 1Hz:
import RPi.GPIO as GPIO import time # Pin Definitons: ledPin = 4 # Pin Setup: GPIO.setmode(GPIO.BCM) GPIO.setup(ledPin, GPIO.OUT) # LED pin set as output print("Here we go! Press CTRL+C to exit") try: while 1: GPIO.output(ledPin, GPIO.HIGH) time.sleep(0.5) GPIO.output(ledPin, GPIO.LOW) time.sleep(0.5) except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly: GPIO.cleanup() # cleanup all GPIO
Fade an LED on GPIO output pin 5 using software PWM
This script will slowly fade on/off an LED connected to GPIO output 23
import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) ledPin = 23 GPIO.setup(ledPin, GPIO.OUT) p = GPIO.PWM(ledPin, 50) # channel=12 frequency=50Hz p.start(0) print("Here we go! Press CTRL+C to exit") try: while 1: for dc in range(0, 101, 5): p.ChangeDutyCycle(dc) time.sleep(0.1) for dc in range(100, -1, -5): p.ChangeDutyCycle(dc) time.sleep(0.1) except KeyboardInterrupt: pass p.stop() GPIO.cleanup()
In this example, an LED is connected to GPIO output pin 23 and a button connected to GPIO input pin 24. The LED will turn on when the button is pressed.
# External module imports import RPi.GPIO as GPIO import time # Pin Definitons: ledPin = 23 buttonPin = 24 # Pin Setup: GPIO.setmode(GPIO.BCM) GPIO.setup(ledPin, GPIO.OUT) # LED pin set as output GPIO.setup(buttonPin, GPIO.IN) # Button pin set as input print("Here we go! Press CTRL+C to exit") try: while 1: if GPIO.input(buttonPin): GPIO.output(ledPin, GPIO.LOW) else: GPIO.output(ledPin, GPIO.HIGH) except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly: GPIO.cleanup() # cleanup all GPIO