Skip to main content

A module to control Raspberry Pi GPIO channels

Project description

This package provides a class to control the GPIO on a Raspberry Pi.

Note that this module is unsuitable for real-time or timing critical applications. This is because you can not predict when Python will be busy garbage collecting. It also runs under the Linux kernel which is not suitable for real time applications - it is multitasking O/S and another process may be given priority over the CPU, causing jitter in your program. If you are after true real-time performance and predictability, buy yourself an Arduino http://www.arduino.cc !

Note that the current release does not support SPI, I2C, PWM or serial functionality on the RPi yet. This is planned for the near future - watch this space! One-wire functionality is also planned.

Example Usage :

import RPi.GPIO as GPIO

# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

# set up GPIO output channel
GPIO.setup(12, GPIO.OUT)

# set RPi board pin 12 high
GPIO.output(12, GPIO.HIGH)

# set up GPIO output channel with an initial state
GPIO.setup(26, GPIO.OUT, initial=GPIO.LOW)

# set up GPIO input with pull-up control
#   (pull_up_down be PUD_OFF, PUD_UP or PUD_DOWN, default PUD_OFF)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# input from RPi board pin 11.  Will return GPIO.HIGH==1 or GPIO.LOW==0
input_value = GPIO.input(11)

# Enable edge detection events
#   can be RISING, FALLING or BOTH
GPIO.add_event_detect(11, GPIO.RISING)

# Check to see if an event has occurred since the last time we checked (poll)
if GPIO.event_detected(11):
    print('Rising edge has occurred!')

# Add a threaded callback for an edge detection event. Note that event detection must be enabled first using add_event_detect()
def my_event_callback_function():
    print('Callback function called!')
GPIO.add_event_callback(11, my_event_callback_function)

# Remove edge detection events for a channel
GPIO.remove_event_detect(11)

# Another way of adding edge detection events with a threaded callback
def my_event_callback_function():
    print('Callback function called!')
GPIO.add_event_detect(11, GPIO.RISING, callback=my_event_callback_function)

# wait for a button press without polling (uses negligable CPU)
GPIO.wait_for_edge(11, GPIO.RISING)

# to change to BCM GPIO numbering
GPIO.setmode(GPIO.BCM)

# to reset every channel that has been set up by this program to INPUT with no pullup/pulldown and no event detection.
GPIO.cleanup()

Change Log

0.5.0a

  • Added new edge detection events (interrupt handling) - Added add_event_detect() - Added remove_event_detect() - Added add_event_callback() - Added wait_for_edge()

  • Removed old experimental event functions - Removed set_rising_event() - Removed set_falling_event() - Removed set_high_event() - Removed set_low_event()

  • Changed event_detected() for new edge detection functionality

  • input() now returns 0/LOW == False or 1/HIGH == True (integers) instead of False or True (booleans).

  • Fix error on repeated import (issue 3)

  • Change SetupException to a RuntimeError so it can be caught on import (issue 25, Chris Hager <chris@linuxuser.at>)

  • Improved docstrings of functions

0.4.2a

  • Fix for installing on Arch Linux (Python 3.3) (issue 20)

  • Initial value when setting a channel as an output (issue 19)

0.4.1a

  • Added VERSION

  • Permit input() of channels set as outputs (Eric Ptak <trouch@trouch.com>)

0.4.0a

  • Added support for Revision 2 boards

  • Added RPI_REVISION

  • Added cleanup() function and removed automatic reset functionality on program exit

  • Added get_function() to read existing GPIO channel functionality (suggestion from Eric Ptak <trouch@trouch.com>)

  • Added set_rising_event()

  • Added set_falling_event()

  • Added set_high_event()

  • Added set_low_event()

  • Added event_detected()

  • Added test/test.py

  • Converted debian to armhf

  • Fixed C function short_wait() (thanks to Thibault Porteboeuf <thibaultporteboeuf@gmail.com>)

0.3.1a

  • Fixed critical bug with swapped high/low state on outputs

  • Added pull-up / pull-down setup functionality for inputs

0.3.0a

  • Rewritten as a C extension

  • Now uses /dev/mem and SoC registers instead of /sys/class/gpio

  • Faster!

  • Make call to GPIO.setmode() mandatory

  • Added GPIO.HIGH and GPIO.LOW constants

0.2.0

  • Changed status from alpha to beta

  • Added setmode() to be able to use BCM GPIO 00.nn channel numbers

  • Renamed InvalidPinException to InvalidChannelException

0.1.0

  • Fixed direction bug

  • Added MANIFEST.in (to include missing file)

  • Changed GPIO channel number to pin number

  • Tested and working!

0.0.3a

  • Added GPIO table

  • Refactored

  • Fixed a few critical bugs

  • Still completely untested!

0.0.2a

  • Internal refactoring. Still completely untested!

0.0.1a

  • First version. Completely untested until I can get hold of a Raspberry Pi!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

RPi.GPIO-0.5.0a.tar.gz (20.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page