0
# Digital Input/Output
1
2
GPIO pin control with direction configuration, pull resistors, and drive modes. Provides CircuitPython-compatible digital I/O operations with automatic resource management and context manager support.
3
4
## Capabilities
5
6
### Digital Pin Control
7
8
The DigitalInOut class provides comprehensive GPIO pin control with CircuitPython-compatible behavior. Supports automatic cleanup through context managers and proper resource management.
9
10
```python { .api }
11
class DigitalInOut(ContextManaged):
12
def __init__(self, pin):
13
"""
14
Initialize digital I/O pin.
15
16
Args:
17
pin: Board pin object (from board module)
18
"""
19
20
@property
21
def direction(self) -> Direction:
22
"""Get or set pin direction (INPUT or OUTPUT)"""
23
24
@direction.setter
25
def direction(self, value: Direction) -> None:
26
"""Set pin direction"""
27
28
@property
29
def value(self) -> bool:
30
"""
31
Get or set pin logic level.
32
33
Returns:
34
bool: True for HIGH (3.3V/5V), False for LOW (0V)
35
"""
36
37
@value.setter
38
def value(self, val: bool) -> None:
39
"""Set pin logic level"""
40
41
@property
42
def pull(self) -> Pull:
43
"""Get or set pull resistor configuration"""
44
45
@pull.setter
46
def pull(self, val: Pull) -> None:
47
"""Configure pull resistor (UP, DOWN, or None)"""
48
49
@property
50
def drive_mode(self) -> DriveMode:
51
"""Get or set output drive mode"""
52
53
@drive_mode.setter
54
def drive_mode(self, val: DriveMode) -> None:
55
"""Set output drive mode (PUSH_PULL or OPEN_DRAIN)"""
56
57
def switch_to_output(self, value: bool = False, drive_mode: DriveMode = DriveMode.PUSH_PULL) -> None:
58
"""
59
Switch pin to output mode with initial value and drive mode.
60
61
Args:
62
value: Initial output value (default False)
63
drive_mode: Output drive mode (default PUSH_PULL)
64
"""
65
66
def switch_to_input(self, pull: Pull = None) -> None:
67
"""
68
Switch pin to input mode with optional pull resistor.
69
70
Args:
71
pull: Pull resistor configuration (UP, DOWN, or None)
72
"""
73
74
def deinit(self) -> None:
75
"""Release pin resources"""
76
```
77
78
### Direction Constants
79
80
Pin direction configuration constants for input and output modes.
81
82
```python { .api }
83
class Direction:
84
INPUT: Direction # Configure pin as input
85
OUTPUT: Direction # Configure pin as output
86
```
87
88
### Pull Resistor Constants
89
90
Pull resistor configuration constants for input pins.
91
92
```python { .api }
93
class Pull:
94
UP: Pull # Enable internal pull-up resistor
95
DOWN: Pull # Enable internal pull-down resistor
96
```
97
98
### Drive Mode Constants
99
100
Output drive mode constants for output pins.
101
102
```python { .api }
103
class DriveMode:
104
PUSH_PULL: DriveMode # Standard push-pull output (default)
105
OPEN_DRAIN: DriveMode # Open-drain output (requires external pull-up)
106
```
107
108
## Usage Examples
109
110
### Basic LED Control
111
112
```python
113
import board
114
import digitalio
115
import time
116
117
# Setup LED on pin D18
118
led = digitalio.DigitalInOut(board.D18)
119
led.direction = digitalio.Direction.OUTPUT
120
121
# Blink LED
122
for i in range(10):
123
led.value = True # Turn on
124
time.sleep(0.5)
125
led.value = False # Turn off
126
time.sleep(0.5)
127
128
# Cleanup
129
led.deinit()
130
```
131
132
### Reading Button Input
133
134
```python
135
import board
136
import digitalio
137
138
# Setup button with pull-up resistor
139
button = digitalio.DigitalInOut(board.D2)
140
button.direction = digitalio.Direction.INPUT
141
button.pull = digitalio.Pull.UP
142
143
# Read button state (False when pressed due to pull-up)
144
if not button.value:
145
print("Button pressed!")
146
147
# Using context manager for automatic cleanup
148
with digitalio.DigitalInOut(board.D2) as button:
149
button.switch_to_input(pull=digitalio.Pull.UP)
150
print(f"Button state: {button.value}")
151
```
152
153
### Open-Drain Output
154
155
```python
156
import board
157
import digitalio
158
159
# Setup open-drain output (for I2C-style communication)
160
signal = digitalio.DigitalInOut(board.D5)
161
signal.switch_to_output(value=True, drive_mode=digitalio.DriveMode.OPEN_DRAIN)
162
163
# Signal communication
164
signal.value = False # Pull line low
165
time.sleep(0.001)
166
signal.value = True # Release line (pulled high by external resistor)
167
```
168
169
### Multiple Pin Control
170
171
```python
172
import board
173
import digitalio
174
175
# Setup multiple LEDs
176
leds = []
177
for pin in [board.D18, board.D19, board.D20, board.D21]:
178
led = digitalio.DigitalInOut(pin)
179
led.direction = digitalio.Direction.OUTPUT
180
leds.append(led)
181
182
# Create chase pattern
183
for i in range(10):
184
for j, led in enumerate(leds):
185
led.value = (i % len(leds)) == j
186
time.sleep(0.2)
187
188
# Cleanup all LEDs
189
for led in leds:
190
led.deinit()
191
```