Search

Magic Wand Project For Halloween Delights

About the project

For this project, we are going to add a little flare to the quintessential Halloween activity of Trick-or-Treat.

Project info

Difficulty:

Platforms:

Estimated time:

1 day

Published:

13th December 2017

Items used in this project

Hardware components

Microcontroller board, Uno [Arduino] A000066 Microcontroller board, Uno 7640152110092 x 1
SWI5-N Series 5 W 5 V DoE Level VI Center Positive AC-DC Wall-Plug x 1
38 kHz 45 m 2.5-5.5 V Through Hole IR Receiver Module for Remote Control System x 1
antalum Capacitors - Solid Leaded 25volts 1uF 10% x 1
Carbon Film Resistors - Through Hole 56 ohm 5% x 1
Red T-1 3/4 5 mm 30° Tinted Diffused 600 mcd 1.85 V Solid State LED Through Hole x 1
1/4Watt Axial Leaded Carbon Film Resistors 1/4W 110 ohm 5% x 1
Stepper motor [Parallax] 900-00005 Stepper motor x 1
Cable Assembly 0.1m 28AWG Jumper to Jumper M-M x 1
Global Specialties GS-400 Solderless Breadboard with 400 Tie-points, built-in Bus Strip x 1

Story

Summer is over, and the days are growing shorter. It’s always sad to say goodbye to those lazy days of leisure. The good news is that those cooler nights mean one of the most creative holidays is almost here. Halloween has become a showcase event for makers to demonstrate their skills through whimsical projects that elicit smiles and the occasional shriek.

For this project, we are going to add a little flare to the quintessential Halloween activity of Trick-or-Treat. Before I go into exactly what this project entails, allow me to deliver some backstory. A week before summer vacation, my family took a little vacation to the Great Wolf Lodge family resort in the Poconos Mountains. We were all immediately drawn to the ultimate geek game, MagiQuest. If you’ve never encountered MagiQuest, where players purchase a wand and then race around the public areas of the hotel flicking the wand at various objects to complete a dozen or so quests. Each successful completion equips your characters with virtual runes that endow the owner with abilities that can be used in later, more complex missions. It’s hard to put into words exactly how much fun this game can be for kids and adults alike (even those of us who were already too old when Harry Potter became a thing). Unfortunately, when checkout day comes, the magic wand (Figure 1) loses its power—so to speak—as MagicQuest is confined to the walls of the Great Wolf Lodges.

Of course that is not an acceptable answer to an engineer. Once we got home, I delicately dissected the wand to see its electronic innards. The relatively simple circuit contains an infrared LED and a few motion sensors (tilt switch and spring sensor). It was pretty easy to deduce that the motion sensor detects when the user flicks their wrist, which triggers the IR LED to transmit a signal. It stood to reason that the various objects at Great Wolf Lodge must have had IR receivers that would decode the signals and respond accordingly.

At that moment I knew what I had to do for Halloween. Instead of just letting the neighborhood kids grab candy out of a plain plastic bowl, we could instead spark their imagination a bit by empowering them with a technologically-assisted magic wand to unlock the candy from a greedy pumpkin head. Translation: Build a circuit that can receive the MagiQuest wand's IR signal and open a secret stash of candy from a mechanically modified pumpkin-head bucket.

Of course, this concept could be reimagined to make a variety of props to meet your specific Halloween needs. With that, let’s take a look at how we made this work.

Materials

By employing commercial, off-the-shelf components, we can rapidly prototype a working system using just a handful of items. Once we have proven the concept of operations, custom hardware can be built to help drive down the unit costs of our solution. 

Overview

Notwithstanding the lighthearted nature of this project, it does illustrate a very valuable technical skill for electronics designers, both professional and DIYers. Sometimes we don’t get to design a complete system from scratch. There are times when there will be existing hardware that must be integrated into a new project. Sometimes you are lucky and the existing system is very well documented and the integration goes smoothly. Other times, especially for systems that are no longer supported by the original equipment manufacturer (OEM), documentation may be scarce. Learning how to do a bit of reverse engineering of an existing system is a valuable skill set to develop.

