20. API - Pins

As of release 1.1, the GPIO Zero library can be roughly divided into two things: pins and the devices that are connected to them. The majority of the documentation focuses on devices as pins are below the level that most users are concerned with. However, some users may wish to take advantage of the capabilities of alternative GPIO implementations or (in future) use GPIO extender chips. This is the purpose of the pins portion of the library.

When you construct a device, you pass in a pin specification. This is passed to a pin Factory which turns it into a Pin implementation. The default factory can be queried (and changed) with Device.pin_factory, i.e. the pin_factory attribute of the Device class. However, all classes accept a pin_factory keyword argument to their constructors permitting the factory to be overridden on a per-device basis (the reason for allowing per-device factories is made apparent later in the Configuring Remote GPIO chapter).

This is illustrated in the following flow-chart:

_images/device_pin_flowchart.svg

The default factory is constructed when GPIO Zero is first imported; if no default factory can be constructed (e.g. because no GPIO implementations are installed, or all of them fail to load for whatever reason), an ImportError will be raised.

20.1. Changing the pin factory

The default pin factory can be replaced by specifying a value for the GPIOZERO_PIN_FACTORY environment variable. For example:

$ GPIOZERO_PIN_FACTORY=native python
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiozero
>>> gpiozero.Device.pin_factory
<gpiozero.pins.native.NativeFactory object at 0x762c26b0>

To set the GPIOZERO_PIN_FACTORY for the rest of your session you can export this value:

$ export GPIOZERO_PIN_FACTORY=native
$ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiozero
>>> gpiozero.Device.pin_factory
<gpiozero.pins.native.NativeFactory object at 0x762c26b0>
>>> quit()
$ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gpiozero
>>> gpiozero.Device.pin_factory
<gpiozero.pins.native.NativeFactory object at 0x76401330>

If you add the export command to your ~/.bashrc file, you’ll set the default pin factory for all future sessions too.

The following values, and the corresponding Factory and Pin classes are listed in the table below. Factories are listed in the order that they are tried by default.

Name Factory class Pin class
rpigpio gpiozero.pins.rpigpio.RPiGPIOFactory gpiozero.pins.rpigpio.RPiGPIOPin
rpio gpiozero.pins.rpio.RPIOFactory gpiozero.pins.rpio.RPIOPin
pigpio gpiozero.pins.pigpio.PiGPIOFactory gpiozero.pins.pigpio.PiGPIOPin
native gpiozero.pins.native.NativeFactory gpiozero.pins.native.NativePin

If you need to change the default pin factory from within a script, either set Device.pin_factory to the new factory instance to use:

from gpiozero.pins.native import NativeFactory
from gpiozero import Device, LED

Device.pin_factory = NativeFactory()

# These will now implicitly use NativePin instead of
# RPiGPIOPin
led1 = LED(16)
led2 = LED(17)

Or use the pin_factory keyword parameter mentioned above:

from gpiozero.pins.native import NativeFactory
from gpiozero import LED

my_factory = NativeFactory()

# This will use NativePin instead of RPiGPIOPin for led1
# but led2 will continue to use RPiGPIOPin
led1 = LED(16, pin_factory=my_factory)
led2 = LED(17)

Certain factories may take default information from additional sources. For example, to default to creating pins with gpiozero.pins.pigpio.PiGPIOPin on a remote pi called remote-pi you can set the PIGPIO_ADDR environment variable when running your script:

$ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=remote-pi python3 my_script.py

Like the GPIOZERO_PIN_FACTORY value, these can be exported from your ~/.bashrc script too.

Warning

The astute and mischievous reader may note that it is possible to mix factories, e.g. using RPiGPIOFactory for one pin, and NativeFactory for another. This is unsupported, and if it results in your script crashing, your components failing, or your Raspberry Pi turning into an actual raspberry pie, you have only yourself to blame.

Sensible uses of multiple pin factories are given in Configuring Remote GPIO.

20.2. Mock pins

There’s also a gpiozero.pins.mock.MockFactory which generates entirely fake pins. This was originally intended for GPIO Zero developers who wish to write tests for devices without having to have the physical device wired in to their Pi. However, they have also proven relatively useful in developing GPIO Zero scripts without having a Pi to hand. This pin factory will never be loaded by default; it must be explicitly specified. For example:

from gpiozero.pins.mock import MockFactory
from gpiozero import Device, Button, LED
from time import sleep

