Skip to content

Instantly share code, notes, and snippets.

@guoxiangke
Created February 21, 2024 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guoxiangke/352d68754acca46dc1a2d5c9ca5a9e00 to your computer and use it in GitHub Desktop.
Save guoxiangke/352d68754acca46dc1a2d5c9ca5a9e00 to your computer and use it in GitHub Desktop.
PIR sensor with raspberrypi
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