Turn on a Lamp with a Gesture-Controlled Harry Potter Wand
To say Harry Potter is a cultural phenomenon is an understatement. It’s simply part of our culture, arguably as beloved as any other media franchise in history, inspiring many of us to try to capture some of the magic on our own — which is what this project is about.
After a recent trip to Universal’s Wizarding World of Harry Potter, my daughters and I decided to create a project that could use the interactive wands from the theme park, at home, to control our own props and gadgets. We called it Raspberry Potter because it was powered with a Raspberry Pi. We demonstrated the project last year at the Minneapolis/St. Paul Mini Maker Faire and this article — Ollivander’s Lamp — is the latest extension of that project.
Here’s how the Raspberry Potter works:
1. Using an infrared camera, the Raspberry Pi computer looks for small circles of reflected infrared light in its field of vision.
2. These small circles of light get tracked for movement, using OpenCV computer vision software. These patterns of movement are the gestures or “spells,” and you can make them using the reflective tip of a wand.
3. When the predefined patterns of movement are matched, a “spell” is cast (Figures A, B, and C) and the Raspberry Pi runs code that controls a connected device — in this case our magic lamp.
What About the Wand?
If you don’t have an official interactive wand from the park (Figure D), don’t worry! You can easily make your own wand by gluing a sequin to the end of a stick. Any wand-like object will work fine — it only needs a reflective tip that can be used to reflect infrared light. Just make sure the sequin is shiny and flat; the faceted ones don’t work well. You can also use a “pearl sticker” such as these.
We 3D-printed this wood-grained wand from Thingiverse and it turned out OK (Figure E). The Make: Labs used these cool-looking ones from Etsy, intended as party favors. Or check out the Raspberry Potter website for a link to my new book with detailed instructions for making your own wand.
PREPARE THE ELECTRONICS
1. Set up the Particle Photon
Let’s start with the Particle Internet Button, which will double as our light source (Figure F) — in addition to the Particle Photon microcontroller, it offers 11 bright RGB LEDs for all sorts of possible effects.
To be honest, using this device for our purposes is complete overkill — the Internet Button offers lots of other cool functionality like Wi-Fi connectivity, directional buttons, and a 3-axis accelerometer. But it’s easy to control and fits perfectly with our hurricane lamp concept. Plus, it provides endless possibilities for extending this project in your own way. Let’s set it up, add some code, and prepare to connect it directly with the Raspberry Pi.
1a. Follow these instructions to activate the Particle Internet Button.
1b. Once activated, grab the lightsource.ino code from my Github repo. Download the code and then flash it to the Internet Button through the online console at particle.io.
1c. We’ll power the Internet Button directly through the Photon’s breakout pins: Connect a wire to the 3.3V pin and another to GND as shown in Figure G. We’ll also use the Photon’s analog GPIO pins to control the lights; connect the A0, A1, and A2 pins to the Pi after setting up the Pi.
1d. Carefully remove the translucent plastic cover on the Internet Button. This will make the lamp just a little bit brighter. When done, it should look something like Figure H.
2. Set up the Raspberry Pi
We’ll need to add some software packages that give the Raspberry Pi (Figure I) the ability to utilize the camera, GPIO, and some basic computer vision. We’ll assume you’re running Raspbian Jessie with Pixel as your Pi’s operating system. If you aren’t, download and install it first.
2a. Install OpenCV
Once the Pi is running and online, install OpenCV. This will probably take a few hours! OpenCV is an awesome open source computer vision project, and there’s a great installation guide for the Raspberry Pi 3 here.
2b. Install PiCamera
Now make sure the camera module is ready to go. Physically install the Pi NoIR camera to the Pi by plugging in the ribbon cable. Then run the following command on the Pi:
sudo pip install --upgrade “pic amera[array]”
sudo pip install imutils
2c. Install Pigpio
Finally, we need to install Pigpio to allow us to communicate via Python directly through the Pi’s GPIO pins. Follow the instructions under “Method 1” here. Then start up the Pigpio daemon by running:
2d. Install the Raspberry Potter script
Now we can download and install the Python script that runs our little project. Download the latest and greatest Raspberry Potter script from the rpotter repo on GitHub.
Attach the Pi to a monitor or display and run the script to make sure it works properly. If everything is installed correctly you should see video from your camera on the screen.
2e. Set up the daemons
Now that we know it’s running, we have to add a background process (an init daemon) so that the Raspberry Potter script will start automatically anytime we start up the device.
Go to github.com/sean-obrien/rpotter/tree/master/ollivanderslamp and download rpotter-startup. Install this file to the /etc/init.d/ directory and make it executable by running:
sudo chmod 755 /etc/init.d/rpot ter-startup
Then register the script to run at startup:
sudo update-rc.d rpotter-startu p defaults
Now unplug the Pi from the display and restart it. It should start up the script automatically and start looking for something that looks like a magic wand.
3. Connect the Pi to the Particle
Now that our Raspberry Pi is ready to go, we’ll wire the Pi to the Photon. Figure J is a wiring diagram showing how to connect the two. You’ll also notice the commented pin references in the Photon code we downloaded earlier. Basically, connect pins A0, A1, and A2 on the Photon to pins 15, 16, and 18 respectively on the Pi. Make sure that 3.3V and GND from the Pi are connected to the Photon as well — otherwise it won’t get power!
When we’re done, it should look something like Figure K. Figure L shows the Raspberry Pi successfully powering the Internet Button!
4. Prepare the IR LED
Figure J also shows an infrared LED, which we’ll connect to the Pi later. Using an IR LED for illumination allows the Raspberry Potter to work in low-light conditions and even in the dark. Follow Figure J to add a resistor and power leads to the IR LED. I lazily used jumper wires (Figure M); you may want to solder yours.
You’ll need to choose a resistance value based on your particular LED. Check out ledcalculator.net to determine what resistor you’ll need.
CREATE THE LAMP
In the movies, Ollivander’s lamp looks a bit like an antique hurricane lamp, so that’s what I’ve based this build on. Feel free to use any kind of lamp, of course — the one I used can be found on Amazon (Figure N). We also love the Victorian hurricane lamp model by Louise Driggers but we haven’t tried to fit this project into it!
5. Modify the wick channel
It just so happens that our Particle Internet Button perfectly fits inside the lamp glass, and the base is large enough to fit our Pi — but how to wire the two together?
If we unscrew the burner, we can remove the wick (Figure O), which gives us a hole to thread wires through.
So far so good, but we also want to remove the wick channel (Figure P) so that the Internet Button can sit with a lower profile on the burner.
Remove the top of the burner, and then cut off the top of the wick channel (Figure Q) with a rotary tool, or bend it back and forth with locking pliers until it snaps off. Then reattach the collar to the burner (Figure R).
Now we’ll be able to thread the wires from the Internet Button through the collar into the base below.
6. Frost the lamp glass
Giving the lamp a frosted look will diffuse the Internet Button’s full-color LED light and make the lamp really glow. I just used standard Rust-Oleum frosted glass spray (Figure S) to get the effect I wanted.
Tape off the top of the lamp glass to protect it from chips or scratches while handling it (Figure T).
The spray takes about 5–10 minutes to dry per coat. After 5 coats, I made a stencil of the Deathly Hallows logo just so we know what universe we’re in (Figure U).
Of course, this would be a great time to apply some creativity — maybe do a stencil of your Patronus, or a lightning bolt. Apply your stencil to the frosted glass, and coat another 5 times. Here’s what it looks like after all 10 coats (Figure V). Looking good!
7. Modify the base
Drill 2 holes next to each other into the base of the lamp (Figure W): a larger hole for the camera lens, and a smaller hole for the IR LED. Make the holes close together, but leave enough room so that the camera board doesn’t block the hole for the LED. These holes in the picture are almost too close together.
Now you need to open up the base of the lamp so you can insert the electronics. Cut through the bottom with a rotary tool (Figure X). We’re lucky — it’s just barely big enough for our Pi.
For ease of access and installation, you may want to go further and cut a bit more out of the base. Then crimp the edges using pliers, so that there aren’t any sharp edges exposed (Figure Y).
8. Mount the IR LED and Camera
Take the IR LED that we wired earlier and thread it through the smaller hole in the base (Figure Z). Secure the LED in place with hot glue, but first test-fit the camera board to make sure you leave enough space for it (Figure AA).
We need to block as much visible light from the camera as possible to allow for more accurate infrared detection. Cut a small piece of developed film or IR-pass filter plastic and cover the lens of the camera board (Figure BB).
Secure the camera inside the base of the lamp with its lens looking out the hole, using hot glue (Figure CC). Be careful not to burn the board or any of the wiring!
9. Put it all together
Thread the Internet Button through the collar and then attach the collar to the base (Figure DD).
Attach the wires from the Internet Button to the Pi as shown in the wiring diagram. Attach the LED and the camera board to the Pi as well (Figure EE). Fit all the wires and the Pi into the base and connect power to the Pi. And you’re done!
You now have your very own magic lamp.
NOW FOR SOME WITCHCRAFT AND WIZARDRY
Inside Ollivander’s lamp, the OpenCV software is watching and waiting to track the shiny end of your magic wand. Point your wand at the lamp, then move the wand right and then up to cast the “Lumos” spell and turn the lamp on (Figure FF).
Move your wand right and then down to cast “Nox” and turn the lamp off (Figure GG).
Move your wand left and then up to cast “Incendio” for a fire effect (Figure HH).
Practice your spells in the mirror!
Making This Project Your Own
Now that you’ve done all the hard work, there’s so much more you can do to make this project your own:
» Create new light effects by modifying the lightsource.ino code. To start experimenting with colors, just replace the RGB values in the b.ledOn function with any number from 0 to 255, like this:
This line should make all the LEDs a light-blue color. Keep experimenting to get the effects you want: strobe lights, rainbows, holiday themes, it’s all possible!
» Add spells Open up the rpotter.py script and add a gesture to IsGesture(), add a spell to Spell(), and add the corresponding pin on your Pi (copy lines 55–56 and modify for your new pin). If you’ve created something cool, send me a picture and let me know!
» Connect more props If you’re feeling ambitious, and want to try more advanced effects — say you want to switch on a fan, or trigger a servo to open a box — you can hook your lamp up to low-voltage devices through the Pi’s GPIO pins, or higher-voltage devices through a relay like a PowerSwitch Tail II.
Whether you’re a wizard or a muggle, I hope you’ve enjoyed this project — it’s been so much fun to create. As a bonus, check out the Raspberry Potter website. You can contribute your own code, and I’d love to see your ideas for new effects or improvements. You’ll also find links to other code examples and Raspberry Potter projects to try.