# Set the default pin factory to a mock factory
Device.pin_factory = MockFactory()

# Construct a couple of devices attached to mock pins 16 and 17, and link the
# devices
led = LED(17)
btn = Button(16)
led.source = btn.values

# Here the button isn't "pushed" so the LED's value should be False
print(led.value)

# Get a reference to mock pin 16 (used by the button)
btn_pin = Device.pin_factory.pin(16)

# Drive the pin low (this is what would happen eletrically when the button is
# pushed)
btn_pin.drive_low()
sleep(0.1) # give source some time to re-read the button state
print(led.value)

btn_pin.drive_high()
sleep(0.1)
print(led.value)

Several sub-classes of mock pins exist for emulating various other things (pins that do/don’t support PWM, pins that are connected together, pins that drive high after a delay, etc). Interested users are invited to read the GPIO Zero test suite for further examples of usage.

20.3. Base classes

class gpiozero.Factory[source]

Generates pins and SPI interfaces for devices. This is an abstract base class for pin factories. Descendents may override the following methods, if applicable:

close()[source]

Closes the pin factory. This is expected to clean up all resources manipulated by the factory. It it typically called at script termination.

pin(spec)[source]

Creates an instance of a Pin descendent representing the specified pin.

Warning

Descendents must ensure that pin instances representing the same hardware are identical; i.e. two separate invocations of pin() for the same pin specification must return the same object.

release_all(reserver)[source]

Releases all pin reservations taken out by reserver. See release_pins() for further information).

release_pins(reserver, *pins)[source]

Releases the reservation of reserver against pins. This is typically called during Device.close() to clean up reservations taken during construction. Releasing a reservation that is not currently held will be silently ignored (to permit clean-up after failed / partial construction).

reserve_pins(requester, *pins)[source]

Called to indicate that the device reserves the right to use the specified pins. This should be done during device construction. If pins are reserved, you must ensure that the reservation is released by eventually called release_pins().

spi(**spi_args)[source]

Returns an instance of an SPI interface, for the specified SPI port and device, or for the specified pins (clock_pin, mosi_pin, miso_pin, and select_pin). Only one of the schemes can be used; attempting to mix port and device with pin numbers will raise SPIBadArgs.

pi_info

Returns a PiBoardInfo instance representing the Pi that instances generated by this factory will be attached to.

If the pins represented by this class are not directly attached to a Pi (e.g. the pin is attached to a board attached to the Pi, or the pins are not on a Pi at all), this may return None.

class gpiozero.Pin[source]

Abstract base class representing a pin attached to some form of controller, be it GPIO, SPI, ADC, etc.

Descendents should override property getters and setters to accurately represent the capabilities of pins. Descendents must override the following methods:

  • _get_function()
  • _set_function()
  • _get_state()

Descendents may additionally override the following methods, if applicable:

close()[source]

Cleans up the resources allocated to the pin. After this method is called, this Pin instance may no longer be used to query or control the pin’s state.

input_with_pull(pull)[source]

Sets the pin’s function to “input” and specifies an initial pull-up for the pin. By default this is equivalent to performing:

pin.function = 'input'
pin.pull = pull

However, descendents may override this order to provide the smallest possible delay between configuring the pin for input and pulling the pin up/down (which can be important for avoiding “blips” in some configurations).

output_with_state(state)[source]

Sets the pin’s function to “output” and specifies an initial state for the pin. By default this is equivalent to performing:

pin.function = 'output'
pin.state = state

However, descendents may override this in order to provide the smallest possible delay between configuring the pin for output and specifying an initial value (which can be important for avoiding “blips” in active-low configurations).

bounce

The amount of bounce detection (elimination) currently in use by edge detection, measured in seconds. If bounce detection is not currently in use, this is None.

For example, if edges is currently “rising”, bounce is currently 5/1000 (5ms), then the waveform below will only fire when_changed on two occasions despite there being three rising edges:

TIME 0...1...2...3...4...5...6...7...8...9...10..11..12 ms

bounce elimination   |===================| |==============

HIGH - - - - >       ,--. ,--------------. ,--.
                     |  | |              | |  |
                     |  | |              | |  |
LOW  ----------------'  `-'              `-'  `-----------
                     :                     :
                     :                     :
               when_changed          when_changed
                   fires                 fires

If the pin does not support edge detection, attempts to set this property will raise PinEdgeDetectUnsupported. If the pin supports edge detection, the class must implement bounce detection, even if only in software.

edges

The edge that will trigger execution of the function or bound method assigned to when_changed. This can be one of the strings “both” (the default), “rising”, “falling”, or “none”:

HIGH - - - - >           ,--------------.
                         |              |
                         |              |
LOW  --------------------'              `--------------
                         :              :
                         :              :
Fires when_changed     "both"         "both"
when edges is ...     "rising"       "falling"

If the pin does not support edge detection, attempts to set this property will raise PinEdgeDetectUnsupported.

frequency

The frequency (in Hz) for the pin’s PWM implementation, or None if PWM is not currently in use. This value always defaults to None and may be changed with certain pin types to activate or deactivate PWM.

If the pin does not support PWM, PinPWMUnsupported will be raised when attempting to set this to a value other than None.

function

The function of the pin. This property is a string indicating the current function or purpose of the pin. Typically this is the string “input” or “output”. However, in some circumstances it can be other strings indicating non-GPIO related functionality.

With certain pin types (e.g. GPIO pins), this attribute can be changed to configure the function of a pin. If an invalid function is specified, for this attribute, PinInvalidFunction will be raised.

pull

The pull-up state of the pin represented as a string. This is typically one of the strings “up”, “down”, or “floating” but additional values may be supported by the underlying hardware.

If the pin does not support changing pull-up state (for example because of a fixed pull-up resistor), attempts to set this property will raise PinFixedPull. If the specified value is not supported by the underlying hardware, PinInvalidPull is raised.

state

The state of the pin. This is 0 for low, and 1 for high. As a low level view of the pin, no swapping is performed in the case of pull ups (see pull for more information):

HIGH - - - - >       ,----------------------
                     |
                     |
LOW  ----------------'

Descendents which implement analog, or analog-like capabilities can return values between 0 and 1. For example, pins implementing PWM (where frequency is not None) return a value between 0.0 and 1.0 representing the current PWM duty cycle.

If a pin is currently configured for input, and an attempt is made to set this attribute, PinSetInput will be raised. If an invalid value is specified for this attribute, PinInvalidState will be raised.

when_changed

A function or bound method to be called when the pin’s state changes (more specifically when the edge specified by edges is detected on the pin). The function or bound method must take no parameters.

If the pin does not support edge detection, attempts to set this property will raise PinEdgeDetectUnsupported.

class gpiozero.SPI[source]

Abstract interface for Serial Peripheral Interface (SPI) implementations. Descendents must override the following methods:

Descendents may override the following methods, if applicable:

  • read()
  • write()
  • _set_clock_mode()
  • _get_lsb_first()
  • _set_lsb_first()
  • _get_select_high()
  • _set_select_high()
  • _get_bits_per_word()
  • _set_bits_per_word()
read(n)[source]

Read n words of data from the SPI interface, returning them as a sequence of unsigned ints, each no larger than the configured bits_per_word of the interface.

This method is typically used with read-only devices that feature half-duplex communication. See transfer() for full duplex communication.

transfer(data)[source]

Write data to the SPI interface. data must be a sequence of unsigned integer words each of which will fit within the configured bits_per_word of the interface. The method returns the sequence of words read from the interface while writing occurred (full duplex communication).

The length of the sequence returned dictates the number of words of data written to the interface. Each word in the returned sequence will be an unsigned integer no larger than the configured bits_per_word of the interface.

write(data)[source]

Write data to the SPI interface. data must be a sequence of unsigned integer words each of which will fit within the configured bits_per_word of the interface. The method returns the number of words written to the interface (which may be less than or equal to the length of data).

This method is typically used with write-only devices that feature half-duplex communication. See transfer() for full duplex communication.

bits_per_word

Controls the number of bits that make up a word, and thus where the word boundaries appear in the data stream, and the maximum value of a word. Defaults to 8 meaning that words are effectively bytes.

Several implementations do not support non-byte-sized words.

clock_mode

Presents a value representing the clock_polarity and clock_phase attributes combined according to the following table:

mode polarity (CPOL) phase (CPHA)
0 False False
1 False True
2 True False
3 True True

Adjusting this value adjusts both the clock_polarity and clock_phase attributes simultaneously.

clock_phase

The phase of the SPI clock pin. If this is False (the default), data will be read from the MISO pin when the clock pin activates. Setting this to True will cause data to be read from the MISO pin when the clock pin deactivates. On many data sheets this is documented as the CPHA value. Whether the clock edge is rising or falling when the clock is considered activated is controlled by the clock_polarity attribute (corresponding to CPOL).

