Want to upgrade your prop sword/battle axe/light saber/magic wand with motion reactive light animation and sound effects? You can do just that with this simple project! All you'll need is a Circuit Playground or Circuit Playground Express, a battery pack, and zip ties to attach it all to any prop or toy. 

Or, you can go a bit further and fabricate your own wand with embedded Circuit Playground from scratch using a 3D printer. This more advanced build will require a LiPoly battery and JST switch cable, plus a LiPoly charger, and the use of a 3D printer.

To program the lights and sounds, there are a couple of options. You'll learn to program the Circuit Playground Express using Microsoft Make:Code's drag-and-drop web interface. Then, you'll program either the Circuit Playground Express or Circuit Playground with the Arduino IDE.

Lots of Coding Options

There are many options for programming the Circuit Playground Express! You can hand-code it with the traditional, text-based Arduino IDE, you can try your hand at the exciting new CircuitPython methods, or you can harness the incredibly user-friendly, yet powerful block-based interface of Microsoft's MakeCode web app!! (You can even go full circle and hand type code inside the MakeCode interface with Javascript!)

By simply dragging and dropping elemental code blocks, you can create Circuit Playground Express software in no time, and even test it out with the virtual Circuit Playground Express simulator built into the page.

Be sure you've gone through the tutorial here on the basics of MakeCode for Circuit Playground Express. 

Wand Lights and Sound

You're now ready to begin coding your own program with the MakeCode interface to use theCircuit Playground Express as a magic wand prop. 

Click the Projects button so you can start a new project.

Click the New Project... button to create the new empty project.

The new project should have only a single block in the layout area, the forever block. Any block of code you place inside the forever block will run over and over and over and over and over and over and over again. FOREVER! (Unless you create other inputs in your code that can change that, later.)

Rainbow Test

Try it out now. Click the LIGHT category. A collection of new blocks appears, all related to lighting up LEDs.

Click the block called show frame of _rainbow_ animation then drag and drop it inside the forever loop.

The animation block will snap inside the forever loop, and then after a moment, the Circuit Playground Express simulator will begin to play the rainbow animation.

In case the simulator doesn't play, click the play/stop button.

Notice what is happening here -- even though the show frame of _rainbow_ animation block only tells the Circuit Playground Express to play one frame, since it is inside the forever loop, it plays back a full animation over and over again.

Give your file a name in the name field, such as cpe_Wand.

As you did before, you'll Download the UF2 file, put Circuit Playground Express into bootloader mode by clicking the reset button, and then drag the UF2 file onto the CPLAYBOOT drive.

Now, the rainbow animation plays on your Circuit Playground Express!

User Input

What would happen if you required user input to initiate the show frame of _rainbow_ animation code? Let's find out! Click the INPUT category, then pick the on _button A_ _click_ block.

This adds the new input block to your layout. Drag and drop the show frame of _rainbow_ animation block out of the forever block and into the on _button A_ _click_ block.

Try this out on the simulator by clicking the left button a few times. Each time you do so, the rainbow animation progresses by one frame.

Download the UF2 file, put your Circuit Playground Express into booloader mode, and drag UF2 file to the CPLAYBOOT drive. 

Then, click the left button on your real world Circuit Playground Express to see the rainbow animation play back frame-by-frame!

NeoPixel LEDs can go from very dim to super bonkers bright! You can control them with a range of 0-256 levels of brightness. Adjust the overall brightness of all of the NeoPixels with the set brightness block. Click on the LIGHT category, then pick the set brightness block to add it to your layout area.

Setup

Notice how the new block is greyed out. This means that it isn't currently enabled. It needs to be part of a process that can run during some part of the program. Right now, this can either be any code blocks in the forever loop or any code blocks in the on _button A_ _click_ loop. Neither of these is what we want in the case of setting overall NeoPixel brightness. Sorry guys! For this, we'll use an on start loop. Any code blocks inside of on start will only be run once when the Circuit Playground Express first starts up. Think of it like an initialization phase.

Click on the LOOPS category, then chose the on start loop.

Rearrange the on start loop to the top of the layout graph, just to keep things logically and spatially organized. Then, drag the set brightness block into the on start loop.

Try adjusting the brightness value by typing 140 into the set brightness number value block. Then, upload the UF2 file to your Circuit Playground Express. 

When you press the left button the frame of animation will display as before, only this time it'll be much brighter!

Alternate Input

You can make your Circuit Playground Express do something while it waits to get instructions to do something else. What if we want to have the rainbow animation running unless the button is pressed, and then it'll play a sparkle animation for a moment?

You can do this by moving the show frame of _rainbow_ animation block into the forever loop, and then putting some new blocks into the on _buttonA_ _click_ loop.

First, move the show frame of _rainbow_ animation block into the forever loop.

If the LEDs are too bright, you can lower the set brightness value to something like 40 as seen here

Duplicate the show frame of _rainbow_ animation block by right-clicking on it and choosing the duplicate dropdown menu item. Then, drag the new block into the on _buttonA_ _click_ loop.

Change the animation pattern by clicking on _rainbow_ and selecting sparkle instead.

Try uploading this UF2 code to your Circuit Playground Express. Notice that the rainbow animation plays constantly, until you press the left button. Then, there is a momentary flash as a single frame of the sparkle animation plays. It then returns to playing the rainbow animation, since that's in the forever loop.

Well, that's not very fun! Let's use a different kind of loop to play a bit more of the sparkle animation upon each button press.

From the LOOPS category, create a repeat _4_ times/do loop. This is a really useful loop that will execute any contained code blocks x many times, with x being the integer number you fill in. In more traditional Arduino programming this might be represented as:

for(int i = 0; i<4; i++){

   //code to be repeated goes here

}

So you can see how convenient this loop block is!

Drag the show frame of _sparkle_ animation block into the repeat _4_ times/do loop.

The repeat/do and animation blocks are greyed out, since they don't sit inside of any loops that will be executed by the Circuit Playground Express. 

Move them as a unit into the on _buttonA_ _click_ loop.

Increase the number of repetitions from 4 to 24, then upload the program to the Circuit Playground Express and test it out by pressing the left button.

That's more like it! Now the rainbow animation plays until you click the left button, and you then get a moment of sparkle.

Shake It

To use our magic wand and sword props, it'll be much more fun to trigger the lights and sounds with motion instead of a button press. The Circuit Playground Express has a built in accelerometer that can be used to detect different types of motion. Lets swat out the button push input for a shake input.

Chose the shake loop from the INPUT category.

