digitalio – Basic digital pin support

The digitalio module contains classes to provide access to basic digital IO.

All classes change hardware state and should be deinitialized when they are no longer needed if the program continues after use. To do so, either call deinit() or use a context manager. See Lifetime and ContextManagers for more info.

For example:

import digitalio
from board import *

pin = digitalio.DigitalInOut(D13)
print(pin.value)

This example will initialize the the device, read value and then deinit() the hardware.

Here is blinky:

import digitalio
from board import *
import time

led = digitalio.DigitalInOut(D13)
led.direction = digitalio.Direction.OUTPUT
while True:
    led.value = True
    time.sleep(0.1)
    led.value = False
    time.sleep(0.1)

class digitalio.DriveMode

Defines the drive mode of a digital pin

Enum-like class to define the drive mode used when outputting digital values.

PUSH_PULL :DriveMode

Output both high and low digital values

OPEN_DRAIN :DriveMode

Output low digital values but go into high z for digital high. This is useful for i2c and other protocols that share a digital line.

class digitalio.DigitalInOut(pin: microcontroller.Pin)

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.

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

direction :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 :bool

The digital logic level of the pin.

drive_mode :DriveMode

The pin drive mode. One of:

pull :Optional[Pull]

The pin pull direction. One of:

Raises

AttributeError – if direction is OUTPUT.

deinit(self)None

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

__enter__(self) → DigitalInOut

No-op used by Context Managers.

__exit__(self)None

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

switch_to_output(self, value: bool = False, drive_mode: DriveMode = DriveMode.PUSH_PULL)None

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(self, pull: Optional[Pull] = None)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)
class digitalio.Direction

Defines the direction of a digital pin

Enum-like class to define which direction the digital values are going.

INPUT :Direction

Read digital data in

OUTPUT :Direction

Write digital data out

class digitalio.Pull

Defines the pull of a digital input pin

Enum-like class to define the pull value, if any, used while reading digital values in.

UP :Pull

When the input line isn’t being driven the pull up can pull the state of the line high so it reads as true.

DOWN :Pull

When the input line isn’t being driven the pull down can pull the state of the line low so it reads as false.