NELSON HALLOWEEN PUMPKIN

NELSON HALLOWEEN PUMPKIN

06 Nov 2017/Alin/ Tutorial, Raspberry Pi, Technology, Python

This Halloween we wanted to mix scary with funny, so we added sensors, lights and sound to the traditional carved pumpkin.

This is what we needed:

  • a pumpkin (obviously)
  • 2.0 USB speakers
  • 5 multicolor LEDs
  • a Raspberry Pi 3 Model B
  • an infrared obstacle sensor 

We configured the Raspberry to connect on boot to our WiFi network using wpa_supplicant.conf like this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
        ssid="NETWORK_NAME"
        psk="NETWORK_PASSWORD"
        proto=RSN
        key_mgmt=WPA-PSK
        pairwise=CCMP
        auth_alg=OPEN
}

We have six audio files cut from this Youtube video, so every time the sensor detects movement, it triggers the playback of the first file, and after that a random file of the remaining five is being played. We also added a blinking light effect while the audio is on. For input, we use GPIO pin 18, and pin 15 for led output. The light effect is being executed in a different thread so it doesn't interfere with the audio and to avoid the risk of freezing the whole script.

Here is the code:

import RPi.GPIO as GPIO                          
import time     
import os
import random
import threading
from pygame import mixer

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)                          
pir = 18
led = 15                                       
GPIO.setup(pir, GPIO.IN)
GPIO.setup(led, GPIO.OUT)
start = False
mixer.init()
mixer.music.set_volume(1.0)

def blink():
    global start
    while start:
        rand = random.uniform(0,.5)
        GPIO.output(led, GPIO.HIGH)
        time.sleep(rand/2.0)
        GPIO.output(led, GPIO.LOW)
        time.sleep(rand/2.0)

def detect():
    while True:
        if not GPIO.input(pir):                       
            print "Motion Detected!"
            global start
            start = True
            try:
                print "Starting thread..."
                t = threading.Thread(target=blink)
                t.start()
            except:
                pass
            try:
                print "Playing sound 0..."
                mixer.music.load("nelson/0.mp3")
                mixer.music.play()
            except:
                pass
            time.sleep(2.5)
            rand = random.randint(1,6)
            try:
                print "Playing sound " + str(rand)
                mixer.music.load("nelson/" + str(rand) + ".mp3")
                mixer.music.play()
            except:
                pass
            time.sleep(5)
            print "Stopping thread..."
            start = False

detect()

To make the script run on boot, add in the .bashrc file "python /path-to-file.py &".

This is the dirty work:

20171030 110000

20171030 131302

20171030 132151

And this is the result:

You can download the project from the link below and make your own motion detecting device!

Download