Skip to content

Rvouill/microbitpy-sandbox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🧪 Micro:bit Serial Test Integration

This README documents how to test basic serial communication between a micro:bit board and the Python dice-game project. The goal is to verify that button A and B presses on the micro:bit send "PING" and "PONG" signals that are received correctly in Python.


🔌 Hardware Required

  • 1x BBC micro:bit
  • 1x USB cable (data-compatible)
  • Host machine (macOS, Linux or Windows)

🔧 Setup Steps

📦 1. Create a Python virtual environment

python3 -m venv .venv
source .venv/bin/activate
# deactivate when done
deactivate

📥 2. Install dependencies

We need pyserial to read from the micro:bit and Pillow for GUI (used elsewhere in the project):

pip3 install pyserial Pillow
# verify install
python -c "import serial, PIL; print('pyserial and pillow are installed')"

💾 3. Flash the micro:bit

Drag the microbit-test.hex file into the device volume, or use (MakeCode editor) with the following code

def on_forever():
    basic.show_icon(IconNames.HAPPY)
    if input.button_is_pressed(Button.A):
        serial.write_line("PING")
    if input.button_is_pressed(Button.B):
        serial.write_line("PONG")
basic.forever(on_forever)

Upload it to your micro:bit by dragging the .hex file into the device volume, or use MakeCode editor at https://makecode.microbit.org/

🖥️ 4. Python test script: microbit-test.py

with serial.Serial('/dev/tty.usbmodem111402', 115200, timeout=1) as ser:
    print("Listening for micro:bit PING…")
    while True:
        line = ser.readline().decode('utf-8').strip()
        if line == "PING":
            print("🅰 PING received from micro:bit!")
        if line == "PONG":
            print("🅱 PONG received from micro:bit!")

⚠️ Replace /dev/tty.usbmodem111402 with your actual serial port if needed.

🧪 5. Run the test

  • Plug in your micro:bit

  • Activate the virtual environment:

    source .venv/bin/activate
  • Run the Python script:

    python3 microbit-test.py
  • On Micro:bit Press Button A → You should see:

    🅰 PING received from micro:bit!
  • On Micro:bit Press Button B → You should see:

    🅱 PONG received from micro:bit!

🛠️ Troubleshooting

  • Use ls /dev/tty.* to find your micro:bit port on macOS
  • On Windows, try COM3, COM4, etc.
  • Ensure the .hex file is correctly flashed and includes serial.write_line

About

A sandbox for interfacing micro:bit + python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages