Skip to main content

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:

  1. Understand the magnetic sensor and master its use.

  2. Understand the magnetic sensor programming building blocks in MakeCode.


Required Materials:

ItemPictureQuantity
micro:bit V21
Jacdac expansion board1
Jacdac 10cm connection wire1
Magnetic sensor1
USB Cable1

传感器原理说明:

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

Microsoft Makecode


Makecode programming

Step 1: How to add Jacdac extension

  1. Enter Microsoft Makecode and click "New Project".

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

  1. Click "Extend" in the code drawer.”

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


Step 2:

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

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

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


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.