These input device component interfaces have been provided for simple use of everyday components. Components must be wired up correctly before use in code.
Note
All GPIO pin numbers use Broadcom (BCM) numbering. See the Recipes page for more information.
Extends DigitalInputDevice and represents a simple push button or switch.
Connect one side of the button to a ground pin, and the other to any GPIO pin. Alternatively, connect one side of the button to the 3V3 pin, and the other to any GPIO pin, then set pull_up to False in the Button constructor.
The following example will print a line of text when the button is pushed:
from gpiozero import Button
button = Button(4)
button.wait_for_press()
print("The button was pressed!")
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. |
---|
The Pin that the device is connected to. This will be None if the device has been closed (see the close() method). When dealing with GPIO pins, query pin.number to discover the GPIO pin (in BCM numbering) that the device is connected to.
Returns True if the device is currently active and False otherwise.
If True, the device uses a pull-up resistor to set the GPIO pin “high” by default. Defaults to False.
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 SmoothedInputDevice and represents a passive infra-red (PIR) motion sensor like the sort found in the CamJam #2 EduKit.
A typical PIR device has a small circuit board with three pins: VCC, OUT, and GND. VCC should be connected to a 5V pin, GND to one of the ground pins, and finally OUT to the GPIO specified as the value of the pin parameter in the constructor.
The following code will print a line of text when motion is detected:
from gpiozero import MotionSensor
pir = MotionSensor(4)
pir.wait_for_motion()
print("Motion detected!")
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. |
---|
The Pin that the device is connected to. This will be None if the device has been closed (see the close() method). When dealing with GPIO pins, query pin.number to discover the GPIO pin (in BCM numbering) that the device is connected to.
Returns True if the device is currently active and False otherwise.
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 SmoothedInputDevice and represents a light dependent resistor (LDR).
Connect one leg of the LDR to the 3V3 pin; connect one leg of a 1µf capacitor to a ground pin; connect the other leg of the LDR and the other leg of the capacitor to the same GPIO pin. This class repeatedly discharges the capacitor, then times the duration it takes to charge (which will vary according to the light falling on the LDR).
The following code will print a line of text when light is detected:
from gpiozero import LightSensor
ldr = LightSensor(18)
ldr.wait_for_light()
print("Light detected!")
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. |
---|
The Pin that the device is connected to. This will be None if the device has been closed (see the close() method). When dealing with GPIO pins, query pin.number to discover the GPIO pin (in BCM numbering) that the device is connected to.
Returns True if the device is currently active and False otherwise.
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 MCP3004 is a 10-bit analog to digital converter with 4 channels (0-3).
The SPI bus that the device is connected to. As the Pi only has a single (user accessible) SPI bus, this always returns 0.
The select pin that the device is connected to. The Pi has two select pins so this will be 0 or 1.
The channel to read data from. The MCP3008/3208/3304 have 8 channels (0-7), while the MCP3004/3204/3302 have 4 channels (0-3), and the MCP3301 only has 1 channel.
The current value read from the device, scaled to a value between 0 and 1.
If True, the device is operated in pseudo-differential mode. In this mode one channel (specified by the channel attribute) is read relative to the value of a second channel (implied by the chip’s design).
Please refer to the device data-sheet to determine which channel is used as the relative base value (for example, when using an MCP3008 in differential mode, channel 0 is read relative to channel 1).
The MCP3008 is a 10-bit analog to digital converter with 8 channels (0-7).
The SPI bus that the device is connected to. As the Pi only has a single (user accessible) SPI bus, this always returns 0.
The select pin that the device is connected to. The Pi has two select pins so this will be 0 or 1.
The channel to read data from. The MCP3008/3208/3304 have 8 channels (0-7), while the MCP3004/3204/3302 have 4 channels (0-3), and the MCP3301 only has 1 channel.
The current value read from the device, scaled to a value between 0 and 1.
If True, the device is operated in pseudo-differential mode. In this mode one channel (specified by the channel attribute) is read relative to the value of a second channel (implied by the chip’s design).
Please refer to the device data-sheet to determine which channel is used as the relative base value (for example, when using an MCP3008 in differential mode, channel 0 is read relative to channel 1).
The MCP3204 is a 12-bit analog to digital converter with 4 channels (0-3).
The SPI bus that the device is connected to. As the Pi only has a single (user accessible) SPI bus, this always returns 0.
The select pin that the device is connected to. The Pi has two select pins so this will be 0 or 1.
The channel to read data from. The MCP3008/3208/3304 have 8 channels (0-7), while the MCP3004/3204/3302 have 4 channels (0-3), and the MCP3301 only has 1 channel.
The current value read from the device, scaled to a value between 0 and 1.
If True, the device is operated in pseudo-differential mode. In this mode one channel (specified by the channel attribute) is read relative to the value of a second channel (implied by the chip’s design).
Please refer to the device data-sheet to determine which channel is used as the relative base value (for example, when using an MCP3008 in differential mode, channel 0 is read relative to channel 1).
The MCP3208 is a 12-bit analog to digital converter with 8 channels (0-7).
The SPI bus that the device is connected to. As the Pi only has a single (user accessible) SPI bus, this always returns 0.
The select pin that the device is connected to. The Pi has two select pins so this will be 0 or 1.
The channel to read data from. The MCP3008/3208/3304 have 8 channels (0-7), while the MCP3004/3204/3302 have 4 channels (0-3), and the MCP3301 only has 1 channel.
The current value read from the device, scaled to a value between 0 and 1.
If True, the device is operated in pseudo-differential mode. In this mode one channel (specified by the channel attribute) is read relative to the value of a second channel (implied by the chip’s design).
Please refer to the device data-sheet to determine which channel is used as the relative base value (for example, when using an MCP3008 in differential mode, channel 0 is read relative to channel 1).
The MCP3301 is a signed 13-bit analog to digital converter. Please note that the MCP3301 always operates in differential mode between its two channels and the output value is scaled from -1 to +1.
The SPI bus that the device is connected to. As the Pi only has a single (user accessible) SPI bus, this always returns 0.
The select pin that the device is connected to. The Pi has two select pins so this will be 0 or 1.
The current value read from the device, scaled to a value between 0 and 1.
The MCP3302 is a 12/13-bit analog to digital converter with 4 channels (0-3). When operated in differential mode, the device outputs a signed 13-bit value which is scaled from -1 to +1. When operated in single-ended mode (the default), the device outputs an unsigned 12-bit value scaled from 0 to 1.
The SPI bus that the device is connected to. As the Pi only has a single (user accessible) SPI bus, this always returns 0.
The select pin that the device is connected to. The Pi has two select pins so this will be 0 or 1.
The channel to read data from. The MCP3008/3208/3304 have 8 channels (0-7), while the MCP3004/3204/3302 have 4 channels (0-3), and the MCP3301 only has 1 channel.
The current value read from the device, scaled to a value between 0 and 1.
If True, the device is operated in pseudo-differential mode. In this mode one channel (specified by the channel attribute) is read relative to the value of a second channel (implied by the chip’s design).
Please refer to the device data-sheet to determine which channel is used as the relative base value (for example, when using an MCP3008 in differential mode, channel 0 is read relative to channel 1).
The MCP3304 is a 12/13-bit analog to digital converter with 8 channels (0-7). When operated in differential mode, the device outputs a signed 13-bit value which is scaled from -1 to +1. When operated in single-ended mode (the default), the device outputs an unsigned 12-bit value scaled from 0 to 1.
The SPI bus that the device is connected to. As the Pi only has a single (user accessible) SPI bus, this always returns 0.
The select pin that the device is connected to. The Pi has two select pins so this will be 0 or 1.
The channel to read data from. The MCP3008/3208/3304 have 8 channels (0-7), while the MCP3004/3204/3302 have 4 channels (0-3), and the MCP3301 only has 1 channel.
The current value read from the device, scaled to a value between 0 and 1.
If True, the device is operated in pseudo-differential mode. In this mode one channel (specified by the channel attribute) is read relative to the value of a second channel (implied by the chip’s design).
Please refer to the device data-sheet to determine which channel is used as the relative base value (for example, when using an MCP3008 in differential mode, channel 0 is read relative to channel 1).