DigitalInOut – digital input and output

A DigitalInOut is used to digitally control I/O pins. For analog control of a pin, see the analogio.AnalogIn and analogio.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

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


No-op used by Context Managers.


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.

  • value (bool) – default value to set upon switching
  • drive_mode (DriveMode) – drive mode for the output

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)
# Or, after switch_to_input
switch.pull = digitalio.Pull.UP

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.


The digital logic level of the pin.


The pin drive mode. One of:


The pin pull direction. One of:

Raises:AttributeError – if direction is OUTPUT.