DigitalInOut – digital input and output

A DigitalInOut is used to digitally control I/O pins. For analog control of a pin, see the AnalogIn and AnalogOut classes.

class digitalio.DigitalInOut(pin)

Create a new DigitalInOut object associated with the pin. Defaults to input with no pull. Use switch_to_input() and switch_to_output() to change the direction.

Parameters:pin (Pin) – The pin to control
deinit()

Turn off the DigitalInOut and release the pin for other use.

__enter__()

No-op used by Context Managers.

__exit__()

Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.

switch_to_output(value=False, drive_mode=digitalio.DriveMode.PUSH_PULL)

Set the drive mode and value and then switch to writing out digital values.

Parameters:
  • value (bool) – default value to set upon switching
  • drive_mode (DriveMode) – drive mode for the output
switch_to_input(pull=None)

Set the pull and then switch to read in digital values.

Parameters:pull (Pull) – pull configuration for the input

Example usage:

import digitalio
import board

switch = digitalio.DigitalInOut(board.SLIDE_SWITCH)
switch.switch_to_input(pull=digitalio.Pull.UP)
# Or, after switch_to_input
switch.pull = digitalio.Pull.UP
print(switch.value)
direction

The direction of the pin.

Setting this will use the defaults from the corresponding switch_to_input() or switch_to_output() method. If you want to set pull, value or drive mode prior to switching, then use those methods instead.

value

The digital logic level of the pin.

drive_mode

The pin drive mode. One of:

pull

The pin pull direction. One of:

Raises:AttributeError – if direction is OUTPUT.