KY switch modules and the spring switch

The 37-in-1 kit's button, mercury tilt, mini reed and knock modules, and the bare SW-18010P spring switch: a switch and a 10 k resistor.

The switch boards of the 37-in-1 sensor kit: the KY-004 button, the KY-017 mercury tilt switch, the KY-021 mini reed switch and the KY-031 knock sensor, and the bare SW-18010P spring vibration switch the Freenove kits ship. Aliases: key switch module, mercury switch, magic reed, knock module, vibration switch.

The four KY boards are one board with a different switch soldered to it: a three-pin header printed S + -, the switch, and one 10 k resistor. With the resistor from + to S and the switch from S to -, S sits high and the switch pulls it low.

Pins

Pin What it does
S The signal: high through the 10 k, low while the switch is closed.
VCC +, the middle pin. 3.3 V to 5 V.
GND -.

The SW-18010P has two legs, 1 and 2, and nothing else: read it with INPUT_PULLUP and a wire to ground.

The switches

KY-004, a button. A tactile switch, closed while held. It bounces for settle, 3 ms by default (0 turns it off), the middle of the 1 to 5 ms the bare pushbutton bounces.

KY-017, a mercury tilt switch. Tip the board and the drop of mercury runs onto the contacts and closes them, so tipped is S low. Mercury wets both contacts rather than striking them, which is why mercury switches are known for not bouncing: settle is 0 by default.

KY-021, a mini reed switch. A magnet closes it at close millimeters (15 by default; the listings give 10 to 20) and it opens again only past close / release, 18.75 mm, as the bare reed switch does.

KY-031 and SW-18010P, a spring. A coil spring round a center pin, normally open. A knock sets it ringing and it touches the pin again and again until it is still. LinkSprite gives the SW-18010P a conductive time of 2 ms and under 30 ohms closed, so a knock here is 30 ms of closures of 1 to 3 ms with gaps of 1 to 5 ms. The 30 ms and the gaps are an assumption of that order, not a figure from a sheet. Press knock for one tap, shake to keep it shaking.

Properties

resistor is where the 10 k sits. pull-up (the default) is the board above, active low, which is how Joy-IT's boards and most clones are built. The original Keyes boards and some clones put the switch on the + side and the resistor to -, which makes S active high: that is pull-down. A multimeter across the resistor tells you which board you have.

What it does not model

The contacts' current rating (the SW-18010P's 20 mA), how hard the knock is, the direction of a tilt or a shake, and the LED that some batches of the KY-017 carry.

Common mistakes

Polling the knock sensor every 100 ms. A closure lasts a millisecond or two, so a slow loop misses it. Read it in a tight loop or on an interrupt, then ignore it for a while: the kit's sketch waits 250 ms.

Reading every KY board as active high. Check which way yours is built.

See it in action

Knock module on pin 3 (the Keyestudio kit's project 15) and Vibration alarm on pins 3 and 13 (Freenove 20.1) use these. Open them at /templates.