Microbit and KY-031 Knock Sensor

In this article, we connect a KY-031 Knock Sensor module to our Microbit – we will use Makecode and show the JavaScript and Python output as well

The KY-031 Knock Sensor module is a vibration sensor that sends a signal when a knock/tap is detected on the module.

This module consists of a spring-based sensor, a 10K resistor and a header. The spring sends a high signal when a vibration is detected.

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

def on_pulsed_p0_low():
    basic.show_icon(IconNames.NO)
pins.on_pulsed(DigitalPin.P0, PulseValue.LOW, on_pulsed_p0_low)

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

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

 

JavaScript

pins.onPulsed(DigitalPin.P0, PulseValue.Low, function () {
    basic.showIcon(IconNames.No)
})
pins.setPull(DigitalPin.P0, PinPullMode.PullUp)
basic.forever(function () {
    if (pins.digitalReadPin(DigitalPin.P0) == 1) {
        basic.showIcon(IconNames.Yes)
    }
})

 

Leave a Comment

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views :