Unicorn Hat with Moving Ears
2017-08-24 | By Adafruit Industries
License: See Original Project
Courtesy of Adafruit
Guide by Erin St Blaine
Introduction
Turn yourself or someone you love into a magical animatronic rainbow unicorn.
The little girl in your life will very likely refuse to ever take this hat off. (But you can wear it yourself while she's sleeping. We won't tell anyone).
A joystick in the pocket controls servos inside the ears, and the Circuit Playground's onboard LEDs animate the magical glowing rainbow unicorn horn.
We've kept this project fairly simple - it's a great first step into the world of robotics and servos. This technique will work on any animal hat with attached ears, so if your inner animal is a panda or a fox or some other critter, go nuts. The world is your animatronic playground.
Parts List
- 1 x Circuit Playground Classic (The Amazing Circuit Playground Board)
- 2 x Micro Servos (Tiny motors to drive the ears)
- 1 x Joystick (Analog 2-axis Thumb Joystick with Button)
- 1 x Silicone Stranded Wire (26 AWG wire in 3-4 colors)
- 1 x Perma-Proto Breadboard (Half Size Breadboard)
To use standard AA batteries (safer, recommended if this will be worn by a child):
- 1 x AA Battery Holder (4xAA Battery Holder with Switch)
or, for rechargeable power (longer life but a slight fire hazard, use with knowledge of the risks):
- 1 x LiPoly Battery (Rechargeable 2500 mAh Battery)
- 1 x On/Off Switch (Battery Extension Cable with On/off switch)
- 1 x Battery Charger (Charger for LiPoly Battery)
Other Materials
- Hat with Ears and pockets: I used a Magicorn Hat from Smoko
- 3mm Light Pipe: I used LED shoelaces from Flammi
- 3D Printed Circuit Playground case: download and print your own or order from Shapeways
Tools
- Soldering iron & accessories
- 3D Printer (optional)
- Heat Gun
- Hot Glue gun or E6000 glue
- Needle & thread
Code
Before You Start
If this is your first foray into the world of Arduino-based microcontrollers, you'll need to install some software first. Head over to the Circuit Playground Lesson 0 guide for detailed installation and setup instructions.
You'll only need to do all this once!
TiCo Servo Library
You will also need to install the TiCoServo library in Arduino (Sketch > Include Library > Manage Libraries...)
This library by Phil Burgess makes it possible to use servos and NeoPixel LEDs at the same time, driven from one Arduino board. Head over to the TiCoServo LIbrary guide to learn more about why we need this, and how it works.
Upload Code
Once you've got everything installed and your computer can talk to the Circuit Playground, it's time to upload the code.
Plug your Circuit Playground into your computer and select the Circuit Playground under Tools > Boards. Then select the Circuit Playground as the Port.
Copy and paste this code into a new Arduino window and click "upload".
/*------------------------------------------------------------------------
Unicorn Hat sketch for Circuit Playground
Reads a potentiometer on pin 10, moves servos (pin 9)
------------------------------------------------------------------------*/
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_TiCoServo.h>
// Pin number for joystick. Only the "right side" pads work w/analog!
// And the numbering is unusual: for the pads labeled 12, 6, 9 and 10,
// set CONTROL_PIN to 11, 7, 9 or 10, respectively.
#define CONTROL_PIN 10
// Servo parameters. On Circuit Playground, only pins 9 and 10 are
// supported by the TiCoServo library.
// Servo position can be specified in degrees or in microseconds; library
// can distinguish between the two. The #defines below are reasonable
// min/max pulse durations (in microseconds) for many servos, but for
// maximum control you'll probably need to do some calibration to find
// the optimal range for your specific servos.
#define SERVO_PIN 9
#define SERVO_MIN 1000 // 1 ms pulse
#define SERVO_MAX 2000 // 2 ms pulse
// set currentSpeed to 0-4 to change the horn animation speed
static int speeds[] = { 5, 10, 50, 100 };
int currentSpeed = 1;
Adafruit_TiCoServo servo;
void setup(void) {
servo.attach(SERVO_PIN, SERVO_MIN, SERVO_MAX);
CircuitPlayground.begin();
// Make it bright!
CircuitPlayground.setBrightness(255);
}
void loop() {
// Servo control
int a, x;
a = analogRead(CONTROL_PIN); // 0 to 1023
x = map(a, 0, 1023, SERVO_MIN, SERVO_MAX); // Scale to servo range
servo.write(x); // Move servo
// Horn light control. Make an offset based on the current millisecond count scaled by the current speed.
uint32_t offset = millis() / speeds[currentSpeed];
// Loop through each pixel and set it to an incremental color wheel value.
for(int i=0; i<10; ++i) {
CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.colorWheel(((i * 256 / 10) + offset) & 255));
}
// Show all the pixels.
CircuitPlayground.strip.show();
}
If all goes well, the lights on the Circuit Playground will come on in a rainbow cycle pattern. You can change the speed of the LED animation from within the code. Look for this line:
// set currentSpeed to 0-4 to change the horn animation speed
static int speeds[] = { 5, 10, 50, 100 };
int currentSpeed = 1;
Change currentSpeed to a number between 0-4, and watch for the difference in the neopixels.
You may also need to calibrate your servo response time. Look for these lines:
#define SERVO_MIN 1000 // 1 ms pulse
#define SERVO_MAX 2000 // 2 ms pulse
Servo position can be specified in degrees or in microseconds. The TiCoServo library can distinguish between the two. The #defines in the code are reasonable min/max pulse durations (in microseconds) for many servos, but for maximum control you may need to do some calibration to find the optimal range for your specific servos.
The code as-written works great for the micro servos recommended in this guide, but play with these numbers once you've got everything hooked up to change the range of motion for your ears.
Wiring Diagram
Servo Wiring
To keep this project simple, the two servos are wired to the same pins. They'll be synchronized and always move together.
Joystick Wiring
Connect the joystick to the X-out pin for a left to right control action. If you'd prefer up-and-down action, use the Y-out pin instead.
Wiring Constraints
The TiCoServo library limits which pins we can use for servos: pins 9 or 10 only. Additionally, analog inputs (like the joystick) can only be read on Circuit Playground's "right side" pins: 12, 6, 9 or 10. Two of these overlap the servo-compatible pins, so it's really just pins 12 and 6 that remain.
If you're planning to use more than pins 9-10 take note: Digital pin 12 is called A11 and digital pin 6 is called A7 so be sure to write your code appropriately.
More about Circuit Playground's Pinouts
With some planning, it could be possible to control the ears independently and add lots more "expressions" in the finished project. However, for the scope of this intro project, we've chosen to keep things simple, and have just one left-to-right control for both ears. It's a one-trick pony.
As you can see, though, it's still incredibly effective and fun! See where your imagination takes you.
Layout
This hat has two little attached pockets which are perfect for hiding the joystick and the battery. I recommend placing the joystick on the side with your non-dominant hand, since I've found it harder to sneak my dominant hand away to control the ears, as it's often busy holding things.The Circuit Playground rests right under the horn so we can use its onboard NeoPixels for magical rainbow light.
3D Printing
Download .stl from Thingiverse
The case prints in three pieces: the cover, the lid ring, and the base.
Download the files and print them out one at a time in white ABS or PLA. You won't need to use any supports.
Or, if you don't have access to a 3D printer you can order a pre-printed case from Shapeways.
Temporarily attach the cover to the lid ring. Put your Circuit Playground inside the base, and screw the lid on.
Then, adjust the cover so that the holes align perfectly with the NeoPixels when the case is fully screwed closed.
Once you've got it perfect, glue the cover to the lid ring.
Joystick Assembly
Place the joystick on top of the breakout board -- it only fits one way.
Flip the board over and solder all the connections on the bottom side.
Solder the header pins into the breakout board. It's easiest to do this if you have a solderless breadboard to hold everything in place.
Solder the header into a perma-proto breadboard, centering the joystick left to right but aligning it with the top edge, leaving a few pins open below the breakout board.
Solder a very long wire into the VCC, G, and Xout pins. (Photo shows a wire in each of the 5 pins, but you only need those three for this project). These wires will need to reach from the pocket to the Circuit Playground, so give yourself plenty of slack.
Put the hat on and put your hands in the pockets. Decide where you'd most like the joystick to be and make a small hole in the lining of the pocket for it to poke through.
Cut a larger hole in the edge of the pocket lining and slip the breadboard assembly inside. Poke the mechanism through, then snap the plastic joystick onto the whole assembly.
Stitch the breadboard in place using the mounting holes on either end.
Carefully cut a hole in the lining of the hat right at the crown of the head that's big enough to give yourself room to work.
Thread all the joystick wires up through the lining to the top of the head where the Circuit Playground is going to rest.
Temporarily attach the wires to the Circuit Playground with alligator clips, so you can test the servos in the next step.
Servo Ears Assembly
Carefully detach the unicorn horn and the ears from the hat with a thread ripper.
Cut some craft foam the same shape as the ear but a little smaller. We'll use this to attach to the servo.
Give the craft foam a bit of character with a curved shape and stitch it to the servo attachment piece.
Slip the servo and foam inside the ear and sew it closed across the bottom.
Repeat with the other ear.
Attach some alligator clips to the three wires and hook them up to the Circuit Playground. Move the ear back and forth with the joystick and play around with proper placement on your hat. I ended up adding another little stitch to make my ears flop over a bit more.
Once you're happy with the placement, sew and/or glue the servo in place on the hat, hiding it inside the fabric. Don't sew the EAR in place -- it should be free to move! Just attach the servo.
It's alive! Hooray! Go find someone and show off a bit.
Not done yet though… a magical animatronic unicorn must have a glowing horn. (I think that's some kind of law) Luckily our Circuit Playground comes equipped with lots of lights...
Unicorn Horn Assembly
Each neopixel on the Circuit Playground's face lights one strand of light pipe, creating a gorgeous animated twisty twirly rainbow. The code we included is a simple rainbow animation, and you can customize it however you like. There's plenty of space on the Circuit Playground to add lots of color modes or animations, and they'll pretty much all look amazing.
Sculpting the horn took a few tries to get right, so you may want to order a few extra lengths of light pipe, even if you're super crafty.
My horn ended up about 6" long, so with a little extra for the twists and to have enough to trim evenly below the lid, I used around 80" of light pipe. The Flammi shoelaces I ordered are about 30" long, so it took almost three full laces. They come in pairs, so be sure to order at least two sets.
Decide how tall you'd like your horn to be. Add a couple inches to that measurement and cut 10 pieces of light pipe to that length.
Thread the light pipe through the 10 holes on the Circuit Playground cover and hold them over the Circuit Playground to make sure all your light pipe is working with no kinks.
Pull each piece of light pipe through about another inch further than needed to reach the NeoPixels. When you twist the horn, the pipe will get pulled back out a little bit, so make sure you've got lots of extra. We'll trim it to the perfect length later.
Time to get artsy. Get out your heat gun and a pair of protective gloves.
Twist and gently heat the light pipe with your heat gun. It will hold its shape as it cools. Take your time and go slowly! If you get the light pipe too hot it will melt and warp and cease to carry light very well. Be patient!
I'm not very patient. After two or three discarded tries, I began using a tumbler of ice water for cold plunges, and had a bit more success.
To finish it off at the top, I trimmed each piece of light pipe slightly longer than the last in a spiral, and carefully melted them together. I left the very last piece extra long, then melted it with the heat gun and pulled, stretching it into a perfect point.
Use a sharp pair of scissors or snips to trim the underside of the light pipes to 3mm, so they just touch the neopixels when the Circuit Playground is encased in its 3d housing.
Power
The battery will live in the opposite pocket as the joystick. This makes the hat feel more balanced, and makes it easy to remove and change or charge the battery.
AA Battery Option
The simplest and safest option for power is using four AA batteries in this battery holder. When using alkaline batteries, the output will be around 6v. This is perfect for our project.
Add a couple feet of wire so the battery holder reaches one of the hat pockets. This battery holder doesn’t include a JST plug…you can either add one from the shop, or solder the + and – wires directly to Circuit Playground’s VBATT and GND pins.
You should be able to use either alkaline or rechargeable batteries in this holder, but keep in mind that rechargeables have a slightly lower voltage so if your servos don't respond as well, try switching to alkaline.
LiPoly Battery Option
For those who want a little more battery life without adding to our planet's overwhelming landfill problem, Adafruit sells these fabulous Lithium Polymer batteries. The 2500 mAh battery will give you 3.7+ volts for hours, and runs this project very nicely. The voltage is a little lower than recommended for servos, but we've found that it works just fine.
These batteries come with a protection circuit onboard, so you can run them down willy-nilly and recharge them hundreds of times, without worrying about the voltage dropping too low for recharging. They're smaller and lighter than AA batteries and easier to hide in costumes. Be sure to get a charger, and if you want to take your project to a weeklong festival with no power plugs, be sure to get a few batteries or get a AA battery holder as backup -- you can't find these at the corner store.
They don't have an on/off switch included, so you'll want to add one.
There are dangers that come with LiPoly batteries. If the batteries get punctured or torn, or if they get wet, there's a chance they can catch fire. You may not want to use LiPoly if you're making this project for a little girl, or for anyone who's likely to spill on/drop/engage in fisticuffs in this costume.
Battery Cable Extension
Cut the female end off the power switch and splice in about 3 feet of wire, enough to reach the Circuit Playground/horn assembly.
Cut a small hole in the pocket lining and thread the switch and wires through, leaving the male connector accessible to plug in your LiPoly battery.
Thread the female end inside the lining up to the Circuit Playground.
If you're using a LiPoly battery you can plug it directly into the other end of the battery extension cable. If you're using the AA option, cut off the male connector and connect the battery holder directly to the extension cable wires -- red to red and black to black.
Putting it All Together
Place the Circuit Playground inside the case bottom to be sure it fits and snaps neatly down onto the three pillars.
Remove it, and thread all the wires through the hole on the side of the case.
Solder all the wires in place according to the wiring diagram. You'll have 2 wires going to BATT, 3 wires going to G, and two going to pin 9. There will be one wire each on 3.3v and pin 10.
Note: It's a bit of a tight fit in the case, so it works best to solder the wires from the TOP of the Circuit Playground rather than the bottom.
Plug your battery in, switch it on, and be sure everything is working properly.
Slide the horn through the hole in the top of the hat and sew it in place. Wrap the threads around and between the light pipe strands rather than piercing them with your needle.
Finish by stitching any holes in the lining closed so that all wires are hidden.
Put your hat on and listen to the left. Listen to the right. Can you hear the magical rainbows all around you?
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum