micro:bit CreateAI を使用して、手をたたくとライトが点灯し、手を振るとライトが消える AI ライト スイッチを作成します。
やさしいプロジェクトガイド
ステップ 1: 理解する
どんな仕組み?
このプロジェクトでは、拍手や手を振っていることを認識する機械学習 (ML) モデルをトレーニングします。
このモデルを、拍手するたびに「lightsOn」無線信号を別の micro:bit に送信し、手を振るたびに「lightsOff」無線信号を送信する MakeCode プログラムと組み合わせます。
受信側のmicro:bitが「lightsOn」信号を受信すると、すべてのLEDが点灯します。 「lightsOff」信号を受信すると、LEDディスプレイがクリアされます。
機械学習とは何ですか?
機械学習 (ML) は、コンピュータがデータに基づいて学習し、意思決定できる人工知能 (AI)の一種です。
ML モデルは、たとえば micro:bit をさまざまな方法で動かしたときに、さまざまな「アクション」を認識するなど、決定を下すのに役立つよう人がトレーニングします。
必要なこと
AIシステムは、人間が設計し、組み立て、テストしてから使う必要があります。 You'll collect data to train an ML model, test it, improve it, and combine it with computer code to make a smart device that uses AI. これを行うには、micro:bit と micro:bit CreateAI のWebサイトを使います。
ステップ2: プログラムする
必要なもの
- Two micro:bit V2s, USB cable, one or two battery packs
- ChromeまたはEdgeウェブブラウザを使用して、micro:bit作成AIのウェブサイトにアクセスできるコンピューター(デスクトップ、ラップトップ、またはChromebookなど)
- コンピュータにBluetoothが搭載されていない場合は、micro:bit V2が別途必要になります。
- A strap and holder, or another way to attach the micro:bit to you (e.g. flexible craft stems or elastic bands)
- micro:bit CreateAIを指導するヒントも役立つかもしれません
データサンプルの収集
When you open the project in micro:bit CreateAI, you’ll see we’ve given you some data samples for ‘waving’, 'clapping’ and ‘still’ actions:
waving
clapping
still
You can add your own movement samples using the micro:bit's movement sensor or accelerometer.
micro:bit CreateAI で、「接続」(Connect)ボタンをクリックして、データ収集用micro:bitを接続して指示にしたがいます。

Attach the micro:bit to your wrist. Click on the first action, ‘waving’, and click 'Record'. 間違えた場合は、不要なサンプルを削除できます。 micro:bit のボタン B を押して記録を開始することもできます。
If you would like to record continuously for 10 seconds to get 10 samples, click on the three dots next to the record button and select that option.
Now record your own data samples for the ‘clapping’ and 'still' actions, making sure for 'still' that you collect samples in different positions, such as facing up and down.
モデルのトレーニングとテスト
Click the ‘Train model’ button to train the model. Try clapping, waving and keeping still to see if those actions are estimated. Give your micro:bit to someone else to wear (making sure they put it on the same wrist and in the same orientation) and see if it works as well for them.
モデルの改善
ほとんどのモデルは、より多くのデータを学習させることで精度を上げることができます。 If the model needs improving, click on ‘← Edit data samples’.
You can delete any data samples which you think don’t fit or add more samples.
モデルを再度トレーニングし、再度テストします。
モデルとプログラムをmicro:bitに入れる
micro:bit CreateAI で、「MakeCode で編集」(Edit in MakeCode)をクリックすると、MakeCode エディターでプロジェクトのプログラムが表示されます。
You can modify the code or just try it out as it is. Attach the first micro:bit using a USB cable, click on the ‘Download’ button in the MakeCode screen, and follow the instructions to transfer your AI model and the code blocks to it. Afterwards, download again onto another micro:bit.
Attach a battery pack to the sender micro:bit. You can keep the receiver micro:bit plugged into the computer or attach it to battery pack. Now wear the sender micro:bit and test it out.
プログラムの仕組み
When the program starts, the radio group is set to 52. Radio groups are like channels, so any micro:bit using the same group will get the message. 0から255の好きなグループ番号を使用できます。 If you are using this project in a class or coding club, you might like to encourage each pair or group of students to use a different radio group from others in the class.
The 'on ML... start' blocks are triggered when the sender micro:bit’s ML model decides you have started waving, clapping or being still.
If you are clapping, the ‘on ML clapping start’ block causes a tick icon to appear on the LED display of the sender micro:bit and the radio message ‘lightsOn’ to be sent to the receiver micro:bit. If you are waving, the ‘on ML waving start block’ causes a cross icon to appear on the LED display of the sender micro:bit and the radio message ‘lightsOff’ to be sent to the receiver micro:bit.
If you are still or the sender micro:bit can’t recognise your movements, the LED display is cleared and no radio message is sent.
The ’on radio received’ blocks turn on all the LEDs on the receiver micro:bit if a message is received saying ‘lightsOn’ and clear the screen if a message is received saying ‘lightsOff’.
The ‘on radio received lightsOn / lightsOff’ blocks are from a MakeCode extension called ‘Radio broadcast’. If you want to code this project yourself, you can find the blocks by clicking on ‘Extensions’ then choosing ‘Radio broadcast’.
評価
How accurate is the light switch in turning on and off in response to your movements? How could you improve it, if needed?
How useful would this device be in real life? How does it compare to a regular light switch?
ステップ 3: 拡張する
- You could light up a room by putting the same code on multiple micro:bits. Keep the other micro:bits still while someone moves another to turn them all on or off.
- Add a fourth action which causes the receiver micro:bit to show dimmed lights. Use the ‘set brightness’ block, found in the LED section under ‘More’.
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.