Get crafty and make a light up fairy crown using NeoPixels and an Adafruit Gemma.   The coding is easy with MakeCode -- just drag and drop blocks of code to get the custom animations of your dreams. In this project we'll explore the Photon function in MakeCode to create a twinkling rainbow gradient effect.  

This is a perfect project to do with kids, or use this same concept in your artwork to add lights to your Etsy store products or craft fair booth inventory.   

These LEDs are perfect for wearables -- they're pre-soldered for you, and encased in plastic so they're waterproof, bend-proof and squish-proof!  They're also small enough to be hidden in your project, and the silver connector wires are easy to disguise.   What are you waiting for?

Attaching NeoPixel strips to your costume can be a struggle as the flexible PCBs can crack when bent too much. So how to add little dots of color? Use these stranded NeoPixel dots!...
Out of Stock
The Adafruit Gemma M0 is a super small microcontroller board, with just enough built-in to create many simple projects. It may look small and cute: round, about the...
$9.95
In Stock

I've included two different ideas for powering your lights: a LiPoly battery or a AAA battery case.  There are pros and cons for each method.  The LiPoly batteries are lighter, smaller, and will last a good long while -- perfect for hiding in wearable costumes.  However, they do come with some dangers: they can catch fire if they get mangled or wet, the leads can break fairly easily, and they require a special charger which makes it harder to swap out batteries in the middle of a festival or event.  

AAA batteries are easy to find and easy to use.  They are a lot bulkier, and harder to hide in a costume, but if you're making things for sale, they make a bit more sense.  You don't have to sell a separate charger and show customers how to use it, and don't have to worry about any liability if kids end up wearing your creations.  

My rule of thumb: if I'm making a costume for a cosplay photo shoot or a professional event where looks matter a lot, I'll use LiPoly.  If I'm making something as a gift or for sale, I'll use AAA. 

It never hurts to have both kinds of batteries / battery cases on-hand.  If my LiPoly batteries die in the middle of an event, I can always replace them with AAA at a moment's notice since the connectors are the same.

Lithium ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery...
$7.95
In Stock
Oh so adorable, this is the tiniest little lipo charger, so handy you can keep it any project box! Its also easy to use. Simply plug in the gold plated contacts into any USB port and a...
$5.95
In Stock
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about...
Out of Stock
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAA's. This is a...
$1.50
In Stock

One more benefit of the AAA battery holder is that it has an onboard on/off switch.  The Gemma has an on/off switch, but if you leave your battery plugged in all the time it will slowly drain, even with the Gemma switched off.  The easiest way to add an accessible power switch to your project is one of these JST connector switches.  Simply plug it inline with your battery (either LiPoly or AAA) and your project has an easy-to-find clicky switch that will turn your project completely off.

By popular request - we now have a way you can turn on-and-off Lithium Polymer batteries without unplugging them.This PH2 Female/Male JST 2-pin Extension...
Out of Stock
By popular demand, we now have a handy extension cord for all of our JST-terminated battery packs (such as our LiIon/LiPoly and 3xAAA holders). One end has a JST-PH socket, and the...
Out of Stock

The LED strand has 3 wires.  The red-striped wire goes to the Gemma's Vout pin, the middle wire goes to D1, and the third wire goes to G.  

Be sure you are wiring to the IN end of the strip.  LED strips have an IN and OUT end and they won't work if you wire them the wrong way.  If you loo closely at the back of the pixels you can make out an arrow pointing in the direction of data flow (from in to out).  This is really hard to see, so it's usually reliable to start from the end of the strip that has the male connector.  If you find your project isn't working, you may have a strip that's manufactured differently -- try starting at the other end.

Find the end of the LED strand that has the female connector.  This should be the OUT end of the strip.  Double check this is right by looking for an arrow on the back of the LEDs pointing in the direction of data flow.

 

Use a pair of wire cutters to cut this connector off with a few inches of wire still attached.

Separate the three wires on the connector and strip about 1/4" from each one using a pair of wire strippers.

Wrap the bare ends of the wires through the holes on the Gemma as shown, and use a soldering iron to solder them in place.  The red wire will go to VOUT, the middle wire goes to D1, and the remaining wire goes to G.

