Skip to main content

A smart serial port detector for BBC micro:bit

Project description

Micro:bit Detector

A lightweight Python utility to automatically detect and select BBC micro:bit devices connected via USB.

It handles cross-platform port detection (Windows/Mac/Linux) and robustly manages scenarios where multiple micro:bits are connected simultaneously.

Features

  • Auto-Detection: Automatically finds the correct serial port (COM3, /dev/ttyACM0, etc.).
  • Multi-Device Support: Can detect multiple connected micro:bits.
  • Smart Selection:
    • If 1 device is found, it selects it automatically.
    • If multiple are found, it can prompt the user to choose one.
  • Metadata: Retrieves Serial Numbers to distinguish between identical devices.

Installation

pip install mb_detect

Usage

1. The Easiest Way (Single micro:bit)

If you only have one micro:bit plugged in, the tool will auto-select it and return the port string directly.

import mb_detect
import serial

# Returns the port name as a string (e.g., "COM3")
port = mb_detect.find()

if port:
    print(f"Connected to {port}") 
    # Example output: Connected to COM3
    
    # You can pass the variable directly to pyserial
    ser = serial.Serial(port, 115200)
else:
    print("No micro:bit found.")

2. Handling Multiple micro:bits (Interactive)

If you have multiple devices connected, the find() function will automatically pause the script and ask the user to choose one via the terminal input.

# If 2 micro:bits are plugged in, this will print:
#    ⚠️  Found 2 micro:bits:
#    [0] Port: COM3 | Serial: 9900...
#    [1] Port: COM4 | Serial: 9901...
#
#    Select device number (0-9): 

port = mb_detect.find()
print(f"User selected: {port}")

3. Non-Interactive Mode (Automation)

If you are running a script in the background (e.g., a robot or server) and cannot accept user input, use interactive=False. It will default to the first available micro:bit found.

# Will not ask for input; just picks the first one found
port = mb_detect.find(interactive=False)

4. Advanced: Get All Data

If you need the Serial Number or want to connect to all micro:bits at once, use the scan() function. This returns the full data (not just the string).

all_devices = mb_detect.scan()

# Returns a list of dictionaries:
# [
#   {'port': 'COM3', 'serial_number': '99000...', 'description': 'mbed Serial Port'},
#   {'port': 'COM4', 'serial_number': '99001...', 'description': 'mbed Serial Port'}
# ]

for dev in all_devices:
    print(f"Found device at {dev['port']} with serial {dev['serial_number']}")

Requirements

  • Python 3.6+
  • pyserial

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mb_detect-1.0.3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mb_detect-1.0.3-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file mb_detect-1.0.3.tar.gz.

File metadata

  • Download URL: mb_detect-1.0.3.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mb_detect-1.0.3.tar.gz
Algorithm Hash digest
SHA256 b4e9199aea8e7d2b7e126866368a15b35444424df9b8e7237bf484985422235c
MD5 7d4aa3086ca7ed94650872c4bf4012aa
BLAKE2b-256 b21eae5091924d59436873c33b7360979db8a95e9caebff9bfd5e7fd62d12b43

See more details on using hashes here.

File details

Details for the file mb_detect-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: mb_detect-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mb_detect-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3d41d532ad2e1b7a4a1c034d0b34aa54a74e705ddb5ff47c0da12c5bac382736
MD5 507140757db755e2de99299259c8170d
BLAKE2b-256 b4a0015a07ce385c92b2db76116e85725d6e0e3c01a6fe66037cf2fd471e5247

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page