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!
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):
Setting up a script to run on boot:
#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 /