Skip to content

Instantly share code, notes, and snippets.

@AO8
Created October 9, 2017 20:12
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 AO8/c3ca5b4a3d9a91ad9e4025cebf53af7c to your computer and use it in GitHub Desktop.
Save AO8/c3ca5b4a3d9a91ad9e4025cebf53af7c to your computer and use it in GitHub Desktop.
A fun Halloween-themed Python project, perfect for unsuspecting trick-or-treaters. Use a Raspberry Pi, the Pygame package, and a cheap PIR sensor to play a random .wav file when motion is detected.
# tutorial to connect PIR sensor to RPi at: https://www.raspberrypi.org/learning/parent-detector/worksheet/
# download the Pygame package at: http://www.pygame.org/download.shtml
# download free .wav audio files at: http://soundbible.com/
# this scream horror .wav file is a favoriate: http://soundbible.com/1627-Female-Scream-Horror.html
from gpiozero import MotionSensor
import pygame.mixer
from pygame.mixer import Sound
import random
from time import sleep
pygame.init()
pygame.mixer.init()
pir = MotionSensor(4)
sounds = [
# add .wav files in quotes to this list with commas in between, i.e. "scream.wav", "chainsaw.wav", etc
]
while True:
if pir.motion_detected:
print("Motion detected")
random_sound = random.choice(sounds) # chooses a random .wav file from the sounds list
play_random = pygame.mixer.Sound(random_sound)
play_random.play()
sleep(10)
play_random.stop()
else:
print("Waiting for motion")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment