CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-adafruit-blinka

CircuitPython APIs for non-CircuitPython versions of Python such as CPython on Linux and MicroPython.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

digital-io.mddocs/

Digital Input/Output

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.

Capabilities

Digital Pin Control

The DigitalInOut class provides comprehensive GPIO pin control with CircuitPython-compatible behavior. Supports automatic cleanup through context managers and proper resource management.

class DigitalInOut(ContextManaged):
    def __init__(self, pin):
        """
        Initialize digital I/O pin.
        
        Args:
            pin: Board pin object (from board module)
        """
    
    @property
    def direction(self) -> Direction:
        """Get or set pin direction (INPUT or OUTPUT)"""
    
    @direction.setter
    def direction(self, value: Direction) -> None:
        """Set pin direction"""
    
    @property
    def value(self) -> bool:
        """
        Get or set pin logic level.
        
        Returns:
            bool: True for HIGH (3.3V/5V), False for LOW (0V)
        """
    
    @value.setter
    def value(self, val: bool) -> None:
        """Set pin logic level"""
    
    @property
    def pull(self) -> Pull:
        """Get or set pull resistor configuration"""
    
    @pull.setter
    def pull(self, val: Pull) -> None:
        """Configure pull resistor (UP, DOWN, or None)"""
    
    @property
    def drive_mode(self) -> DriveMode:
        """Get or set output drive mode"""
    
    @drive_mode.setter
    def drive_mode(self, val: DriveMode) -> None:
        """Set output drive mode (PUSH_PULL or OPEN_DRAIN)"""
    
    def switch_to_output(self, value: bool = False, drive_mode: DriveMode = DriveMode.PUSH_PULL) -> None:
        """
        Switch pin to output mode with initial value and drive mode.
        
        Args:
            value: Initial output value (default False)
            drive_mode: Output drive mode (default PUSH_PULL)
        """
    
    def switch_to_input(self, pull: Pull = None) -> None:
        """
        Switch pin to input mode with optional pull resistor.
        
        Args:
            pull: Pull resistor configuration (UP, DOWN, or None)
        """
    
    def deinit(self) -> None:
        """Release pin resources"""

Direction Constants

Pin direction configuration constants for input and output modes.

class Direction:
    INPUT: Direction    # Configure pin as input
    OUTPUT: Direction   # Configure pin as output

Pull Resistor Constants

Pull resistor configuration constants for input pins.

class Pull:
    UP: Pull     # Enable internal pull-up resistor
    DOWN: Pull   # Enable internal pull-down resistor

Drive Mode Constants

Output drive mode constants for output pins.

class DriveMode:
    PUSH_PULL: DriveMode    # Standard push-pull output (default)
    OPEN_DRAIN: DriveMode   # Open-drain output (requires external pull-up)

Usage Examples

Basic LED Control

import board
import digitalio
import time

# Setup LED on pin D18
led = digitalio.DigitalInOut(board.D18)
led.direction = digitalio.Direction.OUTPUT

# Blink LED
for i in range(10):
    led.value = True   # Turn on
    time.sleep(0.5)
    led.value = False  # Turn off
    time.sleep(0.5)

# Cleanup
led.deinit()

Reading Button Input

import board
import digitalio

# Setup button with pull-up resistor
button = digitalio.DigitalInOut(board.D2)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP

# Read button state (False when pressed due to pull-up)
if not button.value:
    print("Button pressed!")

# Using context manager for automatic cleanup
with digitalio.DigitalInOut(board.D2) as button:
    button.switch_to_input(pull=digitalio.Pull.UP)
    print(f"Button state: {button.value}")

Open-Drain Output

import board
import digitalio

# Setup open-drain output (for I2C-style communication)
signal = digitalio.DigitalInOut(board.D5)
signal.switch_to_output(value=True, drive_mode=digitalio.DriveMode.OPEN_DRAIN)

# Signal communication
signal.value = False  # Pull line low
time.sleep(0.001)
signal.value = True   # Release line (pulled high by external resistor)

Multiple Pin Control

import board
import digitalio

# Setup multiple LEDs
leds = []
for pin in [board.D18, board.D19, board.D20, board.D21]:
    led = digitalio.DigitalInOut(pin)
    led.direction = digitalio.Direction.OUTPUT
    leds.append(led)

# Create chase pattern
for i in range(10):
    for j, led in enumerate(leds):
        led.value = (i % len(leds)) == j
    time.sleep(0.2)

# Cleanup all LEDs
for led in leds:
    led.deinit()

Install with Tessl CLI

npx tessl i tessl/pypi-adafruit-blinka

docs

analog-io.md

bitbangio.md

board-pins.md

communication.md

core-framework.md

digital-io.md

index.md

peripherals.md

pwm-pulse.md

utilities.md

tile.json