Pumpkin Pi

20161031_160820

My pumpkin on the left, and a neighbor’s pumpkin on the right

For Halloween I wanted to recreate an old project of mine, a jack o lantern filled with candy that can bite the hands that enter! My original was simply a cardboard jaw attached to a solenoid, which I manually controlled in secret. This time, I wanted automation and sound. I also wanted to build my first Raspberry Pi project. (I know I’m late to the Pi party 🙂 )

This project, dubbed Pumpkin Pi because of the Raspberry Pi inclusion, is quite simple. When a hand is detected, it plays a random sound and uses a servo to close the cardboard inner jaw. I used various free screams and “Halloween” themed sounds found online. I chose the Raspberry Pi because it makes playing audio very easy, compared to the Arduino which would need a separate circuit.

The circuit consists of a Raspberry Pi v2 B+ (though any model should work), a cheap powered speaker, a cheap sonar sensor, hobby servo, some LEDs, and a USB power supply.

Much can be added and improved in this project, mine is just the base model.

Code and more below!

20161029_162951

Cut off its scalp and remove the brains

20161029_162945

Raspberry Pi. LED is not connected here. Notice my attempt to power the servo directly from the USB power supply. It’s the red wire on the right that I soldered before the Pi’s fuse.

20161029_164440

Needs a nice big mouth to stick hands in!

20161031_160848

Dark picture, but shows the tight fit in the top. The orange cardboard is the jaw.

20161031_160859

Sonar sensor in the front, sitting in a little carved space. Powered speaker hanging in the back, behind the Pi. Servo motor on the right, in a tight carved space to hold it in place.

20161031_215117_moment

This pumpkin is lit at night, thanks to a couple LEDs running off Pi power.

Pi Details:

Remember this is my first time! Things could be improved.

I used Remote Desktop Connection on Windows to operate the Pi remotely. You can use the terminal (through Putty and such), but I found using the GUI easier.

I use the pigpio library to operate the servo with precise positioning. The main pumpkin program is a Python script. All the sounds are mp3s. A launcher script starts the pigpio daemon, sets the volume to max, and then runs the python script at boot.

Some useful websites:

Control of Pi remotely over wifi:

http://www.circuitbasics.com/raspberry-pi-basics-setup-without-monitor-keyboard-headless-mode/

Sonar Sensor use:

https://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi

Servo Use with pigpio (NOTE, I couldn’t get this to work directly, I had to modify parts, see my code):

Servo Test

Setting up a script to run on boot:

http://www.instructables.com/id/Raspberry-Pi-Launch-Python-script-on-startup/

My Code:

pumpkin.py

#Warner King
#Halloween 2016

import os
import time
import random
import pigpio
 
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)

TRIG = 15
ECHO = 14
SERVO = 18
LED = 24

pi = pigpio.pi()
pi.set_servo_pulsewidth(SERVO, 1000)
print "START"

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.setup(LED, GPIO.OUT)


GPIO.output(TRIG, False)
GPIO.output(LED, True)
time.sleep(2)
 
 
def randomCallOut():             #Play random audio file
    number = random.randint(1,5)
    if number == 1:
        os.system('mpg123 -q laugh.mp3 &')
        time.sleep(1)
    elif number == 2:
        os.system('mpg123 -q 1.mp3 &')
        time.sleep(1)
    elif number == 3:
        os.system('mpg123 -q 2.mp3 &')
        time.sleep(1)
    elif number == 4:
        os.system('mpg123 -q 3.mp3 &')
        time.sleep(1)
    elif number == 5:
        os.system('mpg123 -q 4.mp3 &')
        time.sleep(1)
    else:
        os.system('mpg123 -q 5.mp3 &')
        time.sleep(1)
                
try:        
    while True:                  
        GPIO.output(TRIG, True)    #Sonar Code
        time.sleep(0.00001)
        GPIO.output(TRIG, False)
        pulse_end = 0
        while GPIO.input(ECHO)==0:
            pulse_start = time.time()
    
        while GPIO.input(ECHO)==1:
            pulse_end = time.time()
    
        pulse_duration = pulse_end - pulse_start
        distance = pulse_duration * 17150
        distance = round(distance, 2)
        if (distance < 60):          #Check distance
            randomCallOut()
            pi.set_servo_pulsewidth(SERVO, 2200)
            time.sleep(1)
            pi.set_servo_pulsewidth(SERVO, 1000)
            time.sleep(9)
         time.sleep(.03)
        
except KeyboardInterrupt:
    pi.set_servo_pulsewidth(SERVO, 1000)
pi.stop()

 

launcher.sh  (Sets volume, starts pigpiod, then runs my python code. See link above, “running a script on boot”)

#!/bin/sh
# launcher.sh

amixer set PCM -- 95%
cd /
cd home/pi/Desktop
sudo pigpiod
sudo python pumpkin.py
cd /

Happy Halloween!

Leave a Reply

Your email address will not be published. Required fields are marked *