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), robustly manages multiple devices, and can automatically create the serial connection for you.
Features
- Auto-Detection: Automatically finds the correct serial port (
COM3,/dev/ttyACM0, etc.) using hardware IDs (VID 3368). - Direct Connection: Can return a ready-to-use
serial.Serialobject in one line. - 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 Recommended Way (Direct Connect)
You can find and connect to the micro:bit in a single line. This returns a standard serial.Serial object, so you can use all standard PySerial methods (write, readline, flush, etc.) immediately.
You can also pass standard PySerial arguments (like timeout) directly to this function.
import mb_detect
import time
# Finds the micro:bit and connects with a 2-second timeout
ser = mb_detect.connect(timeout=2)
if ser:
print(f"✅ Connected to {ser.port}")
# You can now use standard serial methods
ser.write(b'hello\n')
response = ser.readline().decode().strip()
print(f"Received: {response}")
ser.close()
else:
print("❌ No micro:bit found or device is busy.")
2. Get Port String Only (Manual Connection)
If you only want the port name (e.g., "COM3") but want to handle the connection logic yourself, use find().
import mb_detect
import serial
# Returns the port name as a string (e.g., "COM3")
port = mb_detect.find()
if port:
print(f"Found micro:bit at {port}")
# You can pass this string to pyserial manually
ser = serial.Serial(port, 115200)
3. Handling Multiple micro:bits (Interactive)
If you have multiple devices connected, both find() and connect() will automatically pause the script and ask the user to choose one via the terminal.
# 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):
ser = mb_detect.connect()
print(f"User selected: {ser.port}")
4. 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 connects to the first one found
ser = mb_detect.connect(interactive=False)
5. 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 list.
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']}")
6. Handling a Fleet (Multiple Connections)
If you need to connect to multiple micro:bits simultaneously (e.g., a swarm of robots), use connect_multiple(). This returns a list of connected serial objects.
import mb_detect
# Connect to ALL connected micro:bits
# IMPORTANT: Use a timeout so reading doesn't block the loop
fleet = mb_detect.connect_multiple(timeout=0.1)
print(f"Connected to {len(fleet)} devices.")
# Loop through them to read data
for ser in fleet:
if ser.in_waiting:
print(f"[{ser.port}] says: {ser.readline().decode().strip()}")
# Close all
for ser in fleet:
ser.close()
Requirements
- Python 3.6+
- pyserial
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mb_detect-1.0.4.tar.gz.
File metadata
- Download URL: mb_detect-1.0.4.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f2bafba5f2dc6f9dde0a641a3b20399e701283aeba8d41004b6db91a4589866
|
|
| MD5 |
54f2004a7d8bc1ed03c082395e45ad2d
|
|
| BLAKE2b-256 |
4cfcd49b4470bd2e69696537f5d45a60a6372b554c066ebb2e2841da83fdde5a
|
File details
Details for the file mb_detect-1.0.4-py3-none-any.whl.
File metadata
- Download URL: mb_detect-1.0.4-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d50eda621b217a40a6a1af4fede38364adfe954fab96803de4151ae0a4cfa054
|
|
| MD5 |
962658011ea5f423174ac2af4bc66b3e
|
|
| BLAKE2b-256 |
31415e5b0421bf973cc114759501f27fcf7c8218eaedbd3aa93e750bef7df6cd
|