These additional interfaces are provided to group collections of components together for ease of use, and as examples. They are composites made up of components from the various API - Input Devices and API - Output Devices provided by GPIO Zero. See those pages for more information on using components individually.
Note
All GPIO pin numbers use Broadcom (BCM) numbering. See the Basic Recipes page for more information.
Extends LEDCollection and represents a generic LED board or collection of LEDs.
The following example turns on all the LEDs on a board containing 5 LEDs attached to GPIO pins 2 through 6:
from gpiozero import LEDBoard
leds = LEDBoard(2, 3, 4, 5, 6)
leds.on()
Parameters: |
|
---|
Make all the LEDs turn on and off repeatedly.
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Turn all the output devices off.
Turn all the output devices on.
Make the device fade in and out repeatedly.
Parameters: |
|
---|
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends LEDCollection to control a line of LEDs representing a bar graph. Positive values (0 to 1) light the LEDs from first to last. Negative values (-1 to 0) light the LEDs from last to first.
The following example demonstrates turning on the first two and last two LEDs in a board containing five LEDs attached to GPIOs 2 through 6:
from gpiozero import LEDBarGraph
from time import sleep
graph = LEDBarGraph(2, 3, 4, 5, 6)
graph.value = 2/5 # Light the first two LEDs only
sleep(1)
graph.value = -2/5 # Light the last two LEDs only
sleep(1)
graph.off()
As with other output devices, source and values are supported:
from gpiozero import LEDBarGraph, MCP3008
from signal import pause
graph = LEDBarGraph(2, 3, 4, 5, 6, pwm=True)
pot = MCP3008(channel=0)
graph.source = pot.values
pause()
Parameters: |
|
---|
Turn all the output devices off.
Turn all the output devices on.
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
The value of the LED bar graph. When no LEDs are lit, the value is 0. When all LEDs are lit, the value is 1. Values between 0 and 1 light LEDs linearly from first to last. Values between 0 and -1 light LEDs linearly from last to first.
To light a particular number of LEDs, simply divide that number by the number of LEDs. For example, if your graph contains 3 LEDs, the following will light the first:
from gpiozero import LEDBarGraph
graph = LEDBarGraph(12, 16, 19)
graph.value = 1/3
Note
Setting value to -1 will light all LEDs. However, querying it subsequently will return 1 as both representations are the same in hardware. The readable range of value is effectively -1 < value <= 1.
An infinite iterator of values read from value.
Extends CompositeDevice and represents a generic button board or collection of buttons.
Parameters: |
|
---|
Pause the script until the device is activated, or the timeout is reached.
Parameters: | timeout (float) – Number of seconds to wait before proceeding. If this is None (the default), then wait indefinitely until the device is active. |
---|
Pause the script until the device is deactivated, or the timeout is reached.
Parameters: | timeout (float) – Number of seconds to wait before proceeding. If this is None (the default), then wait indefinitely until the device is inactive. |
---|
Pause the script until the device is activated, or the timeout is reached.
Parameters: | timeout (float) – Number of seconds to wait before proceeding. If this is None (the default), then wait indefinitely until the device is active. |
---|
Pause the script until the device is deactivated, or the timeout is reached.
Parameters: | timeout (float) – Number of seconds to wait before proceeding. If this is None (the default), then wait indefinitely until the device is inactive. |
---|
The length of time (in seconds) that the device has been active for. When the device is inactive, this is None.
The length of time (in seconds) that the device has been held for. This is counted from the first execution of the when_held event rather than when the device activated, in contrast to active_time. If the device is not currently held, this is None.
If True, when_held will be executed repeatedly with hold_time seconds between each invocation.
The length of time (in seconds) to wait after the device is activated, until executing the when_held handler. If hold_repeat is True, this is also the length of time between invocations of when_held.
The length of time (in seconds) that the device has been inactive for. When the device is active, this is None.
The length of time (in seconds) that the device has been active for. When the device is inactive, this is None.
If True, the device uses a pull-up resistor to set the GPIO pin “high” by default.
An infinite iterator of values read from value.
The function to run when the device changes state from inactive to active.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated will be passed as that parameter.
Set this property to None (the default) to disable the event.
The function to run when the device changes state from active to inactive.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that deactivated will be passed as that parameter.
Set this property to None (the default) to disable the event.
The function to run when the device has remained active for hold_time seconds.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated will be passed as that parameter.
Set this property to None (the default) to disable the event.
The function to run when the device changes state from inactive to active.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated will be passed as that parameter.
Set this property to None (the default) to disable the event.
The function to run when the device changes state from active to inactive.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that deactivated will be passed as that parameter.
Set this property to None (the default) to disable the event.
Extends LEDBoard for devices containing red, yellow, and green LEDs.
The following example initializes a device connected to GPIO pins 2, 3, and 4, then lights the amber (yellow) LED attached to GPIO 3:
from gpiozero import TrafficLights
traffic = TrafficLights(2, 3, 4)
traffic.amber.on()
Parameters: |
|
---|
Make all the LEDs turn on and off repeatedly.
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Turn all the output devices off.
Turn all the output devices on.
Make the device fade in and out repeatedly.
Parameters: |
|
---|
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends RGBLED for the PiBorg LedBorg: an add-on board containing a very bright RGB LED.
The LedBorg pins are fixed and therefore there’s no need to specify them when constructing this class. The following example turns the LedBorg purple:
from gpiozero import LedBorg
led = LedBorg()
led.color = (1, 0, 1)
Parameters: |
|
---|
Make the device turn on and off repeatedly.
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Turn the LED off. This is equivalent to setting the LED color to black (0, 0, 0).
Turn the LED on. This equivalent to setting the LED color to white (1, 1, 1).
Make the device fade in and out repeatedly.
Parameters: |
|
---|
Toggle the state of the device. If the device is currently off (value is (0, 0, 0)), this changes it to “fully” on (value is (1, 1, 1)). If the device has a specific color, this method inverts the color.
Represents the color of the LED as an RGB 3-tuple of (red, green, blue) where each value is between 0 and 1 if pwm was True when the class was constructed (and only 0 or 1 if not).
For example, purple would be (1, 0, 1) and yellow would be (1, 1, 0), while orange would be (1, 0.5, 0).
Returns True if the LED is currently active (not black) and False otherwise.
Returns True if the LED is currently active (not black) and False otherwise.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
Represents the color of the LED as an RGB 3-tuple of (red, green, blue) where each value is between 0 and 1 if pwm was True when the class was constructed (and only 0 or 1 if not).
For example, purple would be (1, 0, 1) and yellow would be (1, 1, 0), while orange would be (1, 0.5, 0).
An infinite iterator of values read from value.
Extends LEDBoard for the Ciseco Pi-LITEr: a strip of 8 very bright LEDs.
The Pi-LITEr pins are fixed and therefore there’s no need to specify them when constructing this class. The following example turns on all the LEDs of the Pi-LITEr:
from gpiozero import PiLiter
lite = PiLiter()
lite.on()
Parameters: |
|
---|
Make all the LEDs turn on and off repeatedly.
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Turn all the output devices off.
Turn all the output devices on.
Make the device fade in and out repeatedly.
Parameters: |
|
---|
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends LEDBarGraph to treat the Ciseco Pi-LITEr as an 8-segment bar graph.
The Pi-LITEr pins are fixed and therefore there’s no need to specify them when constructing this class. The following example sets the graph value to 0.5:
from gpiozero import PiLiterBarGraph
graph = PiLiterBarGraph()
graph.value = 0.5
Parameters: |
|
---|
Turn all the output devices off.
Turn all the output devices on.
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
The value of the LED bar graph. When no LEDs are lit, the value is 0. When all LEDs are lit, the value is 1. Values between 0 and 1 light LEDs linearly from first to last. Values between 0 and -1 light LEDs linearly from last to first.
To light a particular number of LEDs, simply divide that number by the number of LEDs. For example, if your graph contains 3 LEDs, the following will light the first:
from gpiozero import LEDBarGraph
graph = LEDBarGraph(12, 16, 19)
graph.value = 1/3
Note
Setting value to -1 will light all LEDs. However, querying it subsequently will return 1 as both representations are the same in hardware. The readable range of value is effectively -1 < value <= 1.
An infinite iterator of values read from value.
Extends TrafficLights for the Low Voltage Labs PI-TRAFFIC vertical traffic lights board when attached to GPIO pins 9, 10, and 11.
There’s no need to specify the pins if the PI-TRAFFIC is connected to the default pins (9, 10, 11). The following example turns on the amber LED on the PI-TRAFFIC:
from gpiozero import PiTraffic
traffic = PiTraffic()
traffic.amber.on()
To use the PI-TRAFFIC board when attached to a non-standard set of pins, simply use the parent class, TrafficLights.
Parameters: |
|
---|
Make all the LEDs turn on and off repeatedly.
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Turn all the output devices off.
Turn all the output devices on.
Make the device fade in and out repeatedly.
Parameters: |
|
---|
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends TrafficLights for the PiHardware Pi-Stop: a vertical traffic lights board.
The following example turns on the amber LED on a Pi-Stop connected to location A+:
from gpiozero import PiStop
traffic = PiStop('A+')
traffic.amber.on()
Parameters: |
|
---|
Make all the LEDs turn on and off repeatedly.
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Turn all the output devices off.
Turn all the output devices on.
Make the device fade in and out repeatedly.
Parameters: |
|
---|
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends CompositeOutputDevice and is a generic class for HATs with traffic lights, a button and a buzzer.
Parameters: |
|
---|
Turn all the output devices off.
Turn all the output devices on.
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends TrafficLightsBuzzer for the Pi Supply FishDish: traffic light LEDs, a button and a buzzer.
The FishDish pins are fixed and therefore there’s no need to specify them when constructing this class. The following example waits for the button to be pressed on the FishDish, then turns on all the LEDs:
from gpiozero import FishDish
fish = FishDish()
fish.button.wait_for_press()
fish.lights.on()
Parameters: |
|
---|
Turn all the output devices off.
Turn all the output devices on.
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends TrafficLightsBuzzer for the Ryanteck Traffic HAT: traffic light LEDs, a button and a buzzer.
The Traffic HAT pins are fixed and therefore there’s no need to specify them when constructing this class. The following example waits for the button to be pressed on the Traffic HAT, then turns on all the LEDs:
from gpiozero import TrafficHat
hat = TrafficHat()
hat.button.wait_for_press()
hat.lights.on()
Parameters: |
|
---|
Turn all the output devices off.
Turn all the output devices on.
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends CompositeDevice to represent a generic dual-motor robot.
This class is constructed with two tuples representing the forward and backward pins of the left and right controllers respectively. For example, if the left motor’s controller is connected to GPIOs 4 and 14, while the right motor’s controller is connected to GPIOs 17 and 18 then the following example will drive the robot forward:
from gpiozero import Robot
robot = Robot(left=(4, 14), right=(17, 18))
robot.forward()
Parameters: |
|
---|
Drive the robot backward by running both motors backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Drive the robot forward by running both motors forward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Make the robot turn left by running the right motor forward and left motor backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Reverse the robot’s current motor directions. If the robot is currently running full speed forward, it will run full speed backward. If the robot is turning left at half-speed, it will turn right at half-speed. If the robot is currently stopped it will remain stopped.
Make the robot turn right by running the left motor forward and right motor backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Stop the robot.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
Represents the motion of the robot as a tuple of (left_motor_speed, right_motor_speed) with (-1, -1) representing full speed backwards, (1, 1) representing full speed forwards, and (0, 0) representing stopped.
An infinite iterator of values read from value.
Extends Robot for the Ryanteck motor controller board.
The Ryanteck MCB pins are fixed and therefore there’s no need to specify them when constructing this class. The following example drives the robot forward:
from gpiozero import RyanteckRobot
robot = RyanteckRobot()
robot.forward()
Parameters: | pin_factory (Factory) – See API - Pins for more information (this is an advanced feature which most users can ignore). |
---|
Drive the robot backward by running both motors backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Drive the robot forward by running both motors forward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Make the robot turn left by running the right motor forward and left motor backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Reverse the robot’s current motor directions. If the robot is currently running full speed forward, it will run full speed backward. If the robot is turning left at half-speed, it will turn right at half-speed. If the robot is currently stopped it will remain stopped.
Make the robot turn right by running the left motor forward and right motor backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Stop the robot.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
Represents the motion of the robot as a tuple of (left_motor_speed, right_motor_speed) with (-1, -1) representing full speed backwards, (1, 1) representing full speed forwards, and (0, 0) representing stopped.
An infinite iterator of values read from value.
Extends Robot for the CamJam #3 EduKit motor controller board.
The CamJam robot controller pins are fixed and therefore there’s no need to specify them when constructing this class. The following example drives the robot forward:
from gpiozero import CamJamKitRobot
robot = CamJamKitRobot()
robot.forward()
Parameters: | pin_factory (Factory) – See API - Pins for more information (this is an advanced feature which most users can ignore). |
---|
Drive the robot backward by running both motors backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Drive the robot forward by running both motors forward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Make the robot turn left by running the right motor forward and left motor backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Reverse the robot’s current motor directions. If the robot is currently running full speed forward, it will run full speed backward. If the robot is turning left at half-speed, it will turn right at half-speed. If the robot is currently stopped it will remain stopped.
Make the robot turn right by running the left motor forward and right motor backward.
Parameters: | speed (float) – Speed at which to drive the motors, as a value between 0 (stopped) and 1 (full speed). The default is 1. |
---|
Stop the robot.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
Represents the motion of the robot as a tuple of (left_motor_speed, right_motor_speed) with (-1, -1) representing full speed backwards, (1, 1) representing full speed forwards, and (0, 0) representing stopped.
An infinite iterator of values read from value.
Extends Device to represent an Energenie socket controller.
This class is constructed with a socket number and an optional initial state (defaults to False, meaning off). Instances of this class can be used to switch peripherals on and off. For example:
from gpiozero import Energenie
lamp = Energenie(1)
lamp.on()
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Returns True if the device is currently active and False otherwise. This property is usually derived from value. Unlike value, this is always a boolean.
The iterable to use as a source of values for value.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
An infinite iterator of values read from value.
Extends LEDBoard for The Pi Hut’s STATUS Zero: a Pi Zero sized add-on board with three sets of red/green LEDs to provide a status indicator.
The following example designates the first strip the label “wifi” and the second “raining”, and turns them green and red respectfully:
from gpiozero import StatusZero
status = StatusZero('wifi', 'raining')
status.wifi.green.on()
status.raining.red.on()
Parameters: |
|
---|
Make all the LEDs turn on and off repeatedly.
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Turn all the output devices off.
Turn all the output devices on.
Make the device fade in and out repeatedly.
Parameters: |
|
---|
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends CompositeOutputDevice for The Pi Hut’s STATUS board: a HAT sized add-on board with five sets of red/green LEDs and buttons to provide a status indicator with additional input.
The following example designates the first strip the label “wifi” and the second “raining”, turns the wifi green and then activates the button to toggle its lights when pressed:
from gpiozero import StatusBoard
status = StatusBoard('wifi', 'raining')
status.wifi.lights.green.on()
status.wifi.button.when_pressed = status.wifi.lights.toggle
Parameters: |
|
---|
Turn all the output devices off.
Turn all the output devices on.
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
Extends LEDBoard for the Ryanteck SnowPi board.
The SnowPi pins are fixed and therefore there’s no need to specify them when constructing this class. The following example turns on the eyes, sets the nose pulsing, and the arms blinking:
from gpiozero import SnowPi
snowman = SnowPi(pwm=True)
snowman.eyes.on()
snowman.nose.pulse()
snowman.arms.blink()
Parameters: |
|
---|
Make all the LEDs turn on and off repeatedly.
Parameters: |
|
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Turn all the output devices off.
Turn all the output devices on.
Make the device fade in and out repeatedly.
Parameters: |
|
---|
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
The delay (measured in seconds) in the loop used to read values from source. Defaults to 0.01 seconds which is generally sufficient to keep CPU usage to a minimum while providing adequate responsiveness.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
An infinite iterator of values read from value.
The classes in the sections above are derived from a series of base classes, some of which are effectively abstract. The classes form the (partial) hierarchy displayed in the graph below:
For composite devices, the following chart shows which devices are composed of which other devices:
The following sections document these base classes for advanced users that wish to construct classes for their own devices.
Extends CompositeOutputDevice. Abstract base class for LEDBoard and LEDBarGraph.
A flat tuple of all LEDs contained in this collection (and all sub-collections).
Extends CompositeDevice with on(), off(), and toggle() methods for controlling subordinate output devices. Also extends value to be writeable.
Parameters: |
|
---|
Turn all the output devices off.
Turn all the output devices on.
Toggle all the output devices. For each device, if it’s on, turn it off; if it’s off, turn it on.
A tuple containing a value for each subordinate device. This property can also be set to update the state of all subordinate output devices.
Extends Device. Represents a device composed of multiple devices like simple HATs, H-bridge motor controllers, robots composed of multiple motors, etc.
The constructor accepts subordinate devices as positional or keyword arguments. Positional arguments form unnamed devices accessed via the all attribute, while keyword arguments are added to the device as named (read-only) attributes.
Parameters: | _order (list) – If specified, this is the order of named items specified by keyword arguments (to ensure that the value tuple is constructed with a specific order). All keyword arguments must be included in the collection. If omitted, an alphabetically sorted order will be selected for keyword arguments. |
---|
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...