Introduction: Automated Halloween Candy Dispenser

Candy is loaded into a long PVC tube hanging off the balcony. When a trick-or-treater approaches the house they see a halloween basket and instinctively put their hand into it. An ultrasonic sensor detects their hands and shoots off a candy from the tube.

Step 1: The Candy Kicker

I started the project with my 9 year old daughter and wanted to make a remotely monitored candy dispensing device. I used a Lego Mindstorms NXT motor and MIT App Inventor to program it. Hershey nuggets candy fit nicely inside a 1" PVC tube, and I figured I could dispense one at a time.

Mount the NXT Motor onto the end of the PVC tube. Initially I used a rubber band wrapped around the motor, but eventually hot melted some lego parts onto the tube.

  • Build a platform for the candy to slide out that is attached to the motor base. As shown in the photo it wraps around the left, but is also supported on the right.
  • Leave a gap in the candy slide for the kicker to poke through.
  • Attach the kicker to the motor shaft as shown. You can use rubber bands, cable ties, or hot melt glue to strengthen the kicker.
  • I added a rubber band that keeps the kicker closed even when power is off as shown in the video. If you leave the motor powered for a long period of time, it can overheat even when it is not moving.

Test that the candy drops into the gap, and that the kicker cleanly dispenses a candy.

Note, the second video shows my first attempt. In one position the motor dispensed a candy, and in a second position it closed off the tube. It worked for a short tube, but when I put it on a 10 foot tube, the load of all the candy prevented the motor from moving. The new approach works for the long tube and gives much more of a dramatic kick to the candy.

Make sure that you put stop at the end of your candy slide, so that your candy does not all come out at once.

Step 2: Ultrasonic Sensor in Halloween Bucket

I glued a Lego NXT Ultrasonic range sensor into a halloween bucket. I cut out a disk with 2 small holes to mount the sensor. I used a clear acrylic disk and a laser cutter, but it could more easily be made out of cardboard.

I routed an an extended NXT cable from the bucket up the Lego NXT Controller on the balcony. I used four 2-meter flex cables and cable extenders from An alternative would be to use a long Ethernet cable, cut off the ends, and then splice in an NXT cable. I had read in some forums that a long cable would not work with the I2C communications in the ultrasonic sensor, but I did not have any problems.

Step 3: Programming

There are a number of ways to program the Lego NXT controller, but I chose to use MIT App Inventor (more on the reason for this below). The App Inventor programs an Android phone which communicates via bluetooth with the NXT controller. To set up bluetooth connection with the NXT see tutorial at Make sure to setup the the bluetooth connection as shown in the tutorial and pair the Android phone and NXT controller.

The sensitivity of the ultrasound sensor is setup in the Design screen. As shown in the first figure I used BelowRangeEventEnabled, with BottomOfRange = 13.

As shown in the second figure, the motor is activated when the Ultrasonic sensor is below the specified range. The following command in green sets the ClockOpen to true, which allows the motor to run for just 1 second. Then the motor reverses direction for 1 second using ClockClose, and finally the power is set to zero. I used the NXTDirectCommands, but the NXTDrive commands are just as good and easier to code.

The last 2 figures show the setting of ClockOpen and ClockClosed, where each have a TimeInterval of 1000 milliseconds.

Our long term goal is to a remotely monitor and control the dispenser. MIT App Inventor can send and receive text messages, so next year we plan on programing the Android phone to send out a text message whenever a candy is dispensed. If we detect that one person is taking too many candies we could send a text back to temporarily disable the dispenser. We plan on leaving the Android phone on the balcony overlooking the dispenser. With Skype running on the phone we can see what is happening and project our voice to them as well.

For the project we used a barebones Android phone with no cell service, it just used our house wifi.

Step 4: How It Worked

It took a while to get the dispense to work without jamming, but once it was dialed in the device worked pretty consistently.

When trick-or-treaters showed up it took them a bit to figure out what to do, but eventually most did succeed and they were totally surprised and delighted when the candy popped out.


picklewoman (author)2014-12-25

it took so much work I am ndelsons daughter

MsSweetSatisfaction (author)2014-12-15

Ooo that's such fun! I bet it surprised many a trick or treater!


Thanks. It really was fun, and considering it was made out of Lego kids can really get involved in making it.