Overview

This guide shows how to turn just about any box with a lid into a customizable music box.

This music box begins playing music as soon as it's opened, and the user is greeted with a dancing figure (for this project, rather than a slowly turning ballerina, an Adabot takes center stage). When closed, the singing and dancing automatically shuts off.

Circuit Playground Express

PRODUCT ID: 3333
Circuit Playground Express is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and...
$24.95
IN STOCK

Adafruit CRICKIT for Circuit Playground Express

PRODUCT ID: 3093
Sometimes we wonder if robotics engineers ever watch movies. If they did, they'd know that making robots into slaves always ends up in a robot rebellion. Why even go down that...
$29.95
IN STOCK

USB cable - USB A to Micro-B

PRODUCT ID: 592
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or...
$2.95
IN STOCK

Micro servo

PRODUCT ID: 169
Tiny little servo can rotate approximately 180 degrees (90 in each direction), and works just like the standard kinds you're used to but smaller. You can use any servo code,...
$5.95
IN STOCK

AdaBot LunchBox

PRODUCT ID: 3437
Your eyes do not deceive you – this is absolutely an AdaBot lunchbox! Whether you're sporting this at school, work, Maker Faire, or jury duty, this...
$9.95
IN STOCK

Mini Oval Speaker - 8 Ohm 1 Watt

PRODUCT ID: 3923
Hear the good news! This wee speaker is a great addition to any audio project where you need 8 ohm impedance and 1W or less of power. We particularly like...
OUT OF STOCK

Materials & Tools

To build this project you'll need:

Let's get to work!

CircuitPython Code

Getting Familiar

For this project, we will be using CircuitPython.

CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards.

CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.

If you haven't used Circuit Playground Express with CRICKIT before, make sure you've updated it with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.

Setting Up

To get your CRICKIT for CPX set up to run this code, follow these steps:

1) Make sure you've updated the CircuitPython firmware for CRICKIT from the CRICKIT guide

2) Get the latest library pack, unzip it, and drag the libraries you need over into the /lib folder on CIRCUITPY
https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/

For this project you will need the following libraries:

  • adafruit_crickit.mpy
  • adafruit_seesaw folder
  • neopixel.mpy
# Music Box code in CircuitPython - Dano Wall and Mike Barela
# Revised by Ladyada 2019-01-16

from adafruit_crickit import crickit
from analogio import AnalogIn
import neopixel
import audioio
import board

AUDIO_FILENAME = 'fur-elise.wav'

# Audio output
cpx_audio = audioio.AudioOut(board.A0)
audio = audioio.WaveFile(open(AUDIO_FILENAME, "rb"))

# Rotating dancer
dancer = crickit.servo_2
dancer.angle = 0
MAX_SERVO_ANGLE = 160
move_direction = 1

# neopixels!
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1)
pixels.fill((0, 0, 0))

# light sensor
light = AnalogIn(board.LIGHT)

def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return 0, 0, 0
    if pos < 85:
        return int(255 - pos * 3), int(pos * 3), 0
    if pos < 170:
        pos -= 85
        return 0, int(255 - pos * 3), int(pos * 3)
    pos -= 170
    return int(pos * 3), 0, int(255 - (pos * 3))

def rainbow(value):
    for i in range(10):
        pixels[i] = wheel((value * i) & 255)


while True:
    # turn off LEDs so we can tell if its dark out!
    pixels.brightness = 0
    # read light level
    light_level = light.value
    # turn LEDs back on
    pixels.brightness = 1

    # Turn things off if light level < value, its dark
    if light_level < 2000:
        pixels.fill((0, 0, 0))
        cpx_audio.stop()
    else:
        if not cpx_audio.playing:
            # Start playing the song again
            cpx_audio.play(audio)
        # calculate servo rotation
        if dancer.angle <= 0:
            move_direction = 1
        if dancer.angle > MAX_SERVO_ANGLE:
            move_direction = -1
        # Move servo one degree forward or backward.
        rainbow(int(dancer.angle * 255/MAX_SERVO_ANGLE))
        dancer.angle += move_direction

Uploading

Make sure you've connected the Circuit Playground Express to your computer with a micro USB cable and have Mu open and connected to your board.

Copy and paste the code above into your Mu window.

Once the code is copied into Mu, press the Save button - your code should be saved to the CIRCUITPY disk drive (which appears when the Circuit Playground Express is plugged into your computer) as code.py.

Make sure the file saved to CIRCUITPY is named "code.py", this will allow it to run automatically when your CPX is powered on.

Now it's time to give your music box a song to sing. This free version of Fur Elise is a good one for the music box (courtesy of freesound.org). 