The following diagram indicates when data is read when clock_polarity is False, and clock_phase is False (the default), equivalent to CPHA 0:

    ,---.   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.
CLK |   |   |   |   |   |   |   |   |   |   |   |   |   |
    |   |   |   |   |   |   |   |   |   |   |   |   |   |
----'   `---'   `---'   `---'   `---'   `---'   `---'   `-------
    :       :       :       :       :       :       :
MISO---.   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.
  /     \ /     \ /     \ /     \ /     \ /     \ /     \
-{  Bit  X  Bit  X  Bit  X  Bit  X  Bit  X  Bit  X  Bit  }------
  \     / \     / \     / \     / \     / \     / \     /
   `---'   `---'   `---'   `---'   `---'   `---'   `---'

The following diagram indicates when data is read when clock_polarity is False, but clock_phase is True, equivalent to CPHA 1:

    ,---.   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.
CLK |   |   |   |   |   |   |   |   |   |   |   |   |   |
    |   |   |   |   |   |   |   |   |   |   |   |   |   |
----'   `---'   `---'   `---'   `---'   `---'   `---'   `-------
        :       :       :       :       :       :       :
MISO   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.
      /     \ /     \ /     \ /     \ /     \ /     \ /     \
-----{  Bit  X  Bit  X  Bit  X  Bit  X  Bit  X  Bit  X  Bit  }--
      \     / \     / \     / \     / \     / \     / \     /
       `---'   `---'   `---'   `---'   `---'   `---'   `---'
clock_polarity

The polarity of the SPI clock pin. If this is False (the default), the clock pin will idle low, and pulse high. Setting this to True will cause the clock pin to idle high, and pulse low. On many data sheets this is documented as the CPOL value.

The following diagram illustrates the waveform when clock_polarity is False (the default), equivalent to CPOL 0:

       on      on      on      on      on      on      on
      ,---.   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.
CLK   |   |   |   |   |   |   |   |   |   |   |   |   |   |
      |   |   |   |   |   |   |   |   |   |   |   |   |   |
------'   `---'   `---'   `---'   `---'   `---'   `---'   `------
idle       off     off     off     off     off     off       idle

The following diagram illustrates the waveform when clock_polarity is True, equivalent to CPOL 1:

idle       off     off     off     off     off     off       idle
------.   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.   ,------
      |   |   |   |   |   |   |   |   |   |   |   |   |   |
CLK   |   |   |   |   |   |   |   |   |   |   |   |   |   |
      `---'   `---'   `---'   `---'   `---'   `---'   `---'
       on      on      on      on      on      on      on
lsb_first

Controls whether words are read and written LSB in (Least Significant Bit first) order. The default is False indicating that words are read and written in MSB (Most Significant Bit first) order. Effectively, this controls the Bit endianness of the connection.

The following diagram shows the a word containing the number 5 (binary 0101) transmitted on MISO with bits_per_word set to 4, and clock_mode set to 0, when lsb_first is False (the default):

    ,---.   ,---.   ,---.   ,---.
CLK |   |   |   |   |   |   |   |
    |   |   |   |   |   |   |   |
----'   `---'   `---'   `---'   `-----
    :     ,-------. :     ,-------.
MISO:     | :     | :     | :     |
    :     | :     | :     | :     |
----------' :     `-------' :     `----
    :       :       :       :
   MSB                     LSB

And now with lsb_first set to True (and all other parameters the same):

    ,---.   ,---.   ,---.   ,---.
CLK |   |   |   |   |   |   |   |
    |   |   |   |   |   |   |   |
----'   `---'   `---'   `---'   `-----
  ,-------. :     ,-------. :
MISO:     | :     | :     | :
  | :     | :     | :     | :
--' :     `-------' :     `-----------
    :       :       :       :
   LSB                     MSB
select_high

If False (the default), the chip select line is considered active when it is pulled low. When set to True, the chip select line is considered active when it is driven high.

The following diagram shows the waveform of the chip select line, and the clock when clock_polarity is False, and select_high is False (the default):

---.                                                     ,------
__ |                                                     |
CS |      chip is selected, and will react to clock      |  idle
   `-----------------------------------------------------'

    ,---.   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.
CLK |   |   |   |   |   |   |   |   |   |   |   |   |   |
    |   |   |   |   |   |   |   |   |   |   |   |   |   |
