Hands-on Tasks for micro:bit Practice

Introduction

This task sheet is part of the new course content for “2.4 Sensing and Control” and also serves as a prerequisite for the “Indoor Environment Real-time Monitoring System” project. It takes two class periods to allow students to experience the process of hardware programming with micro:bit through several simple practical tasks, aiming to help them initially master the basic methods of using a computer to write programs to control micro:bit and other smart terminals, thus laying a solid foundation for the subsequent “Indoor Environment Real-time Monitoring System” project.

The task sheet includes a total of 4 cases and 8 sub-tasks. Teachers can provide varying degrees of semi-finished code based on actual student needs, combining teaching and practice to strive for the integration of core subject competencies in project practice and the learning of knowledge and skills.

Hands-on Tasks for micro:bit Practice

Hands-on Tasks for micro:bit Practice

Practice and Experience:Controlthemicro:bitLEDarray
Practice Content:
1. Write a program formicro:bit.
Program Requirements: When the serial port receives “H“, display the “happy” expression; when it receives “S, display the “sad” expression.
Practice Steps:
a. Open the BXY editor, input the following code, check the syntax, and upload it to the micro:bit board.
from microbit import *
while True:
        if uart.any():
                incoming=str(uart.readall(),"UTF-8")
                incoming=incoming.strip("\n")
                if incoming=="H":
                       display.show(Image.HAPPY)
                        print("I am happy")
                elif incoming=="S":
                        display.show(Image.SAD)
                        print("I am sad")
                else:
                        print("err")

b. Debug the micro:bit through the serial port. Click on the “Serial” button to open the serial debugging window, input “H” and “S”, and any other characters, and observe the display results of the LED array and the serial monitor.

Hands-on Tasks for micro:bit Practice
2. Control themicro:bit‘sLEDarray using a Python program through the serial port.
Close the BXY editor, open the IDLE editor, and write the following code:
import serial
ser=serial.Serial()
ser.baudrate=115200
ser.port="COM3" # Adjust the port number as necessary
ser.open()

Run the above code, and in the IDLE interactive window, input:

ser.write(“H”.encode())
ser.write(“S”.encode())
Observe the results displayed by the LED array, and also check the IDLE interactive window display, as shown below:
Hands-on Tasks for micro:bit Practice
3. Write a program usingPython to implement more functions.
(1) Make the LED array switch expressions regularly.
For example: switch the screen expression ofmicro:bit every second.
The code is as follows:
import serial,time
ser=serial.Serial()
ser.baudrate=115200
ser.port="COM3"
ser.open()
while True:
        time.sleep(2)
        ser.write("H".encode())
        time.sleep(1)
        ser.write("S".encode())
Run the code and observe the results displayed by the LED array.
Additionally: Add the following two lines of code at the end of the loop:
    line=ser.readline()
    print(line.strip().decode())
Run the code again and observe the results displayed by the LED array while also checking the IDLE interactive window display.
Hands-on Tasks for micro:bit Practice
(2) Allow interaction by inputting characters in the IDLE interactive window and display real-time feedback information.
The code is as follows:
import serial,time
ser=serial.Serial()
ser.baudrate=115200
ser.port="COM3"
ser.open()
while True:
        name=input()
        ser.write(name.encode())
        line=ser.readline()
        print(line.strip().decode())
Run the code, and in the IDLE interactive window, input “H” and “S“, or any other characters, and observe the display results of the LED array and the IDLE interactive window.
Hands-on Tasks for micro:bit Practice
Summary:
In addition to the necessary hardware, this project practice also requires downloading the BXY_Python_Editor software and installing the pyserial module in Python. To facilitate students’ experience and save class time, teachers can pre-install the necessary modules and send the source code to students, allowing them to run the program directly.
The four cases in this task sheet progress from simple to complex, with increasing difficulty. Teachers can first demonstrate and introduce related knowledge points, then give students enough time to run the program, experience the complete process of hardware programming, and make simple modifications to the program functions according to their needs.

If you need the Word document of this article, source code, and answers to after-class reflections, you can join the “Python Algorithm Journey” knowledge group to participate in discussions and download files. The “Python Algorithm Journey” knowledge group gathers numerous enthusiasts, and more interesting topics are discussed here, along with more useful materials shared.

We focus on Python algorithms, so if you’re interested, come join us!

Hands-on Tasks for micro:bit Practice

Related Excellent Articles:

Reading Code and Writing Better Code

The Most Effective Learning Methods

Python Algorithm Journey Article Categories

Hands-on Tasks for micro:bit Practice

Leave a Comment

×