Download the WAV file, the drag and drop that WAV file onto your CIRCUITPY drive.

When you're all done, your CIRCUITPY drive should contain the code.py file, a .wav file, and a /lib folder with the necessary libraries in it.

Troubleshooting

Problem: My Circuit Playground Express isn't recognized by Mu!

Solution: Make sure your board is set up with CircuitPython, which has the Circuit Playground Express show up as a flash drive named CIRCUITPY when you connect the CPX to your computer. If it is showing up as CPLAYBOOT on your computer, you can follow the steps in this guide to ensure CircuitPython is loaded and you see the CIRCUITPY drive.

Complete the Circuit

Once the code is uploaded, it's a good idea to connect the motor and speaker to make sure they work before moving on to the final build.

In this example we are using the mini 8Ω, 1 Watt oval speaker to play audio, but the powerful Class-D amplifier on the Crickit will allow you to use most any speaker you like for this project (see this guide for a list of recommended speakers). 

Speaker Wires

 

If your speaker has a connector at the end, cut that connector off so the ends of the wires are free to be screwed into a terminal block.

 

Use wire strippers to remove a small length of insulation from the end of each wire.

Making the Connections

 

Connect servo motor to port 2.

 

The lightest color servo wire (like yellow) should face outwards, the darkest color (like brown or black) should face the Circuit Playground Express.

 

Connect speaker to speaker port using a small screwdriver to tighten the terminal connections.

When connected to 5V power, you should see the servo, NeoPixels, and speaker come to life when the CPX is exposed to bright light. 

Troubleshooting

Problem: My servo isn't moving!

Solution: Check that a 5V power supply is connected and the slide switch on CRICKIT is set to "ON". The lightest color servo wire faces the center of Crickit, the darkest faces outwards.

 

Problem: My servo still isn't moving! 

Solution: Make sure you've updated your Circuit Playground Express with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.

Music Box Construction

This music box can be customized to star any character you like in the position of the dancer. For this version we are using Adabot, a PDF of which is available below.

Scissor Time

 

Cut out a picture of your character.. 

 

The mirror images will create a character that looks good from any angle.

Glue both sides of the character together, sandwiching a straw between them.

 

Press between the pages of a heavy book. Let sit for 10 minutes for glue to cure.

Servo Stand

 

To create a stand for the servo motor, fold the ends of a long rectangle of cardboard 90 degrees, creating a small platform with legs.

 

Glue the legs of this platform to a base using two strips of hot glue.

Mark the outline of the base of the servo motor on the top of the platform.

 

Be careful to try to center the hub of the motor in the middle of the platform.

 

Cut a hole for the servo motor and use a line of hot glue on either side to hold it firmly in place.

Spring time

 

Glue a spring from a pen to the hub of the servo motor. A small dab of hot glue should be enough to make a firm connection.

 

Attach a short piece of a wooden skewer to the top of the spring using another dab of hot glue.

 

Slide your paper character onto this wooden skewer using the straw sandwiched in the middle. This will allow the character to rotate if necessary, and make it removable if you'd like to add a different character to your music box.

Hold Fast

 

Use small squares of double sided foam tape to hold everything in place inside the box.

Adding an Insert

Adding some colorful image or scenery to the inside of the music box can accentuate the dancer and highlight the rainbow lights even further. Follow these instructions to make your own box insert.

Using a sheet of paper, cut out a rectangle approximately the same internal dimensions of the box you're using. 

 

Add an inlet to allow room for the top of the servo motor to poke through and trace a circle where the Circuit Playground Express will poke through. This opening will allow the light sensor on CPX to "see" its light environment and detect  whether the box is open or closed.

 

Once you have verified that the cutout in this first insert are where you want them, you can use it as a stencil for other images you'd like to put inside. 

These example images of Adabot and other Circuit Playground friends all make fun inserts for this music box, but let your imagination run wild here.

Customization

Custom Audio

If you want to provide your own audio file for this music box you can easily swap a new one in place of the one provided here.

Audio files can be gathered by any means you like, but they will all need to be formatted as mono 16-bit PCM WAV at 22,050 Hz to be recognized by CRICKIT.

See this guide on how to convert audio files

Making Adjustments

Depending on your location, you may need to adjust your music box's responsiveness to light. This can be done in Mu, by increasing or decreasing the default value of 2000 to 3000 (responds to bright light) or down to 1000 (more responsive to dim light). 

Exploring further

If you enjoy CircuitPython and want to continue learning you can find lots more CircuitPython projects on the Adafruit Learn System.

This guide was first published on Jan 17, 2019. It was last updated on Jan 17, 2019.