Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 24: Vibration Sensor Module (SW-420)

In this lesson, you will learn how to use a vibration sensor with the Raspberry Pi. We’ll help you connect the sensor to GPIO pin 17 and guide you through writing a simple Python script. This script will monitor the sensor and print a message whenever vibration is detected. This lesson is focused on giving beginners a hands-on experience in connecting a simple sensor to the Raspberry Pi and writing a straightforward script to interact with it.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi 5

BUY

Vibration Sensor Module (SW-420)

BUY

Breadboard

BUY

Wiring

../_images/Lesson_24_vibration_sensor_Pi_bb.png

Code

from gpiozero import InputDevice
import time

# Connect the digital output of the vibration sensor to GPIO17 on the Raspberry Pi
vibration_sensor = InputDevice(17)

# Continuous loop to read from the sensor
while True:
    # Check if the sensor is active (no vibration detected)
    if vibration_sensor.is_active:
        print("Vibration detected!")
    else:
        # When the sensor is inactive (vibration detected)
        print("...")
    # Wait for 1 second before reading the sensor again
    time.sleep(1)

Code Analysis

  1. Importing Libraries

    First, we import necessary libraries: gpiozero for interacting with the GPIO pins, and time for handling time-related functions.

    from gpiozero import InputDevice
    import time
    
  2. Setting Up the Vibration Sensor

    We initialize the vibration sensor by creating an instance of InputDevice from the gpiozero library. The vibration sensor is connected to GPIO pin 17 on the Raspberry Pi.

    vibration_sensor = InputDevice(17)
    
  3. Continuous Monitoring Loop

    A while True loop is used for continuous monitoring. This loop will run indefinitely until the program is manually stopped.

    while True:
    
  4. Sensor State Check and Output

    • Inside the loop, we use an if statement to check the state of the vibration sensor. If vibration_sensor.is_active is True, it means no vibration is detected, and “Vibration detected!” is printed.

    • If vibration_sensor.is_active is False, indicating vibration, “…” is printed instead.

    • This distinction is crucial for understanding how the sensor’s output is interpreted in the code.

    if vibration_sensor.is_active:
        print("Vibration detected!")
    else:
        print("...")
    
  5. Delay

    Finally, time.sleep(1) adds a 1-second delay between each iteration of the loop. This delay is crucial to prevent the program from overloading the CPU and to make the output readable.

    time.sleep(1)