# Spy Theme Playback Device

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/052/448/medium800/adafruit_gemma_spyThemesThumb.jpg?1521845761)

When planning and gearing up for a secret op, every good agent needs some motivational spy music to set the tone! (Get it? _Tone_? Sorry, not sorry!)&nbsp;

With a Circuit Playground Express and its built-in speaker you can make your spy themes shine. Programming songs using MakeCode is fun! You can write your own tunes from scratch, or transcribe music.

On Gemma M0 with an added piezo buzzer, we'll look at using CircuitPython to write music as well!

### Part: Circuit Playground Express
quantity: 1
The microcontroller with the works
[Circuit Playground Express](https://www.adafruit.com/product/3333)

### Part: Gemma M0
quantity: 1
Tiny microcontroller with a kick
[Gemma M0](https://www.adafruit.com/product/3501)

### Part: Piezo Buzzer
quantity: 1
Petite but loude
[Piezo Buzzer](https://www.adafruit.com/product/160)

### Part: USB cable
quantity: 1
6" A/MicroB
[USB cable](https://www.adafruit.com/product/898)

### Part: 3 x AAA Battery Holder
quantity: 1
w on/off switch and 2-pin JST
[3 x AAA Battery Holder](https://www.adafruit.com/product/727)

### Part: AAA batteries 
quantity: 1
Alkaline 3 pack
[AAA batteries ](https://www.adafruit.com/product/3520)

You'll also use two M3 x 8mm screws and nuts to connect the piezo to the Gemma M0.

# Spy Theme Playback Device

## Code Music with MakeCode for Circuit Playground Express

First, make sure you’re familiar with setting up the Circuit Playground Express for use with MakeCode by following this guide.&nbsp;[https://learn.adafruit.com/makecode](../../../../makecode)

Once you can successfully create a program with MakeCode and upload it to your board, return here.

You can create songs very easily in MakeCode, by stringing together a series of play (note) for (time) blocks and rest for (time) blocks. For a simple, short melody this works very well, for example, in the embedded MakeCode session here, press the left button on the simulator to hear it play.

https://makecode.com/_Rg2e39V5LU45

Notice how there's a phrase that's repeated twice in that example? I can get pretty tedious to manually enter those same notes and durations over and over again -- so we can instead, turn that one phrase into a reusable mini program within a program! That's what a function is -- a chunk of code that can be referenced easily elsewhere in our program.

In this example, one set of the notes that make up the phrase have been moved into a&nbsp; **function&nbsp;** block ( **functions&nbsp;** are&nbsp;found in the A **dvanced** section). You can click&nbsp; **Make a Function&nbsp;** to create a&nbsp; **function&nbsp;** block -- give it a good, memorable name, in this case **'phrase01'.**

Then, you can use the&nbsp; **call function phrase01&nbsp;** block wherever you want to repeat the phrase.

https://makecode.com/_3jTWMk3aR3K3

Here's what it looks like when we add a couple more phrases and then start to sequence them in the&nbsp; **on button A click&nbsp;** block.

https://makecode.com/_WAV5Rd1mdTWW

Notice how we've been building the song inside the&nbsp; **on button A click&nbsp;** block. If we wanted to also play the song when right button (button B) is pressed, for example, we would have to copy the entire set of blocks and repeat it in two places. This works, but there's a more efficient way -- that's to build another&nbsp; **function&nbsp;** block that itself is filled with **function** blocks!

The new function could be named&nbsp; **song01&nbsp;** and it is full of calls to the other functions that are the phrases that make up the roadmap for the song.

https://makecode.com/_8ud8sd4fLcq1

Here's the full song -- notice how the song function contains many repetitions of the phrases to compose the entire song.

https://makecode.com/_JgvFfKLuJEMz

Here's another favorite -- the theme song from _ **Where in the World is Carmen Sandiego** _.

https://makecode.com/_hvL3grKyd8MJ

Here's the beginning of the **Mission: Impossible** theme. Can you add to it? Maybe make it loop a few more times on button A click?

https://makecode.com/_8F6ULe1sPXkk

You can have fun creating other favorite spy themes, such as:

- Get Smart
- Bloodhound Gang
- Pink Panther

Or, really personalize it and write your own cool theme!

# Spy Theme Playback Device

## Building the Gemma M0 Spy Theme Player

## Assembly

The Gemma M0 can't make any sounds on it's own, so let's connect the piezo buzzer to it so we can hear it!

![](https://cdn-learn.adafruit.com/assets/assets/000/051/973/medium800/adafruit_gemma_IMG_1474_2k.jpg?1521002393)

## Bending Legs

We'll use the two M3 screws and nuts to connect the buzzer's legs to the&nbsp; **D0&nbsp;** and&nbsp; **GND&nbsp;** pads of the Gemma M0.

Use pliers (or your fingers) to bend the legs down, carefully, making sure not to break them off.

Each leg can fit nicely into a small groove molded into the plastic case.

![adafruit_gemma_IMG_1475_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/974/medium640/adafruit_gemma_IMG_1475_2k.jpg?1521002429)

![adafruit_gemma_IMG_1476_2k.jpg](https://cdn-learn.adafruit.com/assets/assets/000/051/975/medium640/adafruit_gemma_IMG_1476_2k.jpg?1521002443)

## Make Feet

Use the pliers or the screws themselves to form small hook-like feet at the end of each leg, as shown here, so they will connect to&nbsp; **D0&nbsp;** and&nbsp; **GND.&nbsp;** (You can check the other side of the board to see the pad names on the silkscreen.)

![](https://cdn-learn.adafruit.com/assets/assets/000/051/977/medium800/adafruit_gemma_IMG_1487_2k.jpg?1521002500)

It doesn’t matter which leg of the buzzer goes to which pad, it is not polarized.

Thread the nuts on and tighten them, being careful that the legs don't touch any copper pads on the board other than their respective pin assignments.

![](https://cdn-learn.adafruit.com/assets/assets/000/051/978/medium800/adafruit_gemma_P3110518_2000.jpg?1521002531)

Next, we'll code the Gemma M0 to play spy themes using CircuitPython.

# Spy Theme Playback Device

## Coding the Gemma M0 with CircuitPython

First, follow this guide [https://learn.adafruit.com/adafruit-gemma-m0/circuitpython](../../../../adafruit-gemma-m0/circuitpython)&nbsp;to get started with coding the Gemma M0 in CircuitPython. Install the latest release version of CircuitPython on the board.

You may also want to install the Mu editor&nbsp;[https://learn.adafruit.com/adafruit-gemma-m0/installing-mu-editor](../../../../adafruit-gemma-m0/installing-mu-editor)&nbsp;for your coding needs.

Once you can successfully code in Mu and upload to the board, return here.

## Ringtones to the Rescue

If you want to write short little ditties, there's an established format we can use: RTTL (Ring Tone Text Transfer Language) that was originally developed by Nokia for cellphone ringtones.

We've written a library to make it easy to use RTTTL in CircuitPython. You can download the library as part the CircuitPython Bundle, and then drag a copy of the&nbsp; **adafruit\_rtttl** &nbsp;into your Gemma M0 **lib** folder. ([Check this page](../../../../adafruit-gemma-m0/circuitpython-libraries) if you have questions about CircuitPython libraries.)

[Click for the Latest Adafruit CircuitPython Library Bundle Release](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest)
Then, to test it out and hear a familiar spy theme, copy the code below, paste it into Mu, and then save it to your **Gemma M0** as **main.py** (if you want to run this code on a **Circuit Playground Express** , see below for alternate code.)

```
import adafruit_rtttl
import board

adafruit_rtttl.play(board.A2, "Bond:d=4,o=5,b=320:c,8d,8d,d,2d,c,c,c,c,8d#,8d#,2d#,d,d,d,c,8d,8d,d,2d,c,c,c,c,8d#,8d#,d#,2d#,d,c#,c,c6,1b.,g,f,1g.")
```

You can see that there isn't too much to it -- we import the rtttl library, and then we can use the `adafruit_rtttl.play()` command. This command takes three elements: name, settings, and notes, all separated by a colon `:`

The song name element in this case is `Bond`.

The settings elements specify:

- `d` for duration of a default note, in this case a `4` means a quarter note. This is a convenience that allows you to not specify the duration of all the quarter notes.
- `o` for the default octave of the song, the range is `4 `to` 7`. Here it is set to `5`
- `b` is the tempo ("beat") of the song in BPM (beats per minute) in this case `320`

Then we get to the final element, the notes. So, for the first few notes of the Bond theme intro we have` c,8d,8d,d,2d,c,c,c,c `which is a C quarter note, two D eighth notes, a D quarter note, a D half note and four C quarter notes. Note, you can also use # for sharps.

Try writing your own tunes, or search online for ringtones published in the RTTTL format.

Here's another favorite:

```
import adafruit_rtttl
import board

adafruit_rtttl.play(board.D0, "The A Team:d=8,o=5,b=132:4d#6,a#,2d#6,16p,g#,4a#,4d#.,p,16g,16a#,d#6,a#,f6,2d#6,16p,c#.6,16c6,16a#,g#.,2a#.")
```

Info: 

```
# rtttl example for Circuit Playground Express
import board
from digitalio import DigitalInOut, Direction
import adafruit_rtttl
spkrenable = DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = Direction.OUTPUT
spkrenable.value = True
adafruit_rtttl.play(board.A0, "Bond:d=4,o=5,b=320:c,8d,8d,d,2d,c,c,c,c,8d#,8d#,2d#,d,d,d,c,8d,8d,d,2d,c,c,c,c,8d#,8d#,d#,2d#,d,c#,c,c6,1b.,g,f,1g.")
```

## Longer Compositions

The RTTTL format works great for short songs -- after all, that's why it was originally designed. Longer songs, however, can get a bit messy if we simply string together note after note after note after note! Even traditional sheet music notation uses repeat signs and codas to avoid writing out one long, linear song!

So, we'll create a number of lists that each contain phrases or measures of the song that are repeated throughout, then we can reference those lists in a larger roadmap for the whole song.

## tone Test

First, we'll use the built-in `pwmio` function to play some test tones over the piezo buzzer connected to **D0** and **GND**.

Copy the code below, paste it into Mu and then save it to your Gemma M0 to test out the tone example.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_Essentials/CircuitPython_PWM_Piezo/code.py

In the first example, we have the James Bond 007 Theme. Note how we create lists for each section -- Bond01, Bond02, and so on -- and then call them at the bottom of the code in the `while True:` loop. See how the Bond01 section repeats twice, then the Bond02 and Bond03 pairings repeat twice more before, calling in Bond04. The opening phrase is again repeated a number of times right before the songs final passage.

First, we set up the `pwmio` output as before.

Then, we create a variable called `tempo`&nbsp;&nbsp;to define the length of a whole note, in this case 2 seconds. You can adjust that to increase or decrease the tempo. All note lengths are derived from this one variable, e.g.`whole_note`is equal to `tempo`,&nbsp;`half_note`&nbsp;is a&nbsp;`whole_note * 0.5`and so on.

Similarly, we create a series of variables to define the pitches different notes, starting from A2 (110Hz) up to B6 (1974Hz). This way, we can call the pitches with a note name instead of a frequency value. This makes it much easier to transcribe from standard music notation!

Here's the James Bond 007 Theme. Copy it and paste it into Mu, then save it to your Gemma M0 as **main.py&nbsp;**

It will play through once -- if you want to repeat it, simply press reset, or turn the board off and then on.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Spy_Theme_Songs/bond/code.py

Here's another fun one -- Where in the World is Carmen Sandiego:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Spy_Theme_Songs/carmen/code.py


## Featured Products

### Adafruit GEMMA M0 - Miniature wearable electronic platform

[Adafruit GEMMA M0 - Miniature wearable electronic platform](https://www.adafruit.com/product/3501)
The **Adafruit Gemma M0** is a super small microcontroller board, with just enough built-in to create many simple projects. It may look small and cute: round, about the size of a quarter, with friendly alligator-clip sew pads. But do not be fooled! The Gemma M0 is incredibly...

In Stock
[Buy Now](https://www.adafruit.com/product/3501)
[Related Guides to the Product](https://learn.adafruit.com/products/3501/guides)
### Piezo Buzzer

[Piezo Buzzer](https://www.adafruit.com/product/160)
Piezo buzzers are used for making beeps, tones and alerts. This one is petite but loud! Drive it with 3-30V peak-to-peak square wave. To use, connect one pin to ground (either one) and the other pin to a square wave out from a timer or microcontroller. For the loudest tones, stay around 4 KHz,...

In Stock
[Buy Now](https://www.adafruit.com/product/160)
[Related Guides to the Product](https://learn.adafruit.com/products/160/guides)
### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/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 made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### USB cable - 6" A/MicroB

[USB cable - 6" A/MicroB](https://www.adafruit.com/product/898)
This here is your standard A-microB USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your NETduino, Arduino Leonardo, Raspberry Pi (among other things).  
  
Approximately 6.5" long from end to end, its a little more compact than our classic 3' long cable  
<br...></br...>

In Stock
[Buy Now](https://www.adafruit.com/product/898)
[Related Guides to the Product](https://learn.adafruit.com/products/898/guides)
### 3 x AAA Battery Holder with On/Off Switch and 2-Pin JST

[3 x AAA Battery Holder with On/Off Switch and 2-Pin JST](https://www.adafruit.com/product/727)
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about 3.3-4.5V, a very similar range to Lithium Ion/polymer (Li-Ion) batteries and have an on-off switch. That makes...

In Stock
[Buy Now](https://www.adafruit.com/product/727)
[Related Guides to the Product](https://learn.adafruit.com/products/727/guides)
### Alkaline AAA batteries - 3 pack

[Alkaline AAA batteries - 3 pack](https://www.adafruit.com/product/3520)
Battery power for your portable project! These batteries are good quality at a good price, and work fantastic with any of the kits or projects in the shop that use AAA's. This is a pack of **3 AAA batteries**.  
  
These batteries are Alkaline (MnO2) chemistry, with a...

In Stock
[Buy Now](https://www.adafruit.com/product/3520)
[Related Guides to the Product](https://learn.adafruit.com/products/3520/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Close Encounters Hat](https://learn.adafruit.com/close-encounters-hat.md)
- [Porting an Arduino library to CircuitPython: VL6180X Distance Sensor](https://learn.adafruit.com/porting-an-arduino-library-to-circuitpython-vl6180x-distance-sensor.md)
- [LED Masquerade Masks](https://learn.adafruit.com/led-masquerade-masks.md)
- [Crickit Powered Minerva Owl Robot](https://learn.adafruit.com/crickit-powered-owl-robot.md)
- [Hanukkah MakeCode Menorah Sweater](https://learn.adafruit.com/hanukkah-menorah-sweater.md)
- [Circuit Playground Express Compass](https://learn.adafruit.com/circuit-playground-express-compass.md)
- [Steven Universe Cosplay Shirt & Gem](https://learn.adafruit.com/steven-universe-cosplay-shirt-gem.md)
- [Mount for CRICKIT](https://learn.adafruit.com/mount-for-crickit.md)
- [Trinket / Gemma Blinky Eyes](https://learn.adafruit.com/trinket-gemma-blinky-eyes.md)
- [Pearl's Gem Weapon – Steven Universe](https://learn.adafruit.com/pearl-s-spear-steven-universe.md)
- [New Year Countdown Clock with Circuit Playground Express](https://learn.adafruit.com/new-year-countdown-clock-with-circuit-playground-express.md)
- [MakeCode Course for Circuit Playground Express](https://learn.adafruit.com/makecode-circuit-playground-express-course.md)
- [CircuitPython Sin Complicaciones para la Circuit Playground Express y la Bluefruit](https://learn.adafruit.com/circuitpython-sin-complicaciones-para-la-circuit-playground-express.md)
- [Make It Sound](https://learn.adafruit.com/make-it-sound.md)
- [CircuitPython with Jupyter Notebooks](https://learn.adafruit.com/circuitpython-with-jupyter-notebooks.md)
