The "Scaredy Pi" Automated Candy Dispenser
2018-10-31 | By ASHLEY
License: None 3D Printing Raspberry Pi
Intro:
Do you want to hand out candy, but have kids to take trick-or-treating? We solved the issue with our original “Scaredy-Pi Candy Dispenser”. Have you ever seen those photos taken at haunted houses at the exact moment someone has the life scared out of them? Well we coupled that concept with our automated candy dispenser. Now we can have twice the fun on Halloween without even being present.
Description:
The idea is that trick-or-treaters will walk up to the candy dispenser and push a button to dispense a small amount of candy. What they don’t know, is that this will also trigger a blinding light, blood curdling scream, and photo that we will inevitably post to Twitter. The concept was pretty straightforward, but with every project there’s got to be at least one difficult hurdle. For us it was the container for the candy dispenser.
We were unable to locate a pre-made dispenser, so the challenge was on to create our own. With a few different ideas in place, the one we tested first was a modified gravity fed dog food dish. We set up the project by adding a servo motor, a hinge, a 3D printed flap, and some cosmetic modifications. The idea was that the flap would be strong enough to close against the force of the candy coming down. Backup plan was to use an empty cheese puff container, 3D print a funnel for the opening, and add a rotating piece (driven by a stepper motor) with 4 flaps on it to feed the candy out in a controlled manner.
What You’ll Need:
- A container to use as the dispenser
- Raspberry Pi + SD Card
- Raspberry Pi Camera V2.0 NO IR
- Power supply- Raspberry Pi
- 5V Servo
- Logic controlled power strip
- Large Momentary SPST Button
- Computer Speakers
- Flood Light
- Hinges
- Servo Coupler
- Epoxy
See end of project to add all parts to cart.
Extra Tools:
- Dremel
- Soldering iron
- 3D Printer (optional)
- Drill
- Screwdrivers
Assembly:
The first step was finding the ideal pivot point for the flap to ensure correct motor placement and candy flow.
After this was determined (depending on the container you use), we drilled a hole for the motor shaft and mounted the motor with epoxy. The flap was attached and tested. There ended up being too much resistance for the candy to come through the opening, so we used the dremel to cut off the neck of the container. This broadened the opening for the candy which improved flow. The nice thing about this design is that the candy doesn’t come flooding out, so trick or treaters will actually have to pick a couple pieces from the opening before it closes again, preventing them from taking too much. “Won’t they get their hands caught in there?!” Actually no, before it closes the light and audio signal will trigger, scaring them away from the container. If they do somehow manage to have their hands still inside, the force is not enough to hurt anyone.
Wiring:
The wiring gets to be a little confusing because the physical pin numbers are different from the GPIO pin numbers used in the code. This diagram shows that difference and gives a visual on how to wire everything up.
The servo should be connected to 5V, GND, and physical pin 32 on the Raspberry Pi. The button will have one wire connected to 3.3V and the other connected to physical pin 12. The power strip will be connected to GND and physical pin 36 on the Raspberry Pi. Power the Pi, connect the camera, and plug the speakers into the Pi.
Code:
Once your Raspberry Pi is set up and you have the current version of Noobs uploaded onto your SD card, you can copy our code from below. You will need to enable the camera. Click the start menu, select Preferences and Raspberry Pi Configuration. From the popup, select the interfaces tab and enable the camera. Click ok, another popup will request to restart, hit no for now. The code is written in Python and commented very well should you need to change anything.
Users will need to change the angle of the servo in correspondence with their enclosure and use the terminal to download the libraries we’ll be using in the code that are not already included in Raspbian (these are the two lines you’ll need: sudo apt-get install vlc
pip3 install python-vlc)
At this point you will need to download your own creepy sound file. We used one from freesound.org. Once that’s done, reboot the Pi and the code should be ready.
The button push will send a high signal to GPIO pin 18 that is set up with the internal pull-down resistor. This signal will open the flap, pause for half a second, then signal the light and Pi camera. The Pi camera will take 5 pictures, with a short delay between each of them. After this the flap will close and the other peripherals will shut off, resetting for the next victim.
'''
Digi-Key Raspberry Pi Halloween candy dispenser
October 2018
Kyle Meier
Digi-Key Electronics
Credit to these sites used for code reference:
https://www.instructables.com/id/Servo-Motor-Control-With-Raspberry-Pi/
https://stackoverflow.com/questions/20021457/playing-mp3-song-on-python
https://raspberrypi.stackexchange.com/questions/667/is-it-possible-to-install-vlc/46829#46829
https://projects.raspberrypi.org/en/projects/getting-started-with-picamera/4
https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/
https://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/
https://learn.sparkfun.com/tutorials/raspberry-gpio/gpio-pinout
'''
from time import sleep
from picamera import PiCamera # Enable camera in Preferences>Raspberry Pi Configuration>Interfaces
import vlc # In terminal, "sudo apt-get install vlc" and then "pip3 install python-vlc"
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # Sets pin numbering scheme to BCM
GPIO.setup(12, GPIO.OUT) # PWM output to servo signal pin (typically yellow or white). Remember to connect the positive and negative of the servo to 5V and GND.
GPIO.setup(16, GPIO.OUT) # Logic output pin for the power strip (positive). Other power strip wire will go to GND.
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) # Input from pushbutton, using internal pulldown resistor. Other button wire will connect to 5V pin.
pwm = GPIO.PWM(12, 50) # PWM Frequency for servo set at 50Hz
pwm.start(0) # Start PWM with duty cycle of 0 (no signal being sent to servo)
camera = PiCamera() # Setup pi camera
p = vlc.MediaPlayer("/home/pi/Desktop/scream1.mp3") # The scream sound track we'll be playing. Modify to match location and name of your sound file. I used this track: https://freesound.org/people/adriancalzon/sounds/220619/
# Variables
buttonpress = 0
picnum = 0
# Function to set the servo angle
def SetAngle(angle):
duty = angle / 18 + 2
GPIO.output(12, 1)
pwm.ChangeDutyCycle(duty)
sleep (0.5)
GPIO.output(12, 0)
pwm.ChangeDutyCycle(0)
while (1):
if (GPIO.input(18) == 1):
camera.start_preview() # STart preview for the camera, it takes a little time for it to adjust to the lighting conditions.
SetAngle(0) # Set servo's angle to 0 degrees, opening the flap.
sleep (1)
GPIO.output(16, 1) # Turn the power strip (light) on
p.play() # Play the mp3 track selected in the setup
for i in range(5):
#Make sure to create a new folder on the desktop named "pictures"
camera.capture('/home/pi/Desktop/pictures/image%s.jpg' %picnum) ''' Capture a picture- note the "%s" in the save location, this will insert the value of the picnum variable
in the image's title. This allows us to store multiple images instead of rewriting over the previously saved image. '''
picnum += 1 # Increment the picnum variable by 1
sleep (1) # Time delay between pictures
buttonpress = 1
elif (buttonpress == 1):
p.stop()
camera.stop_preview()
SetAngle(85)
buttonpress = 0
GPIO.output(16, 0)
Final Placement and Setup:
Once everything is wired together and programmed, it’s time to set the trap. We 3D printed a hollowed out Frankenstein Monster’s head, filled it with black aquarium rocks, and placed the button down inside there.
This was set next to the candy dish, making it very obvious that the button needs to be pressed to get to the candy. A 3D printed hand we had from a previous project was placed on top of the candy dish with the Pi camera adhered to the front of it, and the Pi hidden on the back. Both 3D printed items can be found on www.thingiverse.com along with some other great creations, or you could model your own.
The speakers were cleverly hidden in the existing decor where we placed the project. The flood light we purchased has a clamp on it so we were able to clamp it to something up high and out of sight.
Summary:
This project was a fun, easy way to utilize the Raspberry Pi and a few additional pieces to be able to pass out candy without being home, scare a bunch of trick-or-treaters, and still enjoy the hilarity of it all without missing a thing.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum