The trick-or-treaters clamber up the steps of the next house on their evening route. The porch light is on, but no one comes to the door.

What's this? On a small table, a spooky-looking cardboard box sits next to a lamp. A sign on the lamp says "Touch Here for a Treat."

A small but brave goblin reaches out her hand, but before she can touch the lamp, an electronic disk lights up with a ghoulish orange smile, haunting notes play, and the box springs open as if to laugh at the foolish children.

But perhaps not as foolish as they seem ... for inside the box are spider rings and creepy crawlies for the taking. They each take a treat and move on in their quest to fill their Halloween bags.

This spooky laughing box comes to life thanks to a Circuit Playground Express, a servo motor, and a power source. The project is easy enough for kids to do themselves (with a little help cutting), and you can decorate it any way you choose.

The CPX's onboard light sensor is simple to program using MakeCode software to trigger the lights, music, and motion. If you want, you can add extra features like a mouth-opening mode or change up the eerie tunes. Keep reading to find out how!

Parts List -- Electronics

The Circuit Playground Express and a servo are the only electronic parts in this project, along with the things you need to make them run, like a USB cable and a battery pack. Depending on the size and weight of your cardboard box, you may be able to use a micro servo to lift the lid. But if your box is large or heavy, try the more powerful standard servo.

Circuit Playground Express is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and...
In Stock
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...
In Stock
This high-torque standard servo can rotate approximately 180 degrees (90 in each direction). You can use any servo code, hardware or library to control these servos. Good for beginners...
In Stock
When working with unusual non-header-friendly surfaces, these handy cables will be your best friends! No longer will you have long, cumbersome strands of alligator clips. These...
Out of Stock
This battery holder connects 3 AA batteries together in series for powering all kinds of projects. We spec'd these out because the box is compact, and 3 AA's add up to about...
In Stock

Materials List -- Household and Crafts Supplies

Other supplies you will need include:

  • 1 oblong cardboard box with a lid -- an Adafruit box works well!
  • (optional) extra smaller box
  • extra scrap cardboard
  • (optional) craft stick
  • adhesive such as dots or strips, foam tape or hot glue
  • masking tape
  • (optional) peel-and-stick Velcro dots
  • (optional) peel-and-stick craft foam
  • crafts supplies and/or holiday decorations (eyeballs, plastic creepy crawlies, pipe cleaners, construction paper, etc.)

The Monster Box set-up consists of the scary box itself and a control box.

Inside the Monster Box is a servo motor that has an arm which swings up to open the lid.

The control box holds the Circuit Playground Express and a battery pack. This will help protect the electronics and make it a little easier to arrange the elements of your light-sensor-triggered box where you want them.

Prepare the Lid

To prepare your box, make sure the lid lies as flat as possible. If it keeps popping open a little, try bending it back and forth along the fold line.

You can use also try scoring along the fold line by making an indentation along it with a rounded point (such as a dried-out ballpoint pen or the handle of a spoon).

Next, to make the teeth, sketch out a zigzag line along the inside of the lid's edge. You can make the teeth jagged, but keep the tops of the teeth even along one "gum line" for the best effect.

When you like your design, cut the teeth out.

Prepare the Servo

If you are using the micro servo, it helps to cover the labels on the sides by wrapping some masking tape around the body of the servo. Otherwise the labels have a tendency to peel off when you try to glue the servo down. This also makes it easier to remove the servo if you wish when you're done with the box.

Attach the Servo Horn

Find the one-armed servo horn and press it firmly onto the top of the servo.

Use the servo horn like a handle to turn the shaft of the servo motor counterclockwise as far as it will go.

Then remove the horn and reposition it so it lines up with the body of the servo. It should now swing in a complete 180 degree arc from front to back.

(If you are using the standard servo or another servo that doesn't have a one-armed horn, use the smaller round servo horn, or any shape that lets you lay the servo flat on one side.)

Make an Extension For the Servo Horn

Use a piece of scrap cardboard to make an extension that can push the lid open. To make it tall enough, you can glue the cardboard to a wooden craft stick, like a flag on a pole.

Then glue the extension onto the servo horn so that it will point up when it is attached to the box.

Attach the Servo to the Box

Lay the servo in the box so the end with the wire is pointing towards the back. The extension you connected to the servo horn should lie flat along or just above the bottom of the box, pointing towards the front.

Make sure the extension isn't keeping the lid from closing. If it's too tall, trim as needed.

Use adhesive dots or other tape or glue to attach the servo securely to the bottom of the box.

Add a Hole to Thread the Wires Through

Mark a spot in the back of the box near the servo wire. Carefully cut out a hold big enough to thread the wire through.

Attach the CPX and Battery Holder to the Control Box

Take the smaller control box and hold the battery holder against one side so that the on-off switch presses into it. Cut a hole around that spot big enough to let you slide the on-off switch back and forth. Attach the battery case to the side with adhesive dots, lining up the switch with the hole.

Attach the CPX to the Top of the Control Box

Press two matching Velcro dot halves together. Attach one side to the back of the CPX. Position the CPX on the top of the box and press down so the other Velcro half sticks to the box.

Connect the CPX to the Battery Case

Cut a hole through the top of the box near the JST plug.

Thread the wire from the battery case through the hole. Plug the wire into the CPX.

Now that you've set up your box, you can program it with Microsoft MakeCode for Adafruit!

MakeCode is a web-based code editor designed for beginners that is amazingly simple to learn. You can use it online -- there's no need to download anything. You can build programs quickly with drag-and-drop blocks, similar to Scratch. And best of all, it displays a simulated Circuit Playground Express so you can get an idea of how your program is working before you download your code to the real CPX board.

If you're new to MakeCode and CPX, you can learn more by checking out the Adafruit MakeCode Guide.

What the Code Does

The MakeCode program for this project does several things:

  • Sets up the light sensor so it can trigger actions when the available light gets dimmer;
  • Makes the NeoPixel light ring on the CPX board flash in the shape of an orange face, like a jack-o-lantern;
  • Plays an eerie tune;
  • Makes the servo swing back and forth so the "mouth" of the monster box looks like it's jittering with laughter.

These instructions will take you through a basic version of the program. (You can open it online to look at or download by clicking the button below.)

To personalize your code, see the Trick Out Your Spooky Box page for ways to add other outputs!

Adjust the CPX Settings

To begin, use an on start block from the LOOPS menu to adjust the settings for the servo, speaker, and light sensor. For each one, you will mouse over the white ovals to open up sliders that let you set the values where you like.

Set Up the Servo

First, to find the servo blocks, click on ADVANCED in the menu column and scroll down to EXTENSIONS. When you click on it, it takes you to another page with several choices. Click on SERVOS and it will be added to the menu column. Add a set servo A1 to [90] block. Use the slider to change the 90 to a number that will bring the servo arm down to the bottom of the box. For the sample used here, 160 degrees was the best setting.

Set Up the Speaker

To make sure your eerie music can be heard above the noise of Halloween night, grab a set volume block from the MUSIC menu and raise the level as needed.

Set Up the Light Sensor

The light sensor on the CPX detects changes in the relative light level, so it helps to set it to the actual conditions around it.

From the INPUT group, add a set [dark] light threshold to block and a set [bright] light threshold to block. Since it will probably be dark where your box is, adjust the numbers to specify low levels of light and dark. (You can fine-tune them later when you test out the code on the CPX.)

With those set, you can use the on light dark block to tell the CPX to make the box do its thing.

Program the Actions

Once the CPX is set up, you can write one or more stacks of code that tell it what to do. In this case, you'll build a stack of code blocks inside an on light [dark] block from the INPUT menu to instruct the CPX to flash its lights, play music, and move the servo.

Coding Tip: It's easier to test your code on the screen (and with the actual box) if you use a simple input, like Button A. When you've got the code working, just make a duplicate of that stack and change the input on it to the light sensor.

Make the NeoPixels Flash in a Face Design

To make the pumpkin face in lights, drag a show ring block from the LIGHT menu. Click on all the lights on the left side for the smile. Then click on two lights on the right side for the eyes.

To make it flash, drag a set all pixels to [red] block underneath the show ring block, and change the red to black. This will turn all the lights off. Drag a pause block from the LOOPS menu and set it for the number of milliseconds you want the pause to last.

Then right-click on the first show ring block and choose Duplicate. Drag the second show ring block under the pause.

Don't forget to add another set all pixels to [black] block at the end of the entire stack to turn all the lights off, so it's ready for the next time it's triggered.

Play Some Music

The easiest way to add music is to choose a premade tune from the MUSIC menu. Drag a play sound [ba ding] until done block under the light block. Click on the small down arrow and pick a melody from the drop-down list. Power Down works well.

Program the Servo

To make the mouth open and close, you need to make the servo arm swing back and forth. You already set it to lay flat when the program starts. Use another set servo [A1] angle to [90] ° block to move the servo up until the lid is pushed open as far as you would like it to go. In the example project shown here, 90 degrees was the right setting for the "lid open" position.

A pause block from the LOOPS menu is inserted after the set servo block, to give the servo time to complete the movement before the program goes on.

To create a laughing effect, you want the mouth to open and close several times in a row. Once you've decided on your "open mouth" and "close mouth" settings, you can just repeat them (with pauses in-between) by placing them inside a repeat [4] times block.

However, varying the amount the mouth opens and closes each time makes the box a little more life-like. So the sample code shown here has a slightly different amount each time that is somewhere between the widest open setting and totally shut.

After you download your code and test it, play around with the number of degrees the servo arm pushes the lid open, as well as the timing of the pauses, to get your box to laugh just right!

Download MakeCode to the CPX

Before you download the program to your CPX, give your file a name! It will make it easier to find. You can also download the file to your hard drive or save it as a link to open online later or to share.

To copy the code to the CPX, connect it to your computer with the micro USB cable. Your computer will treat the CPX the same way it does a flash drive, as an extra piece of hardware for storing files. It will show up as a drive called CLAYBOOT. (You can ignore any pop-ups.)

To tell the CPX to get ready to receive code, you need to put it into bootloader mode. Press the tiny Reset button in the middle of the board until all the lights turn red for a second and then green. You may have to press the button a few times the first time you use the CPX, or whenever it doesn't go into bootloader mode the first time.

Then, click the pink Download button on the MakeCode page. It may copy the file to the Downloads folder, or whichever location you last saved a file to. If it's not already on the CPX, drag and drop (or copy and paste) the file into the CLAYBOOT drive.

If you get a drive named CIRCUITPY, no worries! Press the button twice quickly to change that to CPLAYBOOT. CIRCUITPY is the CircuitPython drive and your code will not work if dragged there.

Now it's time to hook up the servo motor to the CPX and test out your code!

The servo has three wires joined to a 3-pin female connector. Plug the male header pins on the ends of the alligator clip wires into this connector. To avoid confusion, choose wires that match the color of the servo wires.

Next, attach the alligator clips to the CPX by clamping them onto these holes around the edge of the board:

  • Connect the red or orange middle wire from the servo to 3.3V on the CPX (or you can connect it to Vout)
  • Connect the black or brown wire from the servo to GND
  • Connect the yellow wire from the servo to A1

A simple way to remember this:

  • The red/orange wire carries power from the CPX board to the servo. (The V on the 3.3V pin stands for "voltage.")
  • The black/brown wire is the ground wire that completes the circuit so current can flow. (GND means "ground.")
  • The yellow wire will carry the signal from the pin that you program -- in this case, Pin A1 -- so the CPX can tell the servo what to do.

If the servo wires don't reach the CPX, use female/male extension jumper wires to make them longer.

Check the Lid Opening Mechanism

Test out your code to be sure it does what you want it to.

One issue you may find is that the servo arm extension hits the lid with a loud noise and the lid bounces.

You can dampen both effects by putting some padding on the lid where the arm hits it. A piece of peel-and-stick craft foam works well.

Display Suggestions

To make the box open, you want the person to move their hand over the CPX. This will block the light, telling the light sensor to trigger the servo, sound, and lights.

For best results, you may want to place a desk lamp pointing down towards the CPX. That way, you'll know there will be a measurable difference between the light and the shadow of someone's hand.

How can you trick someone into reaching out over the CPX without tipping them off that something is about to startle them? One idea is to draw a fake-looking button and write "Press Here" on it. Tape it to the lamp or somewhere that will make the person reach across the control box and block the light. They probably won't even reach the button before the lid flips open and the CPX starts playing its song!

Once your hardware and software are working nicely, jazz up the box -- and even teach it new tricks! How far you take it is up to you.

Give your spooky box some personality with a few decorations! Be sure to attach everything to the box as securely as possible, as things will be bouncing around a lot.

Eyeballs look nice perched on top.

Pipe cleaners can be bent into arms and added to the sides. They jiggle when the lid moves for extra animation. You can staple the ends onto the box and cover with tape.

Finally, fill the box with light-weight, non-sticky creepy crawlies. The example here uses the ever-popular spider rings, with a few centipedes and roaches thrown in for variety.

If you're concerned about them interfering with the servo, add a wall made of scrap cardboard to keep the treats to one side.

Make it Do More with MakeCode

Doing the same thing all the time is boring! Mix it up a little by adding extra behaviors to your spooky box. The code shown here has three modes:

  • Mode #1 -- Laughing: The box laughs like in the previous code. The melody is made up of notes chosen to sound a little like cackling. It was created with a stack of play tone at [Middle C] for [1/2 beat] blocks from the MUSIC menu, which let you change the note played by choosing from a pop-up piano keyboard (or typing in the corresponding frequencies).
  • Mode #2 -- Dies Irae: The box doesn't open -- gotcha! -- but the lights flash in a premade pattern using the show animation [sparkle] for 500 ms block from the LIGHT menu while the speaker plays a well-known ominous theme called Dies Irae. It was created using a relatively new block in the MUSIC menu called play melody [] at tempo [120] bpm that lets you select eight notes from a colorful grid. Try it, it's fun!
  • Mode #3 -- Open-Close: The lid flies up and stays open for a few seconds (so visitors can grab a spider ring from its maw), then slowly closes. A premade light animation, premade musical phrase, and a stack of individual notes were added to make it easier to tell when the code was running on the screen.

Using some more advanced coding techniques like variables and functions, the three modes play in sequence. The first time the box is triggered, Mode #1 runs. The second time, it switches to Mode #2. And the third time, Mode #3 runs and the code resets to start over the next time with Mode #1.

Take a look at the code and try it out at the link below. Then play around with it to figure out how it works and make it your own.

Happy Halloween! Bwah-ha-ha-ha-hah...

This guide was first published on Oct 08, 2019. It was last updated on Oct 08, 2019.