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
import board
pin = digitalio.DigitalInOut(board.LED)
print(pin.value)
This example will initialize the the device, read
value
and then
deinit()
the hardware.
Here is blinky:
import time
import digitalio
import board
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
while True:
led.value = True
time.sleep(0.1)
led.value = False
time.sleep(0.1)
Available on these boards
- 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
andanalogio.AnalogOut
classes.Create a new DigitalInOut object associated with the pin. Defaults to input with no pull. Use
switch_to_input()
andswitch_to_output()
to change the direction.- Paramètres:
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()
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:
- Lève:
AttributeError – if
direction
isOUTPUT
.
- __enter__() DigitalInOut ¶
No-op used by Context Managers.
- __exit__() None ¶
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
- switch_to_output(value: bool = False, drive_mode: DriveMode = DriveMode.PUSH_PULL) None ¶
Set the drive mode and value and then switch to writing out digital values.
- switch_to_input(pull: Pull | None = None) None ¶
Set the pull and then switch to read in digital values.
- Paramètres:
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.