If you're using a LiPoly battery, take a rubber band and carefully wrap it around the battery, trapping the wires inside.  This will provide strain relief so your battery leads don't break off.  This is a weakness of LiPoly batteries, and the rubber band trick will save you lots of heartache.

Plug your LiPoly battery or your AAA battery holder into your Gemma and see if the onboard light comes on.  If it doesn't, try flipping the tiny on/off switch on the face of the Gemma.  If it still doesn't, be sure your AAA battery holder's on/off switch is also turned on and that your batteries are charged.

I've built some code that makes a 20-light strand of NeoPixels twinkle and shift through a rainbow spectrum.  Here's a link to the finished project if you want to skip ahead and work backwards.  

To follow along and learn how I did it, point your browser at maker.makecode.com and select "New Project".  

Select the Gemma M0 board, since that's the one we're working with today.   While you're here, take a second to notice all the other development boards MakeCode will work with.  So many possibilities!

You'll find yourself in the MakeCode editor window.  From here, you can drag and drop snippets of code from the different blocks listed in the middle, into the workspace on the right.  Then, the Gemma emulator will simulate what your code is doing on the left.  Nifty! 

 

We'll start with the on_start loop, which will run some code when the Gemma first boots up.  For now, drag the forever loop down out of the way.

Click on the LIGHT block at the top of the list.  Inside are a bunch of choices having to do with NeoPixels.  First we'll tell our Gemma that we soldered a light strand with 20 lights onto pin D1.  Then we'll tell it how bright we want our lights to be.  Drag the two code snippets indicated out onto your workspace and place them inside the on_start loop.

We soldered onto D1, not D0, so change the first dropdown to D1.  Since we have 20 lights in our strand, change the number in the set strip snippet to 20.  Then choose your brightness.  For costumes I like around 150 -- bright enough to see but not so bright you'll blind people.

The very easiest way to get your lights going is with one of the pre-made animations that come with MakeCode.  Back in the LIGHTS tab, drag out an instance of strip show animationonto your workspace, and put it in the forever loop.  You'll notice that your Gemma emulator now has a strip of LEDs that are playing a rainbow animation.  

Play around with the other canned effects and see if there's one you like.  If so, you're done!  Skip ahead to download the code onto your Gemma and get rolling. 

If you're like me, and you want to customize a bit more and get exactly the effect you want, read on...

Photon Twinkle Rainbow

We'll use the Photon functions (also under the LIGHT tab, just scroll down a bit) to create some custom animations.  Photon is a very clever tool that's super hackable.  I keep on discovering that I can create new fun effects whenever I play with it.   

For this project, I want a slowly shifting rainbow animation that sparkles with LED "glitter".   We'll use variables to change the color and to make the glitter sparkles appear randomly throughout the crown.

Find the forever loop in your workspace. Whatever you put in this loop will run over and over, forever.  Drag an instance of photon set pen hue and photon forward by 1 into your forever loop.  

You can see the animation you've created in the emulator on the left, but it's a little hard to tell what's happening.  So this might be a good time to download the code onto your Gemma so you can see what it actually looks like in real life.  You can do this as many times as you want, until you get it looking just exactly right.

Start by giving the file a name and saving it to MakeCode.  I called mine "Fairy Crown".  Then click the Download button, and plug your Gemma into your computer via the USB port. 

 

Click the Reset button on the face of the Gemma and a drive will appear on your computer called GEMMABOOT.  This means it's in bootloader mode and you can program it.  Drag the file you just downloaded onto that drive.

Another way to tell if Gemma is in bootloader mode is that you'll see a red and green light on the Gemma's face.  Once you've downloaded the code to Gemma, the light will be magenta and the code will be running.  To get back into bootloader mode for another download, just click the reset button again.

Troubleshooting

If the GEMMABOOT drive is not appearing, first check to be sure that the on/off switch on the face of the Gemma is switched on.  It's easy to miss! 

Next, try using a different USB cable.  Some cables are "charge-only" and won't pass data.  Also try using a different USB port on your computer (mine is sometimes twitchy).  

If it's still not working, head over to the Gemma M0 intro guide for more suggestions.