----'   `---'   `---'   `---'   `---'   `---'   `---'   `-------

And when select_high is True:

   ,-----------------------------------------------------.
CS |      chip is selected, and will react to clock      |  idle
   |                                                     |
---'                                                     `------

    ,---.   ,---.   ,---.   ,---.   ,---.   ,---.   ,---.
CLK |   |   |   |   |   |   |   |   |   |   |   |   |   |
    |   |   |   |   |   |   |   |   |   |   |   |   |   |
----'   `---'   `---'   `---'   `---'   `---'   `---'   `-------
class gpiozero.pins.pi.PiFactory[source]

Abstract base class representing hardware attached to a Raspberry Pi. This forms the base of LocalPiFactory.

close()[source]

Closes the pin factory. This is expected to clean up all resources manipulated by the factory. It it typically called at script termination.

pin(spec)[source]

Creates an instance of a Pin descendent representing the specified pin.

Warning

Descendents must ensure that pin instances representing the same hardware are identical; i.e. two separate invocations of pin() for the same pin specification must return the same object.

spi(**spi_args)[source]

Returns an SPI interface, for the specified SPI port and device, or for the specified pins (clock_pin, mosi_pin, miso_pin, and select_pin). Only one of the schemes can be used; attempting to mix port and device with pin numbers will raise SPIBadArgs.

If the pins specified match the hardware SPI pins (clock on GPIO11, MOSI on GPIO10, MISO on GPIO9, and chip select on GPIO8 or GPIO7), and the spidev module can be imported, a SPIHardwareInterface instance will be returned. Otherwise, a SPISoftwareInterface will be returned which will use simple bit-banging to communicate.

Both interfaces have the same API, support clock polarity and phase attributes, and can handle half and full duplex communications, but the hardware interface is significantly faster (though for many things this doesn’t matter).

class gpiozero.pins.pi.PiPin(factory, number)[source]

Abstract base class representing a multi-function GPIO pin attached to a Raspberry Pi. This overrides several methods in the abstract base Pin. Descendents must override the following methods:

  • _get_function()
  • _set_function()
  • _get_state()
  • _call_when_changed()
  • _enable_event_detect()
  • _disable_event_detect()

Descendents may additionally override the following methods, if applicable:

  • close()
  • output_with_state()
  • input_with_pull()
  • _set_state()
  • _get_frequency()
  • _set_frequency()
  • _get_pull()
  • _set_pull()
  • _get_bounce()
  • _set_bounce()
  • _get_edges()
  • _set_edges()
class gpiozero.pins.local.LocalPiFactory[source]

Abstract base class representing pins attached locally to a Pi. This forms the base class for local-only pin interfaces (RPiGPIOPin, RPIOPin, and NativePin).

class gpiozero.pins.local.LocalPiPin(factory, number)[source]

Abstract base class representing a multi-function GPIO pin attached to the local Raspberry Pi.

20.4. RPi.GPIO

class gpiozero.pins.rpigpio.RPiGPIOFactory[source]

Uses the RPi.GPIO library to interface to the Pi’s GPIO pins. This is the default pin implementation if the RPi.GPIO library is installed. Supports all features including PWM (via software).

Because this is the default pin implementation you can use it simply by specifying an integer number for the pin in most operations, e.g.:

from gpiozero import LED

led = LED(12)

However, you can also construct RPi.GPIO pins manually if you wish:

from gpiozero.pins.rpigpio import RPiGPIOFactory
from gpiozero import LED

factory = RPiGPIOFactory()
led = LED(12, pin_factory=factory)
class gpiozero.pins.rpigpio.RPiGPIOPin(factory, number)[source]

Pin implementation for the RPi.GPIO library. See RPiGPIOFactory for more information.

20.5. RPIO

class gpiozero.pins.rpio.RPIOFactory[source]

Uses the RPIO library to interface to the Pi’s GPIO pins. This is the default pin implementation if the RPi.GPIO library is not installed, but RPIO is. Supports all features including PWM (hardware via DMA).

Note

Please note that at the time of writing, RPIO is only compatible with Pi 1’s; the Raspberry Pi 2 Model B is not supported. Also note that root access is required so scripts must typically be run with sudo.

You can construct RPIO pins manually like so:

from gpiozero.pins.rpio import RPIOFactory
from gpiozero import LED

factory = RPIOFactory()
led = LED(12, pin_factory=factory)
class gpiozero.pins.rpio.RPIOPin(factory, number)[source]

