2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

micro:bit用の Pythonエディターでシリアル通信(改行ありのシンプルな書き込み)【micro:bit】

Last updated at Posted at 2025-12-14

(この記事は micro:bit の Advent Calendar 2025 の記事です)

今回の内容とお試し

今回の内容は、以下の micro:bit用の Pythonエディターの話です。

●プログラムしよう | micro:bit
 https://microbit.org/ja/code/

2025-12-14_21-58-14.jpg

Pythonエディターでのシンプルなシリアル通信、具体的には「改行ありのシンプルな書き込み」を試してみます。

Pythonエディターを使う

Pythonエディターは、以下からアクセスできます。

●micro:bit Python Editor
 https://python.microbit.org/v/3

アクセスすると、デフォルトでは以下の LED を使った表示系のコードが表示されます。

# Imports go at the top
from microbit import *


# Code in a 'while True:' loop repeats forever
while True:
    display.show(Image.HEART)
    sleep(1000)
    display.scroll('Hello')

シリアル通信での改行ありの書き込み

以下の uart で、シリアル通信での改行ありの書き込みを試します。

●micro:bit Python Editor
 https://python.microbit.org/v/3/api/microbit.uart

2025-12-14_22-03-49.jpg

コードでは、下記の write(buf) を使います。

2025-12-14_22-04-43.jpg

具体的なコードは、以下のとおりです。

from microbit import *

uart.init(baudrate=115200)

while True:
    uart.write("test\r\n")
    sleep(1000)

書き込む文字列は、「test」と「改行文字の \r\n 」です。これを、1秒ごとに書き込む処理にしてみました。

動作確認

最後に、動作確認です。受信側は、前に以下の記事に書いた Web Serial API のサンプルを使います。

●Web Serial API を最短の手順で試したくてやったこと(micro:bit を利用) - Qiita
 https://qiita.com/youtoy/items/a0071a6d2ef7f6930d33

サンプルは、以下のページで使えます。

●googlechromelabs.github.io/serial-terminal/
 https://googlechromelabs.github.io/serial-terminal/

2025-12-14_22-09-04.jpg

上記のページで micro:bit に接続したところ、以下のように意図した内容を受信できていることが確認できました。

2025-12-14_21-55-51.jpg

その他の内容(メモ)

以下が、micro:bit用の Pythonエディター公式ガイドになるようです。後で見てみようと思って、ひとまずメモしておきます。

●Python editor | micro:bit
 https://microbit.org/get-started/user-guide/python-editor/

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?