Pumpkin Pi


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!


Cut off its scalp and remove the brains


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.


Needs a nice big mouth to stick hands in!


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


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.


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:


Sonar Sensor use:


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:


My Code:


#Warner King
#Halloween 2016

import os
import time
import random
import pigpio
import RPi.GPIO as GPIO

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

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


GPIO.output(TRIG, False)
GPIO.output(LED, True)
def randomCallOut():             #Play random audio file
    number = random.randint(1,5)
    if number == 1:
        os.system('mpg123 -q laugh.mp3 &')
    elif number == 2:
        os.system('mpg123 -q 1.mp3 &')
    elif number == 3:
        os.system('mpg123 -q 2.mp3 &')
    elif number == 4:
        os.system('mpg123 -q 3.mp3 &')
    elif number == 5:
        os.system('mpg123 -q 4.mp3 &')
        os.system('mpg123 -q 5.mp3 &')
    while True:                  
        GPIO.output(TRIG, True)    #Sonar Code
        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
            pi.set_servo_pulsewidth(SERVO, 2200)
            pi.set_servo_pulsewidth(SERVO, 1000)
except KeyboardInterrupt:
    pi.set_servo_pulsewidth(SERVO, 1000)


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

# 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 *