Skip to main content

A python package to connect to the Bluetooth LE GATT services of BBC micro:bit devices. Use your micro:bit as a wireless game controller!

Project description

Kasper's micro:bit

So you finished Python for kids? Wouldn't it be awesome to use your BBC micro:bit as a wireless game controller for your own DIY games? You can do it with this python package! Pair your micro:bit to your computer with bluetooth and use buttons A and B or the accelerometer to control your game. Like it? Give us a :star: on github!

Lint and Test Documentation Status PyPi Nederlands

Kasper's microbit is a python package to make a connection to a BBC micro:bit by means of the Bluetooth LE GATT services exposed by the micro:bit.

Video of games created with kaspersmicrobit

Watch the full video on youtube

Getting started

Install kaspersmicrobit:

$ pip install kaspersmicrobit

Copy this hex file to the micro:bit and run your first program:

import time

from kaspersmicrobit import KaspersMicrobit


def pressed(button):
    print(f"button {button} pressed")

with KaspersMicrobit.find_one_microbit() as microbit:
    microbit.buttons.on_button_a(press=pressed)
    time.sleep(10)

Learn more

Visit https://kaspersmicrobit.readthedocs.io:

Or take a look at the examples directory.

Micro:bit versions, operating systems, Bluetooth pairing

Below you can find which combinations of operating systems and microbit versions have been known to work.

micro:bit v2.x No pairing required Just works pairing
Windows :heavy_check_mark: :heavy_check_mark:
Linux :heavy_check_mark: :heavy_check_mark:
MacOS :heavy_check_mark: :grey_question:

I don't have a mac to test kaspersmicrobit on. Let me know here (#5) if it works or not! Thanks @RyanNorge for testing a micro:bit v2.x on MacOS in "No pairing required" mode

micro:bit v1.x No pairing required Just works pairing
Windows :heavy_check_mark: :x:
Linux :heavy_check_mark: :heavy_check_mark:
MacOS :grey_question: :grey_question:

Troubleshooting

Upgrade to the latest version

$  pip install --upgrade kaspersmicrobit  

Bluetooth connection

First try turning the micro:bit off and on again.

If you are not using the "with"-block, but calling .connect() yourself, always make sure that in any case you call .disconnect() when you don't need the connection anymore (for instance when you exit your application)

No pairing required

If the hex file was created with the setting "No pairing required" then the micro:bit should not be paired with the operating system

Just works pairing

Don't use pairing with a micro:bit v1 on windows, use "No pairing required" instead.

For other versions: try to remove the micro:bit from the paired Bluetooth devices and pairing it your computer again.

See also: https://support.microbit.org/helpdesk/attachments/19075694226

The micro:bit shows a sad face and error 020

This means the micro:bit is out of memory. You probably have enabled too many Bluetooth services in MakeCode. Or maybe your MakeCode program is too large. Because the micro:bit v1 has less memory than the v2, this has a higher chance to occur on v1 micro:bits. See also: the micro:bit error codes

tkinter "main thread is not in main loop"

When combining kaspersmicrobit with tkinter (the window library used in Python for kids) you could bump into the TK error "main thread is not in main loop". This is probably because you call TK code from within a callback function that you registered to be called when a button press occurs or new accelerometer data is present (or some other notification). The callback is executed on a different thread and tkinter does not like this. There are at least 2 solutions for this:

  • don't call tkinter code in a callback
  • wrap the callback with kaspersmicrobit.tkinter.do_in_tkinter(tk, your_callback) this makes sure that your callback will be executed on the tk thread, avoiding the error

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

kaspersmicrobit-0.5.0.tar.gz (40.6 kB view details)

Uploaded Source

Built Distribution

kaspersmicrobit-0.5.0-py3-none-any.whl (49.7 kB view details)

Uploaded Python 3

File details

Details for the file kaspersmicrobit-0.5.0.tar.gz.

File metadata

  • Download URL: kaspersmicrobit-0.5.0.tar.gz
  • Upload date:
  • Size: 40.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for kaspersmicrobit-0.5.0.tar.gz
Algorithm Hash digest
SHA256 789745a8988d1ab7f63cd5b6c42108b04a17edf73dd2ff7fd4e3323fb6a467d7
MD5 d560f9b9fa203a52aa2314a50739722d
BLAKE2b-256 f87c58606fbc1bb48700c2e2f0ae0564f82b007056e0ac4c085087809c99b120

See more details on using hashes here.

File details

Details for the file kaspersmicrobit-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kaspersmicrobit-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 036ae0dc60fdf77903a2959022a048bcc19e1df9941ffc96322d2fac91fcbb3b
MD5 86e14e935b116b032402bc4095f45a4a
BLAKE2b-256 cce37d7bc4891089ef40c3aa4c7988bd4d99b3aa1be7c7c2cbd0df99e88e47e1

See more details on using hashes here.

Supported by

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