Halloween candy vending machine

Halloween Machine

© Lead Image © Inok, Fotolia.com

© Lead Image © Inok, Fotolia.com

Article from Issue 238/2020

A Halloween vending machine frightens visitors, but the braver ones receive a chocolate reward.

With a project as complex as a Halloween vending machine, you need to take everything step by step; otherwise, you quickly get confused. To begin, you must clarify what purpose the vending machine serves. The young visitors are supposed to start it themselves; it then performs a scary show and hands out a piece of chocolate. The show includes sound and light effects – which doesn't sound too complicated – at first.


A stable wooden beam forms the basis of the machine. All of the components can be attached to it easily. The temptation to use warped beams just because they are available and cost almost nothing will probably cause more trouble than spending money on good material. The specific machine from this example has a height of around 190cm (~6ft) with a footprint of 60x60cm (~2x2ft). However, no precise size and material specifications are made at this point, just a few tips.

Above all, you need to make sure that the body does not easily tilt or tip over. For this purpose, it is useful to add weights to the base (e.g., heavy stones). Wheels to roll the machine are useful, too. If you construct the vending machine in your garage or basement, test beforehand whether it will fit through the doors that deliver it to its final destination. I have had to disassemble and shorten a completely finished construction in the past just to get it outside.

Brain Power

Every machine needs a control system. I have used the PiXtend V2 -S- (Table 1) a few times in Halloween projects, and it has always performed excellently [1]; it has all the I/O interfaces I need and is easy to program. This time I chose to use the Python scripting language because it has useful libraries for all the components I use.

Table 1

PiXtend V2 -S- Specs

Digital Inputs and Outputs

8 Inputs (3.3/5/12/24V)

4 Outputs (max. 30V, 0.5A each)

4 PWM/servo outputs (2x16 bits, 2x8 bits)

Analog Inputs and Outputs

2 Voltage inputs (0-5V/0-10V)

2 Voltage inputs (0-10V)


4 GPIOs (5V)

Serial interface (RS-232)

4 Relays (max. 230V, 6A)

Support for up to four DHT11/DHT22/AM2302 sensors (temperature and humidity)

Slot for 433MHz transmitter (radio socket, not included)

Onboard Voltage Regulator

Inputs 12-24V DC (max. 30V)

Output 5V DC/2.4A (supplies PiXtend V2 -S-, Raspberry Pi, and connected USB devices)


Real-time clock (battery buffered)

Retain/persistent memory, 32-byte flash EEPROM

Designed for Raspberry Pi models B+/2B/3B

Certifications: CE, RoHS

Hardware Setup

The construction of the vending machine is divided into two areas: the conveyor belts to output the chocolate and the movement of the eyes and mouth. Figure 1 shows the chocolate store, which consists of two conveyor belts that alternately start up and hand out a piece of chocolate.

Figure 1: Conveyor belts with chocolate.

The sensors are capacitive proximity switches (see the "Zero-Contact Switch" box) with negative switching logic: As soon as something is in front of the sensor, the PiXtend input signal drops to 0V. If nothing is in front of the sensor, the voltage is 24V.

Zero-Contact Switch

In modern installations, zero-contact switches are increasingly found alongside traditional mechanical switches. The three most common sensors are:

  • A reflex light barrier, which is based on the principle of light reflection and comprises an infrared transmitter and an IR receiver. As soon as an object reflects enough light, the photoelectric switch is triggered. The range can be several meters.
  • An inductive proximity switch, which creates a magnetic field and triggers as soon as a metallic object interferes with the field. The range is often limited to a few centimeters.
  • A capacitive proximity switch, which generates an electric field and is triggered as soon as this field is disturbed. It has a similar range as the inductive sensor; however, the electric field can also be disturbed by non-metallic objects.

The conveyor belt starts as soon as the creepy show starts, so the chocolate is ready in time for the output. When the input to which the initiator is connected experiences a positive edge (transition from 0 to 24V) the conveyor belt stops. The belt motors operate at 12V, which they receive from a separate power supply. To get an idea of how the conveyor belt works, watch the video on YouTube [2].

The eyes and the mouth are made of WS2812 LED panels, which a Raspberry Pi controls. It makes sense to order the panels directly from China [3], because they are quite expensive on Amazon. To work with the panels later, you need additional information about their design. The LEDs, soldered in meanders (loops), cannot be addressed directly by physical position. The program contains a matrix that supports easier access to the individual LEDs.

A quick look at the circuit diagram (Figure 2) shows how the sensors and motors for the conveyor belt and the driver for the LEDs work together. The LEDs require 5V, but the GPIO on the Rasp Pi only supplies 3.3V. You therefore need to add a driver component between the Rasp Pi and the WS2812 to increase the voltage: I used an SN74HCT125 TTL [4] (hereafter 74125). Note that the device needs a power supply, which can be provided by the GPIO on the Rasp Pi.

Figure 2: Circuit diagram for the Halloween machine.

The WS2812 programmable diodes can handle 16,777,216 different colors. They consist internally of three individual LEDs for red, green, and blue. Each of the three colors can be controlled by 1 byte, so you can mix the three colors as required. The 3 bytes reach the LED from a shift register, and the protocol is very time-critical. A high-level language such as Python is therefore not really suitable in this case, but there is a Python library that uses assembler code to transfer the data.

The use of a shift register makes it possible to control almost any number of these LEDs as a long chain. On the Rasp Pi, you only need one GPIO pin to control each LED, no matter how many LEDs are in the chain; however, not every pin is suitable for this usage, because it needs certain timing characteristics. More detailed information can be found on the homepage of the library project [5].

At maximum brightness, an LED can consume up to 60mA, which you should keep in mind when dimensioning the power supply. If you do not turn the brightness up that high, the 5V output of the PiXtend (100mA) is fine for supplying the devices with power. If this is not enough, you need to connect both 5V outputs in parallel to reach 200mA.

The PiXtend is connected to the LEDs with a special flat ribbon cable (Figure 3), which routes out GPIO header pin 32. If necessary, you can easily make this yourself.

Figure 3: The flat ribbon cable that connects the WS2812 LEDs to the PiXtend.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • PiXtend V2

    The PiXtend board extends the Raspberry Pi with many useful interfaces and functions for new target groups.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More