Case 3: Magnetic Field Agent Team
Introduction:
When the magnet is close to the magnetic sensor, the micro:bit displays the "√" icon and makes a sound. When the magnet is away from the magnetic sensor, the micro:bit displays the "×" icon.
Case Purpose:
Understand the magnetic sensor and master its use.
Understand the magnetic sensor programming building blocks in MakeCode.
Required Materials:
| Item | Picture | Quantity |
|---|---|---|
| micro:bit V2 | ![]() | 1 |
| Jacdac expansion board | ![]() | 1 |
| Jacdac 10cm connection wire | ![]() | 1 |
| Magnetic sensor | ![]() | 1 |
| USB Cable | ![]() | 1 |
传感器原理说明:

Principle description
Magnetic sensor: a sensor used to measure the strength and direction of a magnetic field.
Connection diagram
As shown in the figure below, insert the micro:bit into the Jacdac expansion board and use a connecting wire to connect the magnetic sensor to the gold finger interface of the Jacdac expansion board.

Programming software
Makecode programming
Step 1: How to add Jacdac extension
- Enter Microsoft Makecode and click "New Project".

- Enter a project name in the pop-up window and click Create.

- Click "Extend" in the code drawer.”

- In the pop-up interface, enter "Jacdac" and click the search icon, select the Jacdac software library, as shown in the figure.

Step 2:
- Connect micro:bit to the computer with a data cable.
Note: If the micro:bit is running the Jacdac program for the first time, please pre-install a blank Jacdac program to the micro:bit, otherwise skip this step.

- Connect the sensor, MakeCode will automatically identify and simulate it, and the sensor will provide real-time feedback of the sensor status in the simulation area.

Click on "ADD BLOCKS" sensor expansion module.
Note: To connect a new sensor, repeat the "Click on "ADD BLOCKS" sensor expansion module" operation process.

Write the program as shown

Reference program link:
https://makecode.microbit.org/_h6DFV70EqF5c
You can also download the program directly from the following webpage, and start running the program after downloading.
Result
When the magnetic sensor detects a magnet, the micro:bit displays the "√" icon and makes a sound. When the magnet moves away from the magnetic sensor, the micro:bit displays the "×" icon.

Thinking
Can the magnetic sensor detect magnetic poles? You can try it yourself.
.png)



