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.
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.

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:

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())
line=ser.readline()
print(line.strip().decode())

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())

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!

Related Excellent Articles:
Reading Code and Writing Better Code
The Most Effective Learning Methods
Python Algorithm Journey Article Categories