Pin implementation for the RPIO library. See RPIOFactory for more information.

20.6. PiGPIO

class gpiozero.pins.pigpio.PiGPIOFactory(host='localhost', port=8888)[source]

Uses the pigpio library to interface to the Pi’s GPIO pins. The pigpio library relies on a daemon (pigpiod) to be running as root to provide access to the GPIO pins, and communicates with this daemon over a network socket.

While this does mean only the daemon itself should control the pins, the architecture does have several advantages:

  • Pins can be remote controlled from another machine (the other machine doesn’t even have to be a Raspberry Pi; it simply needs the pigpio client library installed on it)
  • The daemon supports hardware PWM via the DMA controller
  • Your script itself doesn’t require root privileges; it just needs to be able to communicate with the daemon

You can construct pigpio pins manually like so:

from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import LED

factory = PiGPIOFactory()
led = LED(12, pin_factory=factory)

This is particularly useful for controlling pins on a remote machine. To accomplish this simply specify the host (and optionally port) when constructing the pin:

from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import LED

factory = PiGPIOFactory(host='192.168.0.2')
led = LED(12, pin_factory=factory)

Note

In some circumstances, especially when playing with PWM, it does appear to be possible to get the daemon into “unusual” states. We would be most interested to hear any bug reports relating to this (it may be a bug in our pin implementation). A workaround for now is simply to restart the pigpiod daemon.

class gpiozero.pins.pigpio.PiGPIOPin(factory, number)[source]

Pin implementation for the pigpio library. See PiGPIOFactory for more information.

20.7. Native

class gpiozero.pins.native.NativeFactory[source]

Uses a built-in pure Python implementation to interface to the Pi’s GPIO pins. This is the default pin implementation if no third-party libraries are discovered.

Warning

This implementation does not currently support PWM. Attempting to use any class which requests PWM will raise an exception. This implementation is also experimental; we make no guarantees it will not eat your Pi for breakfast!

You can construct native pin instances manually like so:

from gpiozero.pins.native import NativeFactory
from gpiozero import LED

factory = NativeFactory()
led = LED(12, pin_factory=factory)
class gpiozero.pins.native.NativePin(factory, number)[source]

Native pin implementation. See NativeFactory for more information.

20.8. Mock

class gpiozero.pins.mock.MockFactory(revision='a02082', pin_class=<class 'gpiozero.pins.mock.MockPin'>)[source]

Factory for generating mock pins. The revision parameter specifies what revision of Pi the mock factory pretends to be (this affects the result of the pi_info attribute as well as where pull-ups are assumed to be). The pin_class attribute specifies which mock pin class will be generated by the pin() method by default. This can be changed after construction by modifying the pin_class attribute.

pin(spec, pin_class=None, **kwargs)[source]

The pin method for MockFactory additionally takes a pin_class attribute which can be used to override the class’ pin_class attribute. Any additional keyword arguments will be passed along to the pin constructor (useful with things like MockConnectedPin which expect to be constructed with another pin).

reset()[source]

Clears the pins and reservations sets. This is primarily useful in test suites to ensure the pin factory is back in a “clean” state before the next set of tests are run.

class gpiozero.pins.mock.MockPin(factory, number)[source]

A mock pin used primarily for testing. This class does not support PWM.

close()[source]

Cleans up the resources allocated to the pin. After this method is called, this Pin instance may no longer be used to query or control the pin’s state.

class gpiozero.pins.mock.MockPWMPin(factory, number)[source]

This derivative of MockPin adds PWM support.

class gpiozero.pins.mock.MockConnectedPin(factory, number, input_pin=None)[source]

This derivative of MockPin emulates a pin connected to another mock pin. This is used in the “real pins” portion of the test suite to check that one pin can influence another.

class gpiozero.pins.mock.MockChargingPin(factory, number, charge_time=0.01)[source]

This derivative of MockPin emulates a pin which, when set to input, waits a predetermined length of time and then drives itself high (as if attached to, e.g. a typical circuit using an LDR and a capacitor to time the charging rate).

class gpiozero.pins.mock.MockTriggerPin(factory, number, echo_pin=None, echo_time=0.04)[source]

This derivative of MockPin is intended to be used with another MockPin to emulate a distance sensor. Set echo_pin to the corresponding pin instance. When this pin is driven high it will trigger the echo pin to drive high for the echo time.