262
In this article, we connect an KY-020 Tilt switch from the ever popular 37 sensor kit to a Microbit
The KY-023 is a very basic tilt switch
Depending on the angle of the module, a switch closes the input pins briefly. This occurs due to the fact that there is a ball inside the switch which short-circuits a contact, depending on the position of the module.
Parts Required
Name | Link | |
Microbit |
|
|
37 in one sensor kit | ||
Connecting cables |
Schematic/Connection
We use P1 – you can change this but would need to change the examples
Example
All of these are from the Microsoft Makecode site
MakeCode
Python
pins.set_pull(DigitalPin.P1, PinPullMode.PULL_UP) def on_forever(): serial.write_line("" + str((pins.digital_read_pin(DigitalPin.P1)))) if pins.digital_read_pin(DigitalPin.P1) == 0: basic.show_leds(""" . . . . . . . . . # . . . # . # . # . . . # . . . """) serial.write_line("Switch triggered") else: serial.write_line("Nothing") basic.show_leds(""" # . . . # . # . # . . . # . . . # . # . # . . . # """) serial.write_line("**************************************") basic.pause(1000) basic.forever(on_forever)
JavaScript
pins.setPull(DigitalPin.P1, PinPullMode.PullUp) basic.forever(function () { serial.writeLine("" + (pins.digitalReadPin(DigitalPin.P1))) if (pins.digitalReadPin(DigitalPin.P1) == 0) { basic.showLeds(` . . . . . . . . . # . . . # . # . # . . . # . . . `) serial.writeLine("Switch triggered") } else { serial.writeLine("Nothing") basic.showLeds(` # . . . # . # . # . . . # . . . # . # . # . . . # `) } serial.writeLine("**************************************") basic.pause(1000) })