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.
Materials & Tools
To build this project you'll need:
- Box with lid (such as the AdaBot Lunchbox)
- Paper and a printer
- Hot glue gun and hot glue
- Scissors
- Scrap cardboard
- Double sided foam tape
Page last edited March 08, 2024
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
# SPDX-FileCopyrightText: 2019 Dano Wall for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Music Box code in CircuitPython - Dano Wall and Anne Barela
# Revised by Ladyada 2019-01-16
from adafruit_crickit import crickit
from analogio import AnalogIn
from rainbowio import colorwheel
import neopixel
import audioio
import audiocore
import board
AUDIO_FILENAME = 'fur-elise.wav'
# Audio output
cpx_audio = audioio.AudioOut(board.A0)
audio = audiocore.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 rainbow(value):
for i in range(10):
pixels[i] = colorwheel((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.
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.
Page last edited March 08, 2024
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.
Page last edited March 08, 2024
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.
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.
Page last edited March 08, 2024
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.
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.
Page last edited March 08, 2024