As mentioned before, each MagiQuest wand contains an infrared LED that broadcasts a unique 4-byte code (e.g., 0x09 23 5E 81). The code is hardwired during manufacturing so that each player can be uniquely identified. The infrared waveform itself is very similar to the format that television manufacturers use in their remote controls. The wand uses a 38kHz carrier frequency that is modulated into approximately 1ms pulses to denote a “0” or “1.” The reason that the carrier frequency is set to 38kHz is that there are very few natural infrared sources at that frequency, so it greatly reduces the chances of random triggering of the IR receiver by ambient light sources.

Let’s talk a little about the modulation schemes for infrared remotes, including the MagiQuest wand. Every bit transmitted over IR is composed of two parts: A mark and a space. A mark is the period of time during which the 38kHz IR signal is sent. The zero follows the mark, and during this period, the 38kHz IR signal is suppressed. So, to send both a “1” and “0,” the 38 kHz signal is required for a short period of time, followed by a time period of the 38kHz being off. There is not a single universal protocol that determines the relationship between the marks/spaces and 1s/0s. Don’t assume that what works for this project will work for another infrared transmitter.

For MagiQuest wands the following timing protocol is used:

“1” Mark = 580ms

“1” Space = 600ms

“0” Mark = 280ms

“0” Space = 850ms

That means to send a “1,” a 38kHz IR signal is transmitted for about 580ms and is followed by no transmission for approximately 600ms. This is roughly equal to a period of time of a 50 percent duty cycle. To send a “0,” the 38kHz signal is sent for 280ms and then no transmission for 850ms. This comes out to a roughly 25 percent duty cycle. Thus, the sequence to send the wand code 0×09 23 5E 81 would look like this (see below):

0b0000 1001 0010 0011 0101 1110 1000 0001

To open and close the pumpkin-head, we will use a servo and mechanical linkage that will cause the top of the pumpkin to open and reveal the candy treasure inside. We will also use some large red LEDs to provide some visual feedback to the user. It’s always a good idea to add visual or auditory cues to projects that involve technologies like infrared that are invisible to the human eye so that the user knows the system is doing something. Otherwise, they might get a little impatient if it appears the device is broken.

The Build

The overall project can be broken into the following parts:

The “Brains”

All sensors and actuators will connect to the Arduino Uno. It is perhaps the most prevalent form factor of the popular embedded development platform and one of the most inexpensive. The firmware will run on the open source ATmega328P microcontroller that is at the heart of the Uno.

Infrared Receiver

To detect IR signals, the TSOP38238 38kHz infrared receiver will be used. It comes in a handy, ready-for-prototyping, through-hole form factor. The RC network attaches to the Vs pin of the TSOP38238 to help reduce supply voltage ripple when Vs is less than 2.8V. If you use this component in a low power design keep this in mind. For a little refresher on low pass filters, check out this blog over at Mouser’s Bench Talk.

LED Eyes

To give a little visual feedback to the user, we will add two red LEDs that will serve as eyes for our pumpkin. The particular LEDs we are using have a forward voltage of 1.85V and a forward current of 20mA, which is at the top of what an Uno GPIO pin can deliver. Still, we will want the LEDs to be bright, so a 160 to 200Ω resistor will be used for current limiting. Each LED must have their own resistor. For more information on calculating current limit resistor value, check out this blog.

Servo/Mechanical Interface

servo motor will be used to control the mechanical mechanism used to open and close the pumpkin’s head and thus controlling access to the candy loot hidden inside. Be sure to select a GPIO pin on the Arduino that is Pulse Width Modulation (PWM) capable.

Power Brick

The Uno is equipped with a 2.1mm DC barrel jack. Other Arduino models can be powered with a standard micro USB power supply used for smartphones and other personal electronics. The recommended wall mounted supply delivers 1A of current at 5V.

Breadboard layout of the circuit

Schematic view of the same circuit from above.

Software

The general steps the code will loop through are as follows:

Initialize

This section only runs once at power up. During initialization we will setup the serial communication to the host PC that allows us to send debug messages to the terminal. We will also set pins to inputs or outputs as needed. Then we set up the servo and ensure it’s started in a closed state.

Main Loop

The code will sit in an infinite loop waiting until a MagiQuest wand IR is detected. It’s always a good design to minimize the code that runs in the main loop and instead embed singular functionality into as many functions as warranted. Reducing duplication of code is also beneficial from both debug and memory utilization perspectives.

Handle Wand IR Detection

Once the main loop determines some sort of IR signal has been received, it hands off the result to a decode function. The decode function determines if it is specifically from a MagiQuest wand and if it is the specific wand we are expecting, if both conditions are satisfied.

Image below shows the Arduino IDE.

Some of the values you may want to tweak include:

  1. const uint16_t SERVO_PIN = 3;

This constant sets the pin that attaches to the control wire of the servo motor. This pin should be set as an output.

  1. const uint16_t LED1_PIN = 5;
  2. const uint16_t LED2_PIN = 6;

These two constants establish the pins that are attached to the two red LEDs that serve as the eyes for the pumpkin. These pins should be set as outputs.

  1. const uint16_t IRRECV_PIN = 8;

Establishes the pin that the output of the TSOP38238 connects to the Arduino. This pin should be set as an input.

  1. const uint16_t MAGICWAND_SERIAL_NUM = 153312897;

153312897 is the decimal value equivalent of the hexadecimal value 0x9235E81 which is the unique code for my wand. This will need to be tweaked for your wand. Thankfully by running the code as is it will output the code to the serial debug window. That hexadecimal number has to be converted to a decimal. There are online converters available to help with this.

  1. const uint16_t pumpkinDelayLong = 15000;
  2. const uint16_t pumpkinDelayShort = 3000;

To give folks enough time to reach in and grab the candy before closing, these two values set a delay between the pumpkin head opening and closing. Feel free to alter to meet your specific needs.

In addition the standard setup() and loop() functions, four other functions were created to make the code easy to read and debug. These functions include:

  1. void decodeIR(decode_results *results)

This function will determine if the MagiQuest wand IR signal has been received and if it is the specific wand we desire. If so, it triggers other supporting functions to produce the desired effect of lighting the LED eyes and opening the pumpkin head.

  1. void turnLEDEyesOn()
  2. void turnLEDEyesOff()

These two functions turn both LED eyes on and off, respectively.

void operatePumpkinServo(const uint16_t desiredServoPosition)

The last function will accept an integer value between 0 and 180 and then turn the servo to the position denoted by the integer passed to it via the function call.

One note about using uint16_t versus the standard int. Using uint16_t helps to support code portability. The word size of an integer is not necessarily the same across various platforms and compilers. Using uint16_t ensures that a 16-bit integer is always used.

Check your work using the debug window.

Operations

With this project complete (Figure 8), it’s time to load up the pumpkin with candy, sit it on our porch, and plug the power supply into an AC outlet. Remember that when you open the pumpkin head with the wand, you have 18 seconds to load it up with candy. Once the eyes go dark you have three seconds remaining. Feel free to tweak the constants used to set these delays in the code.

Be aware that lots of direct sunlight could confuse the sensor. However, since this project is likely to be used at night, that should not be a big problem. If you repurpose this project for another purpose just keep in mind that it may be necessary to turn the IR sensor away from direct sunlight. You could also affix a narrow tube to the front of the IR receiver to block out ambient light. Of course, this makes it necessary to shake the wand almost directly in front of the tube as the viewing angle (referred to as directivity) of the IR receiver is now reduced.

Other Resources

Some last notes before we go. By the time you read this article a new Arduino board, the Arduino Primo, will be widely available (see below). Though more expensive than the Uno used in this project it has an onboard IR transmitter and receiver.

Schematics

Schematic diagram

Code

Credits

mbparks

Michael Parks, P.E. is the owner of Green Shoe Garage, a custom electronics design studio and technology consultancy located in Southern Maryland. He produces the Gears of Resistance podcast which shares news and tips for those interested in open source product development including embedded electronic hardware, software, and mechanical design. Michael is also a licensed Professional Engineer in the state of Maryland and holds a Master’s degree in systems engineering from Johns Hopkins University.


Leave your feedback...