A Raspberry Pi Pico and KY-031 Knock Sensor

In this article, we connect an KY-031 Knock Sensor to a Raspberry Pi Pico, any Rp2040 type board will be suitable. We actually tested this with a Cytron Maker Pi Pico

We will use Micropython for these examples but of course you can use the Arduino IDE as well if you have Raspberry Pi Pico support enabled

The sensor looks like this

Parts Required

You can connect to the module using dupont style jumper wire.

Name   Link
Raspberry Pi Pico
37 in one sensor kit
Connecting cables

 

Schematic/Connection

Pico SENSOR
GPIO28 S
3.3V middle pin
GND

 

Code Examples

Basic example in  thonny

from machine import Pin, Timer
import time

# Initialization of GPIO28 as input
button = Pin(28, Pin.IN, Pin.PULL_DOWN)

# Timer initialization
timer = Timer()
# Variables initialization
i = 0

def func(pin):
    global i
    i = i + 1
    print(i)


if button.value() == 1:
    # Initialization interrupt
    button.irq(handler=func, trigger=button.IRQ_FALLING)

# REPL
print("Tap the sensor")
print("-------------------------------------")

 

REPL Output

You will get multiple counts per tap – due to the bounce of the sensor and how hard you tap it

MicroPython v1.17 on 2021-09-02; Raspberry Pi Pico with RP2040
Type “help()” for more information.
>>> %Run -c $EDITOR_CONTENT
Tap the sensor
————————————-
>>> 1
2
3
4

Links

https://github.com/getelectronics/rp2040learning/tree/main/Sensor%20Kit/KY-031_Knock_Sensor

 

 

Related posts

A Raspberry Pi Pico and KY-001 Temperature Sensor

A Raspberry Pi Pico and KY-006 Passive Buzzer

A Raspberry Pi Pico and KY-012 Active Buzzer

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More