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: :x:
micro:bit v1.x No pairing required Just works pairing
Windows :heavy_check_mark: :x:
Linux :heavy_check_mark: :heavy_check_mark:
MacOS :heavy_check_mark: * :x:

* Magnetometer calibration of micro:bit v1 on MacOS fails with the micro:bit not responding or going out of memory (error 020 + sad face)

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: The micro:bit Bluetooth troubleshooting guide (.docx word file download)

What is the name of my micro:bit?

See: How to find the name of your micro:bit

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.7.0.tar.gz (40.6 kB view details)

Uploaded Source

Built Distribution

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

kaspersmicrobit-0.7.0-py3-none-any.whl (49.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kaspersmicrobit-0.7.0.tar.gz
Algorithm Hash digest
SHA256 25bacdb1a90507092247046c705cd3d79ce2e37e60d41aff9570677c3ab90914
MD5 2107f473cc451518f92afd0b18aa5ecb
BLAKE2b-256 5f0ba04a30c331410e6f83c3e52747dbf8903ad5cdfaa9b13e9c62f951149339

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kaspersmicrobit-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f7f920f4d831b773feb87a84274f3c8d5a8db03fd683164befd6f40c5c304e5
MD5 68fff7cdd54d9bca7d3f2288af1a863e
BLAKE2b-256 52f288fedac375b2919d1a402d1bbcf397602c976cccdc7c82facc2d253300d1

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