Try touching the micro: bit with VS Code + Python

This is a memo of writing a micro: bit program in VS Code and Python. The feature of micro: bit is that it can be created in a visual programming environment, and it is doubtful if anyone wants to write it in Python. The usage environment is like this.

The explanation is based on the assumption that VS Code already contains Python extensions.

1. Install uflash

A tool for writing Python programs to the micro: bit.

$ pip install uflash

It seems that the VS Code extension that will be introduced later uses the "uflash" command, so make sure that it can be used properly.

$ uflash --version
uflash 1.3.0

It's OK if the version comes out.

2. Install VS Code extension

Put the extension corresponding to micro: bit in VS Code. Search for "micro: bit" in the extension search field and install the extension below. スクリーンショット 2021-01-15 20.00.39.png After installing, let's restart VS Code for the time being. This completes the construction of the environment.

3. Create Python file

Create a new directory in a suitable location and create a Python file in it. Here, the directory name is "microbit" and the Python file name is "main.py". Drag and drop the created directory to VS Code and open the Python file on VS Code. スクリーンショット 2021-01-15 20.09.24.png If the extension is installed properly, the button "Build current file to Micro: Bit" will appear in the upper right.

Now let's write the code for micro: bit.

from microbit import *

while True:
    if button_a.is_pressed():
        display.show(Image.HAPPY)
    else:
        display.show(Image.ANGRY)

4. Problems that complement does not work

Even so far, I can write to micro: bit at least, but the red line appears in the function for micro: bit on VS Code and the completion does not work yet. To make the completion work, open the VS Code command palette (Cmd + Shift + P) and execute the following command.

Fetch micro:bit modules

Immediately after executing the command, the red line does not disappear yet, but once the source file is saved, it will be reflected and the red line will disappear.

5. Write to micro: bit

When you connect the micro: bit to your PC and press the "Build current file to Micro: Bit" button, the orange LED on the micro: bit will start blinking, and when it stops blinking, writing is complete. After the writing is completed, if you press or release the A button on the main unit, the facial expression displayed on the 5x5 LED matrix will change.

Digression

The micro: bit has a built-in LED/button/sensor as a single unit, and wireless communication is possible if multiple units are prepared, so it seems that you can make interesting things depending on your ingenuity. I had a hands-on experience at a university festival, and I had the impression that even elementary school students who had no programming experience could enjoy using it using the visual programming environment MakeCode. Even if you use Python, the Japanese documentation is solid, so it's relatively easy to get started. Considering that programming materials for children cost more than 10,000 yen, micro: bit, which supports both visual development environment and text development environment as well as being able to buy the main body for around 3000 yen, seems to be a powerful option. ..

References

Recommended Posts

Try touching the micro: bit with VS Code + Python
Python with VS Code (Windows 10)
Debug Python with VS Code
Try running Jupyter with VS Code
Install python with mac vs code
The story that Python stopped working with VS Code (Windows 10)
Try to solve the man-machine chart with Python
Python (Windows 10) Virtual Environment / Package with VS Code
Debug with VS Code using boost python numpy
Try CIing the pushed python code on GitHub.
Use Python in Anaconda environment with VS Code
Convert the character code of the file with Python3
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
Try scraping with Python.
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
Try translating with Python while maintaining the PDF layout
Try to solve the internship assignment problem with Python
Allow real-time code checking in Python development with VS Code
VS Code settings for developing in Python with completion
Revive symbol search in Python workspace with VS Code
Get country code with python
Try Python output with Haxe 3.2
Full bit search with Python
Try running Python with Try Jupyter
Check the code with flake8
Call the API with python3.
Try face recognition with Python
Document Python code with Doxygen
Try scraping the data of COVID-19 in Tokyo with Python
Try hitting the Twitter API quickly and easily with Python
Settings when developing App Engine / Python apps with VS Code
Let's run jupyter natively supported by VS Code with python3.8
Try Fortran with VS Code up to debug settings. [Win10]
Settings to debug the contents of the library with VS Code
Try to automate the operation of network devices with Python
System trade starting with Python3: Get the latest program code
Try to decipher the garbled attachment file name with Python
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
[No venv required] The strongest Python development environment created with Remote Containers [VS Code / Docker]
Decrypt the QR code with CNN
Try scraping with Python + Beautiful Soup
Extract the xz file with python
Try the Python LINE Pay SDK
[Cloudian # 6] Try deleting the object stored in the bucket with Python (boto3)
Try to operate Facebook with Python
Learn search with Python # 2bit search, permutation search
Try singular value decomposition with Python
Get the weather with Python requests
First python ② Try to write code while examining the features of python
Try to create a python environment with Visual Studio Code & WSL
Get the weather with Python requests 2
Find the Levenshtein Distance with python
Hit the Etherpad-lite API with Python
Install the Python plugin with Netbeans 8.0.2
Build mlpy with python3.3 (64bit) (windows 64bit)
Try face recognition with python + OpenCV
I liked the tweet with python. ..
Let's summarize the degree of coupling between modules with Python code
Master the type with Python [Python 3.9 compatible]