API Reference
The API of rpi-lgpio (naturally) follows that of rpi-gpio (aka RPi.GPIO) as closely as possible. As such the following is simply a re-iteration of that API.
Initialization
- RPi.GPIO.setmode(new_mode)[source]
Set up the numbering mode to use for the pins on the board. The options for new_mode are:
If a numbering mode has already been set, and new_mode is not the same as the result of
getmode()
, aValueError
is raised.- Parameters:
new_mode (int) – The new numbering mode to apply
- RPi.GPIO.getmode()[source]
Get the numbering mode used for the pins on the board. Returns
BOARD
,BCM
orNone
.
- RPi.GPIO.setup(chanlist, direction, pull_up_down=20, initial=None)[source]
Set up a GPIO channel or iterable of channels with a direction and (optionally, for inputs) pull/up down control, or (optionally, for outputs) and initial state.
The GPIOs to affect are listed in chanlist which may be any iterable. The direction is either
IN
orOUT
.If direction is
IN
, then pull_up_down may specify one of the valuesPUD_UP
to set the internal pull-up resistor,PUD_DOWN
to set the internal pull-down resistor, or the defaultPUD_OFF
which disables the internal pulls.If direction is
OUT
, then initial may specify zero or one to indicate the initial state of the output.- Parameters:
Pin Usage
- RPi.GPIO.input(channel)[source]
Input from a GPIO channel. Returns 1 or 0.
This can also be called on a GPIO output, in which case the value returned will be the last state set on the GPIO.
- RPi.GPIO.output(channel, value)[source]
Output to a GPIO channel or list of channels. The value can be the integer
LOW
orHIGH
, or a list of integers.If a list of channels is specified, with a single integer for the value then it is applied to all channels. Otherwise, the length of the two lists must match.
Edge Detection
- RPi.GPIO.wait_for_edge(channel, edge, bouncetime=None, timeout=None)[source]
Wait for an edge on the specified channel. Returns channel or
None
if timeout elapses before the specified edge occurs.Note
Debounce works significantly differently in rpi-lgpio than it does in rpi-gpio; please see Debounce for more information on the differences.
- RPi.GPIO.add_event_detect(channel, edge, callback=None, bouncetime=None)[source]
Start background edge detection on the specified GPIO channel.
If callback is specified, it must be a callable that will be executed when the specified edge is seen on the GPIO channel. The callable must accept a single parameter: the channel on which the edge was detected.
Note
Debounce works significantly differently in rpi-lgpio than it does in rpi-gpio; please see Debounce for more information on the differences.
- Parameters:
channel (int) – The board pin number or BCM number depending on
setmode()
to watch for changescallback (callable or None) – The callback to run when an edge is detected; must take a single integer parameter of the channel on which the edge was detected
bouncetime (int or None) – Time (in ms) used to debounce signals
- RPi.GPIO.add_event_callback(channel, callback)[source]
Add a callback to the specified GPIO channel which must already have been set for background edge detection with
add_event_detect()
.
- RPi.GPIO.event_detected(channel)[source]
Returns
True
if an edge has occurred on the specified channel since the last query of the channel (if any). Querying this will also reset the internal edge detected flag for this channel.The channel must previously have had edge detection enabled with
add_event_detect()
.
Miscellaneous
PWM
- class RPi.GPIO.PWM(channel, frequency)[source]
Initializes and controls software-based PWM (Pulse Width Modulation) on the specified channel at frequency (in Hz).
Call
start()
andstop()
to generate and stop the actual output respectively.ChangeFrequency()
andChangeDutyCycle()
can also be used to control the output.Note
Letting the
PWM
object go out of scope (and be garbage collected) will implicitly stop the PWM.- ChangeDutyCycle(dc)[source]
Changes the duty cycle (percentage of the time that the pin is “on”) to dc.
Constants
- RPi.GPIO.RPI_INFO
A dictionary that provides information about the model of Raspberry Pi that the library is loaded onto. Includes the following keys:
- P1_REVISION
The revision of the P1 header. 0 indicates no P1 header (typical on the compute module range), 1 and 2 vary on the oldest Raspberry Pi models, and 3 is the typical 40-pin header present on all modern Raspberry Pis.
- REVISION
The hex board revision code as a
str
.- TYPE
The name of the Pi model, e.g. “Pi 4 Model B”
- MANUFACTURER
The name of the board manufacturer, e.g. “Sony UK”
- PROCESSOR
The name of the SoC used on the board, e.g. “BCM2711”
- RAM
The amount of RAM installed on the board, e.g. “4GB”
The board revision can be overridden with the
RPI_LGPIO_REVISION
environment variable; see Pi Revision for further details.
- RPi.GPIO.OUT
Used with
setup()
to set a GPIO to an output, andgpio_function()
to report a GPIO is an output
- RPi.GPIO.IN
Used with
setup()
to set a GPIO to an input, andgpio_function()
to report a GPIO is an input
- RPi.GPIO.HARD_PWM
- RPi.GPIO.SERIAL
- RPi.GPIO.I2C
- RPi.GPIO.SPI
Used with
gpio_function()
to indicate “alternate” modes of certain GPIO pins.Note
In rpi-lgpio these values will never be returned as the kernel device cannot report if pins are in alternate modes.
- RPi.GPIO.RISING
Used with
wait_for_edge()
andadd_event_detect()
to specify that rising edges only should be sampled
- RPi.GPIO.FALLING
Used with
wait_for_edge()
andadd_event_detect()
to specify that falling edges only should be sampled
- RPi.GPIO.BOTH
Used with
wait_for_edge()
andadd_event_detect()
to specify that all edges should be sampled