If you can drag the code onto GEMMABOOT but your LED strand isn't lighting up, check to be sure that your code reflects the pin you soldered to (did you remember to change from D0 to D1?).  Also be sure you've soldered your connector to the IN end of the LED strand -- it won't work if you soldered to the OUT end.

Customization

Once you've got it working, Let's go ahead and add some customizations!  Right now it's a nice orangey golden color because we gave it a hue of 10. Changing the value of hue will allow us to choose any fully saturated color in the rainbow.  0 is red and 255 is .. well, red, again, coming full circle.  Play with the hue to select a color you like.  My favorite yellow is around 60, green is 100, blue 160 and purple is 200.

Since we can express the color with one simple number, it's easy to tell the code to adjust this number up or down on-the-fly, giving us a rainbow that will shift and rotate at the speed we decide.  To do this, we'll make a variable.

Click the VARIABLES tab and make a new variable.  Call it hue.  Drag this new variable into your workspace and use it to replace the 10 in the set hue to 10 code snippet.  Then, drag an instance of change hue by 1 into your forever loop at the bottom.

Download the code again and see what happens on your LED strip.  Now, each time Photon runs, you get a new color!  Play with the change hue byvalue to make the colors change faster or slower.

Let's make it a little more interesting by adding some randomness.

Go into the MATH tab and find pick random 0 to 10.  Drag that into your workspace and use it to replace the 1 in photon forward by 1.

 

Download the code again to see what happens.

Well, it's a glittery rainbow all right.  But it's a bit frantic.  Maybe we can slow it down a bit.  

Head over to the LOOPS tab and choose pause 100 ms.  Drag it into your forever loop at the bottom.  Download the code again.  Better?  Definitely better... but the twinkles happen pretty regularly.  I think it would look better if we also randomized the timing of the sparkles.  

We know how to do that.  Grab another instance of pick random from the MATH tab (or you can just copy and paste the one you have onscreen already).   This will look for milliseconds, so 0 to 10 will probably be too fast.  I did a little trial-and-error testing and found that I liked 100 to 200 ms for a good randomized twinkle.

This is starting to look pretty cool!  But let's explore one more idea to make it even more twinkly.  Right now the Photon animation always starts from the beginning of the strand and goes in one direction.  We can add a photon flip to make it go the other way, sometimes, which will hide the mechanics of the animation even more.  I like twinkles that go both ways around my head better than just one way.

Head back to the LIGHT tab and scroll down until you find photon flip.  Drag it into the end of your forever loop.  Now the direction of the photon will switch every time it runs.  Download the code again.  

I'm happy with this -- I was going for a slowly shifting, twinkling rainbow and that's exactly what we've got.  Customize and tweak the variables and code until yours makes you happy too.

It's time to get crafty.  I went to my local craft store and got a garland with some bells and twigs and leaves on it.  I wound it around a large foam wig head fairly loosely -- it's easy to squish the crown and make it smaller to fit, but making it bigger is tough so leave yourself some room. 

Wind the garland around on itself until you have a base crown.

Position your Gemma at the back of your crown.  Wind the LED strand around and around, ending up with the lights as evenly spaced as possible around the crown. 

 

Ideally you'll come to the end of the strand when you've circled around to the Gemma again.  It's best to try and position most of the lights so they're facing up or outwards.  The ones facing inwards towards your head will get lost. 

I added a large clear gemstone fro the craft store to the front center of the crown.  I positioned one of my lights directly behind it at the top and hot glued it in place so that it illuminates the whole jewel.  Then, I glued a smaller silver-backed jewel right over where the light shone through, to help reflect and diffuse the light.  

Use floral wire to attach lots of texture.  Large flowers, small flowers, silvery branches, bells, antlers, or whatever catches your eye.  There is an entire aisle full of this stuff at most craft stores around the holiday season.  More is more.  Go nuts.  

Wrap a rubber band around your battery pack and your Gemma to attach it to the back of the crown, hidden among the flowers.  If you're using the on/off switch JST extender, hide it in there too.  Be sure you can access the battery connector for charging.

Finish up by gluing jewels, snowflakes, glitter and anything fancy you can find in your crafting drawer.  Make it special!  Make it yours.  Brighten up everyone's day whenever you wear it.

This guide was first published on Dec 30, 2018. It was last updated on Dec 30, 2018.