Home CodeSensor Kits Microbit and KY-021 Mini reed switch module

Microbit and KY-021 Mini reed switch module

by shedboy71

In this article, we connect a KY-021 Mini reed switch module to our Microbit – we will use Makecode and show the JavaScript and Python output as well

The KY-021 Mini reed switch module is a If a magnetic field is detected, the two input pins are short-circuited by pulling them towards each other.

The most common type of reed switch contains a pair of magnetizable, flexible, metal reeds whose end portions are separated by a small gap when the switch is open. The reeds are sealed within a tubular glass envelope. A magnetic field from an electromagnet or a permanent magnet will cause the reeds to attract each other, thus completing an electrical circuit. The spring force of the reeds causes them to separate, and open the circuit, when the magnetic field ceases.

Parts Required

Name Link
Microbit
37 in one sensor kit
Connecting cables

 

Schematic/Connection

MICRO:BIT SENSOR
0 Signal
3,3 V +V
GND GND

Example

All of these are from the Microsoft Makecode site

MakeCode

Python

pins.set_pull(DigitalPin.P0, PinPullMode.PULL_UP)

def on_forever():
    if pins.digital_read_pin(DigitalPin.P0) == 1:
        basic.show_icon(IconNames.NO)
    else:
        basic.show_icon(IconNames.YES)
    basic.pause(1000)
basic.forever(on_forever)

 

JavaScript

pins.setPull(DigitalPin.P0, PinPullMode.PullUp)
basic.forever(function () {
    if (pins.digitalReadPin(DigitalPin.P0) == 1) {
        basic.showIcon(IconNames.No)
    } else {
        basic.showIcon(IconNames.Yes)
    }
    basic.pause(1000)
})

 

Output

by default the led matrix will display an X, if you place a magnet close to the switch the display will change to a tick mark

Links

https://github.com/getelectronics/Microbitlearning/tree/master/37%20Sensor%20Kit/KY-021%20Reed%20Switch

You may also like

Leave a Comment