Ghost Candy Dispenser
Hi there! Did you enjoy Halloween in 2024?
Since I'm usually not home during trick-or-treat time, for the past three years, I've been leaving handmade candy dispensers at the front door. Unfortunately, this year's dispenser didn't work well on the day because the battery ran out midway, and the candies often got stuck. So, I modified it to allow switching between USB power and battery power, and improved the mechanism to reduce the jamming problem. In this project, I'll share how to make this year's improved version. I hope this project is useful for next Halloween, or at the very least, that you enjoy reading about it!
Supplies
Parts
- MCU: M5StampS3 (M5Stack) (×1)
- RC servo motor: MG90S (×1)
- HC-SR04 Ultrasonic Sensor Module (×1)
- 1.5" 4 Ω 3 W speaker and PAM8403 amplifier module (×1)
- DFPlayer mini MP3 player (×1)
- Micro SD card (×1)
- DC-DC converter: Adafruit MiniBoost 5V @ 1A - TPS61023 (×1)
- USB-C socket breakout (×1)
- 3.7 V LiPo battery (optional)
- Power switch: slide, SPDT (×1)
- Red LEDs: pre-wired SMD LED, RED (×2)
- RGB LEDs: WS2812B breakout module (×10)
- Discrete Semiconductor parts: resistors 1 kΩ (×1), 4.7 kΩ (×3), 10 kΩ (×1); diode 1N4148 (×1); NPN transistor 2SC1815 (×1)
- Pin headers and sockets
- Universal PCB (×1)(depending on wiring)
- Screws: M3 × 8 mm with nuts (×2), M2 × 6 mm with nuts (×2), M2 × 8 mm with nuts (×2), M2 × 10 mm with nuts (×2)
- Candies: Hi-Chew
Tools
- 3D printer: Flashforge Adventurer 4 (Filament: black and white PLA)
- Screwdrivers
- Soldering tools
- Acrylic paint tools
- Masking tape (optional)
- Double-sided tape
- Hot glue gun
Software
- Visual Studio Code with PlatformIO IDE (Arduino)
- Autodesk Fusion
Service
3D Printing
I designed the model using Autodesk Fusion. The ghost part and the Jack-o'-lantern parts were designed using the sculpt (form) mode, while the other parts were designed using the normal solid mode.
Print all parts. I used white PLA filament for ghost.stl, JOLFront.stl, JOLBack.stl, side1.stl, lever.stl, and backBoard.stl. The reason for using white filament is that it reflects the LED light better. The other parts were printed with black filament.
Note that printing ghost.stl and base.stl takes a long time. Printing each the part took over 12 hours, though that depends on your printer and settings.
Painting
Paint the ghost part and the Jack-o'-lantern parts with acrylic paint. It's a good idea to cover the unpainted areas with masking tape before you start painting.
Candy Cartridge Assembly
Assemble the candy cartridge. The numbers in the attached figure correspond to the steps below.
- Insert the back board and the front board into the side1. Note the orientation of the back board.
- Insert the side2 into the previous assembly while inserting and holding the flap.
- Insert the bottom into the previous assembly from the front side.
- Ensure the flap moves smoothly. If it doesn't, enlarge the holes on the side1 and side2 using a drill bit, or sand the flap.
- Insert the part assembled previously into the base until you hear it click.
Designing the Schematic Diagram
The schematic diagram is attached. Connect the J1 socket to the J2 pin header when using USB power. Connect the J1 socket to the J3 pin header when using battery power.
Choose a battery with the appropriate capacity depending on your operating time and frequency. For reference, the results of the current measurements are shown below. The measurements were performed at the SW1 point using a digital multimeter.
- The current in the standby mode was approximately 285 mA. In the mode, the 10 RGB LEDs were set to full brightness, the two red LEDs were on, the ultrasonic sensor was measuring, and the servo motor and DFPlayer mini were in standby.
- The maximum current in the active mode was 350-650 mA when the candy dispensed smoothly. However, if the candy got stuck, the current exceeded 1.2 A. In this mode, the LEDs were the same as in the standby mode, the ultrasonic sensor stopped measuring, and the servo motor and the speaker were active.
For this project, I used my current favorite MCU, the M5StampS3 featuring the Espressif ESP32-S3 chip. Its operating voltage is 3.3 V, while the ultrasonic sensor and the DFPlayer mini operate at 5 V. Therefore level shift is needed to connect them. The resistor R1-R5, diode D1, and transistor Q1 in the schematic are necessary for the level shift between 3.3 V and 5 V.
Wiring
Wire all the electronic components according to the schematic diagram. Here’s how I approached it, though other methods would also work well. The numbers in the attached figures correspond to the steps below.
- Red LEDs Wiring
- a. Insert the red LED leads through each ghost eye's hole.
- b. Fix the leads inside the ghost using a hot glue gun.
- c. Solder and connect the red LED leads via wires to a three-pin header.
- the Jack-o'-lantern Wiring and Assembly
- a. Insert two M2 nuts into the JOLBack.stl part as shown in the figure. (Tip: Attach a nut to an M2 screw, pull the screw with the nut to set the nut into the hole, and then remove only the screw.)
- b. Solder and connect the ultrasonic sensor and the D13 RGB LED via wires to a six-pin header (one pin is not used). Note that the pin header should be soldered after threading the wires through the hole inside the JOLBack.stl part.
- c. Attach the ultrasonic sensor with the JOLBack.stl part using two M2 × 10 mm screws.
- d. Attach the JOLFront.stl part with the JOLBack.stl assembly.
- Switch Wiring
- a. Solder and connect the switch via wires to a two-pin header.
- b. Insert the wired switch into the base part, ensuring the 'ON' mark on the base part and the orientation of the wired switch match.
- c. Fix it using a hot glue gun.
- USB-C Module Wiring
- a. Solder and connect the USB-C module via wires to a two-pin header.
- b. Insert the wired USB-C module into the base part as shown in the figure.
- c. Fix it using a hot glue gun.
- Battery Wiring (optional)
- a. Create wires connecting between your battery and a two-pin header.
- Speaker Wiring
- a. Solder and connect the speaker via wires to a two-pin header.
- RGB LEDs Wiring
- a. Solder and connect each of the nine RGB LEDs (D4-D12) via wires, and connect VDD line, GND line, D4's DIN and D12's DOUT to a four-pin header. The D4-D6 LEDs are intended to be attached to the candy cartridge, and the D7-D12 are intended to be attached to inside of the base. Please refer to the figure for each wire length.
- b. Attach the wired RGB LEDs to the cartridge and base part with double-sided tape as shown in the figure.
- Main Board Soldering
- a. Solder the DFPlayer mini, the amplifier, and M5StampS3 onto a universal PCB and wired all discrete semiconductor parts. When reusing the parts, use headers and sockets. This time, they were used only for M5StampS3.
- b. Solder the sockets for the pin headers wired in the previous step 1-7 and the servo motor.
- c. Cover the back side of the main board with tape to protect against shorts.
Uploading the Code
I'll share my code on GitHub. The code was written using PlatformIO. Download and upload the code to the M5StampS3.
The sound used in the project was downloaded from https://freesound.org/. I used 'Happy Halloween Cute Voices' by NicknameLarry and converted it to mp3 file. Create a folder named 'mp3' on the micro SD card, and store the sound file named 001.mp3 within it. Then, insert the micro SD card into the DFPlayer mini.
Downloads
Final Assembly
In this step, all parts are assembled into the base part. The numbers in the attached figures correspond to the steps below.
- Attaching the Servo Motor
- a. Connect the servo motor's connector to the main board and start the program. Confirm the servo motor rotates to 0-degree position. Then remove the connector.
- b. Insert two M2 nuts into the nut spaces as shown in the figure.
- c. Attach the servo motor and fasten it with two M2×8 mm screws.
- d. Attach the lever to the motor horn and fasten it with two M2×6 mm screws and nuts as shown in the figure.
- e. Attach the assembled lever to the servo motor and fasten it with the motor screw. Ensure that the lever is outside of the cartridge. This lever position should be set to the 0-degree position.
- Attaching the Jack-o'-lantern
- a. Attach the assembled Jack-o'-lantern part to the base and fasten it with two M3×8 mm screws and nuts.
- b. Paint the visible screws and nuts black using a marker pen or acrylic paint.
- Putting All Electronic Component
- a. Insert all pin headers and connectors into the sockets on the main board.
- b. Place the main board inside of the base.
- c. Put the speaker into the front side of the base as shown in the figure.
- Loading Candies
- a. Load candies into the cartridge. It can store about ten candies. The cartridge is designed to fit Hi-Chew. However, if the candies are the same size, it might work well too.
- Attaching the Ghost
- a. Cover the ghost on the base while inserting the tree parts inside the ghost into the designated spaces as shown in the figure.
- b. Attach the ghost by gently pushing the front and back edges of the ghost inward.
- c. To remove the ghost, lift it up while gently pushing the front and back edges inward as in step b.
Have Fun!
You can decorate it at home, use it at events or parties. Enjoy using it however you like!
It might be exciting to increase the variety of sounds or change the way the LEDs lights up.