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.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.AnalogInandanalogio.AnalogOutclasses.-
direction:Direction¶ The direction of the pin.
Setting this will use the defaults from the corresponding
switch_to_input()orswitch_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
directionisOUTPUT.
-
deinit(self)¶ Turn off the DigitalInOut and release the pin for other use.
-
__enter__(self)¶ No-op used by Context Managers.
-
__exit__(self)¶ Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
-
switch_to_output(self, value: bool = False, drive_mode: digitalio.DriveMode = digitalio.DriveMode.PUSH_PULL)¶ Set the drive mode and value and then switch to writing out digital values.
-
switch_to_input(self, pull: 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)
-
-
class
digitalio.Direction¶ Defines the direction of a digital pin
-
INPUT:Any¶ Read digital data in
-
OUTPUT:Any¶ Write digital data out
-