Home CodeMicropython A Raspberry Pi Pico and KY-002 shock and vibration sensor

A Raspberry Pi Pico and KY-002 shock and vibration sensor

by rp2040guy71

In this article, we connect a KY-002 shock and vibration 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 KY-002 shock and vibration sensor is a precise module for detecting shocks and vibrations. It consists of a conductive outer casing and an internal spring. In the event of shocks, the spring closes the contact to the outer casing and thus generates an electrical signal. This simple design enables reliable and fast detection of vibrations.

The sensor looks like this

The sensor has the following the pinout

Component Pin Description
(-) GND
middle +5V
S Signal

Parts Required

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

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

 

Schematic/Connection

Pico SENSOR
GPIO28 S
3v3 +
GND

 

Code Examples

from machine import Pin, Timer

# GPIO28 as input
sensor = Pin(28, Pin.IN, Pin.PULL_DOWN)

timer = Timer()

# Initialization of the counter variable
counter = 0

print("KY-002 Vibration sensor example")

def step(timer):
    global counter
    counter = counter + 1
    print(counter)

# Function that is executed on vibration
def shake(pin):
    timer.init(mode=Timer.ONE_SHOT, period=300, callback=step)

# Initialization of the interrupt
sensor.irq(trigger=Pin.IRQ_FALLING, handler=shake)

 

REPL Output

 

Disclaimer

This website may contain affiliate links, which means we may receive a commission if you click on a link and make a purchase. This comes at no additional cost to you.

You may also like

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