Created
February 21, 2024 17:42
-
-
Save guoxiangke/352d68754acca46dc1a2d5c9ca5a9e00 to your computer and use it in GitHub Desktop.
PIR sensor with raspberrypi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
from gpiozero import MotionSensor, LED | |
from signal import pause | |
pir = MotionSensor(4) | |
led = LED(17) | |
import glob | |
path = "/home/william/Documents/" | |
wavs = glob.glob(path+'*.wav') | |
def base_motion(): | |
sound_file = random.choice(wavs) | |
pygame.mixer.init() | |
pygame.mixer.music.load(sound_file) | |
pygame.mixer.music.play() | |
led.on() | |
while pygame.mixer.music.get_busy() == True: | |
continue | |
pir.when_motion = base_motion | |
pir.when_no_motion = led.off | |
pause() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment