Shake Detecting Talking Hat With Circuit Playground Express




Introduction: Shake Detecting Talking Hat With Circuit Playground Express

This easy and quick tutorial teaches you how to make a talking hat! It would respond with a carefully processed answer when you 'ask' a question, and perhaps it could help you decide if you have any worries or problems.

In my Wearable Tech class, I was given the assignment to incorporate the CPX (circuit playground express), my coding skills, and something you can wear into one project. And I thought, why not make a talking hat just like the sorting hat in Harry Potter? The hat would randomly generate an answer I recorded whenever the CPX detects a shake.


- Fisherman’s hat (or any kind of hat works)

- CPX (circuit playground express)

- AA battery (I used a rechargeable one)

- Clip

- Needle

- Beads


- Audacity application

- Voice Recorder (I used Quicktime player)

- mu-editor (Python editing program)

Materials you may or may need:

- External microphone

- Sequins

- Beads

Step 1: Buying a Hat

I wanted a fisherman’s hat, so I got it from H&M, but you can use any sort of hat style or brand you want to use.

Step 2: Brainstorming and Recording Answers You Want the Hat to Say

First, you have to record answers that your hat is going to generate. For instance, my hat is able to generate five answers, including “Yes,” “No,” “Maybe next time,” “I’ll take another question,” and “That’s okay.”

You can record these using any recording device you want, including your phone recorder, QuickTime player, GarageBand, etc. I used QuickTime player and an external microphone to record clearer and a better quality sound.

Step 3: Exporting the Audio File

Once you have recorded the audio file, you will need to change the audio file with .wav file using audacity. If you don’t have audacity program on your computer, then you could simply download it by googling audacity or following this link:

Then, you have to move the file into audacity program to edit the file. You have to first split the audio file from stereo to mono so it reduces the file size.

Here are steps on how to edit the file into .wav file.

Step 4: Export File From QuickTime Player to Garage Band

Once you have the recording saved in the desktop like the screenshot above, you will simply drag the file into GarageBand to export this into Uncompressed 16-bit AIFF file. This can be done by clicking the Share button --> Export Song to Disk as shown in the third picture. Then, click 'AIFF' and '16-bit CD' and save it into desktop again.

Step 5: Converting AIFF File Into .wav File

Since you have an AIFF audio file saved onto your desktop, you can then open Audacity and import the file. You could do this by simply dragging the AIFF file into it. If the warning sign appears, just click OK and proceed.

Then, an audio file that looks like the second screenshot above will appear. Since you have to split stereo audio into mono, click the downward arrow button next to the title of your audio file (in this case it's 'that's ok') and you will see the sign that says 'Split Stereo to Mono.' Click this. Your audio file will then be split separately into two.

Next, you have to delete one of the audio files since it's been split. This can simply be done by pressing the 'X' button on the left. This will leave you with something like the third screenshot.

You can then edit the file however you want, and if you're finished, click Export --> Export as WAV on top.

Afterward, just save it into your desktop. Repeat this process for every audio file you have recorded.

*Warning: Make sure you didn't record too many because the CPX has little storage and cannot fit all the audio files in.

Step 6: Downloading CircuitPython (if Needed)

Once you have followed these steps, you are now ready to code.

I am going to use python to code this program, so if you don’t have mu-editor on your computer, you should download it. You can download this by simply searching up ‘mu-editor’ on google and clicking the first website that shows up. You can also follow this website and download depending on your computer type.

If you’ve downloaded the program, open it. It will look something like the picture above. This is where you can write your codes and save it.

Step 7: Connection Between Python Program and CPX

Now, get your CPX and a USB cable out.

Connect the smaller part of the USB cable into the CPX, on the silver part as shown in the picture, and connect the bigger part of USB cable into your computer. Now you are really ready to code and every information saved onto your mu-editor will transfer to the CPX.

Step 8: Installing Circuit Python

Next, you will have to install the latest version of circuit playground express using this link:

This is the LAST installation you have to do, I promise. After this comes the fun part.

This file you just installed will copy to your CPX. CPLAYBOOT beeps and becomes CIRCUITPY. Whenever you connect the USB cable, CPX, and the computer together, this CIRCUITPY will appear.

Remember the wav. sound file(s) you converted before? Drop this/these file(s) onto the CIRCUITPY folder. Make sure that the sound file is 16-bit, mono WAV file.

Step 9: Coding!

In this tutorial, the CPX needs to do three things. One, it needs to detect or sense a movement. Two, it also needs to randomly generate answers, and three, it needs to play the file placed in the CPX. So code that makes this hat works needs to do all three.

Add the following code to your mu-editor, and save it as:

import time

import random from adafruit circuit

import cpx sounds=["that's-ok.wav"]

while True:

#play a random sound if shaken

if cpx.shake(shake_threshold=20):


#then pause for a few seconds


If we look at the code, first, we import the time. Then we import random for the random generator. In Python, “time” allows us to handle various operations regarding time, its conversions and representations. Then, it’s shown in the code that from this program the data will transfer.
Next to ‘sounds=’, in brackets and quotation should be the .wav file you dragged into your CIRCUITPY folder. Notice that you have to write down ‘.wav’ onto the code. Python cannot interpret special characters like _, :, ', and more, so make sure you’re putting dashes in between words if you need spaces between words. In this code, there’s only one sound file coded, so if you want to put more sound files, put them into the same format and name as saved into your computer. Remember, in order for the sound file to play, the sound files need to be placed into the CIRCUITPY folder!

The code below 'while True:' tells CPX to play a random sound if it detects a shake, and pause for a few seconds. The (.5) in the code shows how sensible the CPX is, so if you want it to be more sensible or less sensible, you can simply change the number. You can see the screenshot above if you want to double check the format.

Step 10: Final Coding: Part I

Let’s add some inputs and other .wav files. Drag other .wav files into CIRCUITPY folder before you start to code. This, was my final code:

import time

import random from adafruit circuit

import cpx sounds=["that's-ok.wav","maybe-next-time.wav","yes.wav","no.wav","Ill-take-another-question.wav"]

while True:

#play a random sound if shaken

if cpx.shake(shake_threshold=20):


#then pause for a few seconds


Now adjust the sound files according to yours and click the save button! Keep in mind, if you have a really long .wav file, you'll find that you can't do anything else until the file is finished playing. Keep that in mind if you're going to include .wav files with other code. It's also smart to check if you have enough storage in your CPX.

Above is how it looks like on mu-editor.

Step 11: Final Coding: Part 2

If you have written out the codes, make sure you click the save button. Then, turn off your CPX and turn it on again with the USB cable still connected to your laptop and mu-editor still open. This could be done by pressing the very middle button that says 'RESET' on CPX. Shake to make sure the code is working well properly. If the code is working, the CPX should randomly generate one of the answers from your code. If you are having difficulties in doing this, double check:

1) if the format is right

2) if you don't have any unrecognizable characters in Python (e.g. ', _)

3) if you properly saved it

4) if you have all the audio files (.wav) dragged into CIRCUITPY folder.

Remember, coding requires lots of trials and errors to make it properly work.

If everything works, carefully eject the USB cable. Since you’re done with your coding part of this challenge, you are 95% done in making this hat!

Step 12: Decoration and Design

Now is time for decoration.

First, attach the CPX onto the hat by sewing. Above is a picture of how I did it.

How and where you are placing your battery is also important, but it is your choice. I simply clipped the battery pack onto the side to make it seem natural and not clumpy. Then, I just taped to make sure it doesn't fall out and clipped the wires so it doesn't leave it hanging. Above are the pictures of this design.

As for the decoration, it is completely your choice. I simply sewed in little sequins and beads to make it sparkly.

To decorate the top part, I simply used couch stitching with yarn, small thread and needle. This could also be briefly seen in the above image.

Switch on the battery, and now you’re finally done!

Sensors Contest

Participated in the
Sensors Contest

Be the First to Share


    • Lamps Challenge

      Lamps Challenge
    • CNC and 3D Printing Contest

      CNC and 3D Printing Contest
    • Puzzles Challenge

      Puzzles Challenge



    3 years ago

    Very well done on this project, and in documenting your process. Good work!

    Can you tell me more about this wearable tech class? What grade level is it for, and will all the students be sharing instructables?