Drag the repeat/do loop (including it's contained show frame of animation block) into the shake loop.

Get rid of the empty on _buttonA_ _click_ loop by clicking on it and pressing delete on you keyboard.

Test the code on your board. Now, when you give it a good shake, the Circuit Playground Express will play back a bit of the sparkle animation, and then return to the rainbow!

Let's make the sparkles even more magical by setting them to maximum brightness!!

Make two duplicates of the set brightness block from the on start loop. Place one above the repeat _24_ times/do block, and one below it. Then, change tie value of the first one to 255. 

Test it on your board -- now, when you shake the Circuit Playground Express the sparkle animation will be super bright!

Don't look directly at the NeoPixels at full brightness, they're massively bright!

Picking Themes

For versatility, you can make your Circuit Playground Express work in different modes, depending on the prop with which you want to use it. Here's how you can use the selector switch on board to pick between two different color and animation themes, one for the wand and the second for the sword.

Slide Switch Input

Add an on switch moved _left_  loop. This is an input block, just like a button press or shake input, but uses the two position slide switch that's built onto the Circuit Playground Express. 

Duplicate the block and change the dropdown to make the on switch moved _right_ loop.

Variables

Next, we'll create a variable to help us check the state of the switch in other parts of the code. The variable is a named object that can store a value, in this case a number. Pick the set _item_ to block from the Make A Variable selections.

This block not only declares the variable, but can be used to change the value of the variable. 

Duplicate the set _item_ to block, and then place one inside each of the on switch moved loops. Also, set the value of the one in the on switch moved _right_ loop to instead of 0.

A nice thing about variables is that we can give them descriptive names that help you out. Let's rename the item variable to theme. That way you'll know whenever you see it in you code that this variable is used to store the value of which theme we're using: a means Wand, and a 1 means Sword.

Conditionals

You'll want to have the program logic react differently depending on the state of the slide switch.

  • If the switch is on the left, then it will play lights and sounds for the Wand when shaken
  • If the switch is on the right, then it will play lights and sounds for the Sword when shaken

To do this, you'll use a conditional operator called an if/then loop. Add an if/then loop from the LOGIC category.

 

We now need to create a block that lets the if/then loop know what condition needs to be met for it to evaluate as true or false. In our case, one loop will be true if theme equals 0, and the other will be true if theme equals 1.

From the LOGIC category, click on the 0 = 0 block.

Also, from the VARIABLES category, click on the theme block. This will allow you to compare the variable to a number (0 or 1).

Drag the theme variable onto the first in the 0 = 0 comparison block -- it'll replace the first 0. Then, drag the theme = 0 block on top of the true segment of the if/then loop.

This loop is now set so that whenever the theme is set to (the left position of the slide switch) it will run whatever code blocks are inside of the then section of the loop. 

Move the set of blocks out of the shake loop and into the if/then loop.

Then, put the whole thing back into the shake loop.

So, here's what this section of your code now does:

  1. Checks the slide switch
  2. Sets the variable called theme to a or depending on switch position
  3. Checks the accelerometer to feel for a shake
  4. Compares the value of theme to the number 0
  5. If the theme does equal 0, then it will bump up the brightness, play 24 frames of the sparkle animation, and then set the brightness back down

Try this out on your board now. You'll see that with the selector switch in the left position, it behaves the same as before, running the rainbow animation until shaken, at which point it plays the sparkle animation for a moment. With the switch in the right position, however, the shake has no effect.

Now that that's working, let's create a loop to play a different animation when the switch is in the right position.

Duplicate the entire if _theme_ = 0 loop, and then move the copy inside of the shake loop. Then, change the if statement value to 1, this means it will execute all code blocks inside of the if statement when the switch is to the right.

In this right switch loop, change the repeats to 300 and the animation to comet.

Test it out on the board -- you now get two different shake animations depending on the position of the switch!

Let's use the same method to change the animation inside of the forever loop. This way, if you're using the Circuit Playground Express for the magic wand, you can set the switch to the left for rainbows and sparkles, or, set the switch to the right for sword mode which includes comets and color wipes!

Match your block layout to the one shown here, then test it out on your board.

Sound

Now that you've gotten lights and action happening, it's time to add sound! 

To begin with, click the MUSIC category, then click on the MORE button, then chose the set speaker volume button. 

Add this block to the on start loop, so that the speaker volume is initialized on startup. To avoid bothering your neighbors, you can pick a low volume, such as 20 as seen here, or really blast it up to 255!

Next, create a block that makes some sound. Let's chose the play melody _power up_ block. Place this inside the "if left switch" loop of the shake loop. This means that if the condition "shake" is met, and the condition "switch is in the left position" is met, the sound will play.

You can select from a list of pre-made melodys, and then try them out by clicking the "shake" button on the Circuit Playground Express simulator. If you don't hear anything, try flipping the virtual selector switch to the other side.

Duplicate the play melody block, insert the copy into the right switch if loop, and then choose a different melody to play during sword swing maneuvers!

Code Cleanup

It works! You're almost ready to attach your Circuit Playground Express to a wand or sword prop! But before you do so, let's clean up the code a little bit.

Often, when you are first creating a program, you'll copy and paste whole chunks of code that are meant to do similar things, and then change them slightly. This works great, but you can then optimizing things by removing redundant code blocks.

Let's have a look at the shake loop.

Since we built the first if/then loop and then duplicated it, we've got more copies of the set brightness 255 and set brightness 40 blocks than we need. Since we want to up the brightness whenever we shake, regardless of the position of the switch, that block can live outside of the if/then loops. The same is true of the block lowering the brightness once the shake is finished.

Move one set brightness 255 block above and outside of the if/then loops, and one set brightness 40 below and outside, as seen here. Then, delete the extras.

That's looking cleaner already! Next, let's consider the two if/then loops. Since our switch only has two states -- left and right -- we know that if the switch isn't to the left, then it must be to the right. 

This is a great place to switch from two if/then loops to a single if/then else loop.

The if/then else loop will replace both if/then loops. Drag the first condition -- theme = 0 -- over to the condition block of the if/then else loop.

Then, move the play melody, repeat _ _ times, and show frame of _ _ animation, blocks from the first if/then loop to the then section of the if/then else loop.

Do the same with the blocks in the second if/then loop, moving them to the else section of the if/then else loop.

Replace the two empty if/then loops with the if/then else loop.

Your code is more streamlined now! You can also optimize the forever loop in the same fashion, to look like this.

Upload this code to your Circuit Playground Express and test it out. It should work identically, but it's good to know how you can optimize your code, especially if you start to create longer, more complex programs.

Here's what the final program looks like. Feel free to experiment now by changing things around, or making a whole new program from scratch!!

Using the Circuit Playground to make lights and sounds when shaken is pretty straightforward. If this is your first Circuit Playground project, you'll first need to make sure you've installed the Arduino IDE, added the Circuit Playground library to the Arduino software, and and been through the basic tutorials on its use. This excellent guide, Introducing Circuit Playground and Circuit Playground lesson #0 will show you how to do all of that and get you going.

Plug your Circuit Playground into your computer now, and launch the Arduino IDE. Double check that you've selected the board and port as in the Lesson #0 tutorial and are ready to upload code.

The Arduino sketch Circuit Playground Make Believe sketch does the following:

  • Plays one light pattern and sound file while the Circuit Playground (and the prop weapon to which it has been affixed) is idle
  • Plays a second light pattern and sounds file when swung
  • Plays a third light pattern and sound file when tapped

 

Download the file below, then unzip it and place it in your Arduino sketch directory.

Open the CircuitPlaygroundMakeBelieve sketch in Arduino, you'll notice that there are also three other tabs that open -- knight.h, laser.h, and wand.h. These are three different theme sets, which are combinations of light animations and sound files that are triggered at different motion thresholds on the Circuit Playground's accelerometer.

Upload the sketch to your Circuit Playground and then try it out. The NeoPixels will play their idle animation and you'll hear the idle sound coming from the piezo speaker.

Swing the Circuit Playground around a bit to trigger the swing animation and sounds. This works because the code is checking the built-in accelerometer for sudden changes above a certain threshold. When the change is great enough, the swing is triggered.  

The code also checks for even larger changes in acceleration, which indicates that there has been a tap on the Circuit Playground, or that your prop foam sword has hit its target! In this case, it will play the tap animation and sound.

The sketch uses pre-made theme sets of effects for use with different props. You can try them out to see which one works best for you. 

If you look at the top of the Arduino sketch you'll see the theme choices. By uncommenting one (and commenting out the other -- there can only be one uncommented at a time) that theme set of lights and sounds will be used the next time you upload the sketch to your Circuit Playground. 

For example:

// Enable ONE of these lines to select a theme:
//#include "knight.h" // Swoosh & clank metal sword
#include "laser.h"  // MWAAAW! "laser" sword
//#include "wand.h"   // Magic wand

will use the laser.h theme set. To switch to the wand.h theme set, do this:

// Enable ONE of these lines to select a theme:
//#include "knight.h" // Swoosh & clank metal sword
//#include "laser.h"  // MWAAAW! "laser" sword
#include "wand.h"   // Magic wand

Here we have a very nice, off the shelf foam sword. It sure looks like it could use some light and sound effects!

There's a flat part on the sword guard that looks perfect for attaching the Circuit Playground.

Once you've uploaded your code to the Circuit Playground, it's time to connect it to the AAA battery pack and attach it all to your prop weapon.

Depending on your prop, you can choose different methods to attach it. Small zip ties are great because the are sturdy, fit through the holes on the Circuit Playground, and can be customized to different lengths by adding one zip tie to the end of another.

Here, you can see the Circuit Playground zip tied to a foam sword.

After pulling the zip ties tight, use diagonal cutters or scissors to clip the exess length from the ends.

Those little zip tie ends can be sharp after cutting -- use nail clippers to round them down and avoid a scratch!

Next, feed the battery cable under the backside of the Circuit Playground, under the zip ties, then plug it in.

The battery pack fits nicely on the backside of the sword guard, also using zip ties. Create a zip tie harness for the battery pack by threading two through the back of the existing ones, double them with two more zip ties, then secure the battery pack. You can create an additional point of security with a set of three zip ties running around the battery pack horizontally.

Tighten the zip ties and clip the excess ends.

Swing it to try it out!

Next, we'll look at a more customized approch to building a prop magic wand from scratch to fit the Circuit Playground.

This magic wand was designed specially to use the Circuit Playground as its centerpiece effect. It is 3D printed with a mix of translucent and opaque materials, has a mounting plate onto which you can secure the Circuit Playground, as well as an integrated LiPoly battery and on/off switch.

In addition to the Circuit Playground, LiPoly battery, and switch, you'll also need four M3 x 6mm screws and nuts, two M3 x 16mm screws and nuts, and four M2 x 6mm screws and nuts.

Here are the files you'll use to print it.

Print the star parts in a translucent material, such as a natural PLA. The rest of the parts can be printed in any material you choose, and even painted for a more finished look.

The piezo speaker on the Circuit Playground isn't terribly loud, so there are small start cutouts in the shell to help the sound escape. The top star can also be used to access the reset button, should you need it.

Next you'll assemble the wand. Make sure you've uploaded the Make Believe sketch to the Circuit Playground, then mount it to the mount plate using 2.5mm screws and nuts.

Next, place the switch into the handle cutout, feeding the plug ends through their respective holes for the battery and the Circuit Playground board. Plug the battery into the switch and the switch into the Circuit Playground's JST connector. Test the switch to make sure the Circuit Playground turns on.

Place the battery in its place inside the lower handle bottom, then fasten the bottom with an M3 x 16mm screw and nut.

Note how the lower switch wire and battery wire are tucked into the handle with the connectors accessiblye through the handle opening. This is so you can pull them out and dissconnect them for battery charging without unscrewing the wand.

Sandwich the Circuit Playground and mount between the two star halves, feeding the switch wire through the wire channel at the base of the star top half.

Begin the assembly in this order:

  • Top half of handle
  • Trim piece
  • Star top
  • Circuit Playground and mount
  • Star bottom
  • Bottom half of handle

Screw the handle top parts together with the other M3 x 16mm screw, placing the nut on the opposite side.

Add the remaining trim pieces on the top and bottom of the stars and screw them together with M2 x 8mm screws and nuts.

Now, you can turn on the wand and wave and flick it to maximum magical effect. Should the battery ever require charging, you can pull the charging plug through the port in the handle and plug it into a LiPoly USB charger.

Remember, this guide is just a leaping off point for using the Circuit Playground to add motion triggered effects to any prop you like!

This guide was first published on Oct 26, 2016. It was last updated on Mar 08, 2024.