# Make It Sound

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/057/936/medium800/makecode_cpx_skr.jpg?1532365014 The Circuit Playground Express board (left) and the speaker location on-board (right))

Making and reacting to sound is fundamental to daily life. Your next project can use sound quickly and easily to provide indications something is happening or as the perfect accompaniment to your projects use.

Using either Microsoft MakeCode or CircuitPython, you can use sound in your project.

The code will use the Adafruit Circuit Playground Express board. If you have a project that is robotic, you can add the Adafruit Crickit robotics board to Circuit Playground Express which provides the benefit of an amplifier for much better sounds than that provided on the Circuit Playground Express on-board speaker.

## Part List
### 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)
![A Black woman's manicured hand holds a round microcontroller with lit up LEDs.](https://cdn-shop.adafruit.com/640x480/3333-05.jpg)

## To Go Further
### Adafruit CRICKIT for Circuit Playground Express

[Adafruit CRICKIT for Circuit Playground Express](https://www.adafruit.com/product/3093)
Sometimes we wonder if robotics engineers ever watch movies. If they did, they'd know that making robots into servants always ends up in a robot rebellion. Why even go down that path? Here at Adafruit, we believe in making robots our&nbsp; **friends!**

So if you find...

In Stock
[Buy Now](https://www.adafruit.com/product/3093)
[Related Guides to the Product](https://learn.adafruit.com/products/3093/guides)
![Top down view of a Adafruit CRICKIT for Circuit Playground Express with a circular board connected. ](https://cdn-shop.adafruit.com/640x480/3093-07.jpg)

### Mini Metal Speaker w/ Wires - 8 ohm 0.5W

[Mini Metal Speaker w/ Wires - 8 ohm 0.5W](https://www.adafruit.com/product/1890)
Listen up! This tiny 1" diameter speaker cone is the perfect addition to any small audio project where you need an 8 Ω impedance and will be using 0.5W or less of power. We particularly like this cone as it's very simple and its metal body is extremely lightweight. <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1890)
[Related Guides to the Product](https://learn.adafruit.com/products/1890/guides)
![Mini Metal Speaker with Wires](https://cdn-shop.adafruit.com/640x480/1890-06.jpg)

# Make It Sound

## Music and Sound in MakeCode

If you are new to Microsoft MakeCode, you can&nbsp;[learn the basics of MakeCode here](https://learn.adafruit.com/makecode "MakeCode Basics").

Currently MakeCode has a whole block group, **MUSIC** , devoted to making sounds:

![](https://cdn-learn.adafruit.com/assets/assets/000/057/930/medium800/makecode_misic.PNG?1532360329)

The main groups of blocks provide the following:

- Setting the volume (loudness) of your tones
- Play sounds at startup or stop playing all sounds
- Play any frequency tone
- Play a musical note
- Play music faster or slower
- Note duration (beat) and pause between notes (rest)

The following code will play a `220` Hz tone when you press the A button and a `440` Hz tone when you press the B button. Within the **INPUT** group are the loops used for button presses. Select two `on button` blocks and add to each a `play tone at` block from the **MUSIC** group.

Change the value of the button A play tone at to `220` and it will replace the number with the value `Low A`. Replace the value of the Button B play tone to `440` and MakeCode will change the number to `Middle A`. MakeCode recognizes certain sound frequencies as musical notes and will change those accordingly. Alternatively MakeCode will put up a small keyboard when you work with the play tone at block's value to help you select musical notes if you like.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/931/medium800/makecode_music_2.PNG?1532360953 You can type in a numeric frequency value or use the provided keyboard  to select musical note frequencies)

[Click here to open this project in MakeCode](https://makecode.com/_HXq68W5PTibq)
Pull up this example and press the Download button to save it to your Circuit Playground Express. Press the A and B buttons. You get two different sounds! Feel free to change them up.

## Predefined Sounds

MakeCode comes with several predefined sounds that have several musical notes. Change the Button A and Button B contents both to use the `play sound until done` block. Change them so each button plays a different sound:

![](https://cdn-learn.adafruit.com/assets/assets/000/057/932/medium800/makecode_jump.png?1532361468)

[Click here to open this project in MakeCode](https://makecode.com/_5gv6x9Y9X0kb)
Push the buttons and you get two melodies. Feel free to use the predefined sounds in your code.

## Music

Typing in your own musical melody is possible but you need to know a bit about musical notation. I have found that some Internet sites are good at providing the notes, some are not. You can look to someone who knows how to read music to help if all you find is sheet music.

The following program plays a well known tune. See how many notes it takes to do the short portion? The code also uses the A and B buttons to change how fast or slow the music plays.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/933/medium800/makecode_snake.png?1532361897)

[Click here to open this project in MakeCode](https://makecode.com/_VRvPTUboJ6ym)
Using MakeCode, it is a bit tedious to put in lots of custom sounds. On the next page, CircuitPython will be used which also allows use of pre-recorded sound files which MakeCode cannot use at present.

# Make It Sound

## Music and Sound in CircuitPython

![](https://cdn-learn.adafruit.com/assets/assets/000/058/028/medium800/makecode_Blinka-promo.png?1532465714)

CircuitPython has similar capabilities to MakeCode for playing tones. CircuitPython, though, doesn't understand music so you would have to provide your own understanding of which musical notes correspond to which sound frequencies.

You can look up which musical notes correspond to which frequencies on a number of websites including [this chart on Wikipedia](https://en.wikipedia.org/wiki/Piano_key_frequencies "Piano key frequencies").

## Simple Tones

Adafruit provides the `adafruit_circuitplayground` library to provide high level access to the various parts of the board in CircuitPython.&nbsp; The functions CircuitPython provides for using the speaker:

- `cpx.play_tone(frequency, duration)`
- `cpx.start_tone(frequency)`
- `cpx.stop_tone()`
- `cpx.play_file("laugh.wav")`

Most often for tones, it is known how long you might want to play the tone, for a second or so or perhaps until you manually turn it off (tones that play a long time are annoying unless you are doing it on purpose like for an alarm).

The two tone program using buttons A and B for two versions of the musical note A is:

```
from adafruit_circuitplayground.express import cpx

while True:
    if cpx.button_a:
        cpx.play_tone(220, 0.5)
    elif cpx.button_b:
        cpx.play_tone(440, 0.5)
```

In this case we play the frequencies 220 Hz and 440Hz each for 1/2 second.

## Continuous Tones

For an alarm, say your soil is too dry and you need to water or your family member tripped your alarm. We'll use a capacitive touch on Circuit Playground Express pad A1 to trigger an alarm. The Circuit Playground Express slide switch will silence and reset the alarm:

```
    from adafruit_circuitplayground.express import cpx

cpx.adjust_touch_threshold(100)  # pretty sensitive

while True:
    if cpx.switch and cpx.touch_A1:
        cpx.start_tone(2000)
    if not cpx.switch:
        cpx.stop_tone()
  
```

## Wave Files

The strength of CircuitPython lies with its ability to play sound files in the wave or WAV format. Not 100% of wave files on the Internet are compatible though. The parameters for CircuitPython playable files are:

- A sample rate less than or equal to 22,050 hertz
- 16-bit
- mono / single channel

Adafruit has a [guide to help you with your audio files here](https://learn.adafruit.com/adafruit-wave-shield-audio-shield-for-arduino/convert-files "Audio Guide").

Using wave files for your project is wonderful! You have nearly unlimited sounds to sample and play on whatever command you set.

Here is a simple program that will play one wave file when button A is pressed and another if button B is pressed.

Here's the two files we're going to play:

[rimshot.wav](https://cdn-learn.adafruit.com/assets/assets/000/047/231/original/rimshot.wav?1507858005)
[laugh.wav](https://cdn-learn.adafruit.com/assets/assets/000/047/232/original/laugh.wav?1507858014)
Click to download, and save them onto your **CIRCUITPY** drive, alongside your **code.py** &nbsp;program file.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/058/034/medium800/makecode_Untitled.png?1532477946)

```
from adafruit_circuitplayground.express import cpx

while True:
    if cpx.button_a:
        cpx.play_file("rimshot.wav")
    if cpx.button_b:
        cpx.play_file("laugh.wav")
```

It is that easy!

If you want to try out some projects which use wave files, here are some good ones in the Adafruit Learning System:

- [FruitBox Sequencer: Musically Delicious Step Pattern Generator](https://learn.adafruit.com/circuitpython-fruitbox-sequencer-musically-delicious-step-pattern-generator/overview)
- [Sparky the Blue Smoke Monster Automaton](https://learn.adafruit.com/sparky-automaton)


## Featured Products

### 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)
### Adafruit CRICKIT for Circuit Playground Express

[Adafruit CRICKIT for Circuit Playground Express](https://www.adafruit.com/product/3093)
Sometimes we wonder if robotics engineers ever watch movies. If they did, they'd know that making robots into servants always ends up in a robot rebellion. Why even go down that path? Here at Adafruit, we believe in making robots our&nbsp; **friends!**

So if you find...

In Stock
[Buy Now](https://www.adafruit.com/product/3093)
[Related Guides to the Product](https://learn.adafruit.com/products/3093/guides)
### Mini Metal Speaker w/ Wires - 8 ohm 0.5W

[Mini Metal Speaker w/ Wires - 8 ohm 0.5W](https://www.adafruit.com/product/1890)
Listen up! This tiny 1" diameter speaker cone is the perfect addition to any small audio project where you need an 8 Ω impedance and will be using 0.5W or less of power. We particularly like this cone as it's very simple and its metal body is extremely lightweight. <a...></a...>

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

[Circuit Playground Express - Base Kit](https://www.adafruit.com/product/3517)
It's the **Circuit Playground Express Base Kit!** &nbsp;It provides&nbsp;the few things you'll need to get started with the new [Circuit Playground Express](https://www.adafruit.com/product/3333).&nbsp;This version of Circuit Playground is super powered, and will...

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

[Circuit Playground Express Advanced Pack](https://www.adafruit.com/product/2769)
 **Circuit Playground Express** &nbsp;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 program....

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

[Code.org Circuit Playground Express Educators' Pack](https://www.adafruit.com/product/3399)
For many years, instructors and teachers have asked us to come up with a better way to teach programming and electronics. We have worked on Circuit Playground Express for over a year to come up with a board that is powerful, beautiful, fun, and perfect for teaching

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

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Microsoft Teams Mute Button](https://learn.adafruit.com/teams-mute-button.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)
- [Crickit Dancing Marionette Kit](https://learn.adafruit.com/universal-marionette-with-crickit.md)
- [Controlling a Classic Nintendo R.O.B. Robot Using Circuit Playground Express](https://learn.adafruit.com/controlling-a-classic-nintendo-r-o-b-robot-using-circuit-playground-express.md)
- [Make it Move with Crickit](https://learn.adafruit.com/make-it-move-with-crickit.md)
- [CRICKIT Exhibit Demo Board](https://learn.adafruit.com/crickit-exhibit-demo-board.md)
- [Make It a Keyboard](https://learn.adafruit.com/make-it-a-keyboard.md)
- [Rose's Shield - Steven Universe](https://learn.adafruit.com/rose-s-shield-steven-universe.md)
- [LED NeoPixel Corset with Circuit Playground Express and MakeCode](https://learn.adafruit.com/led-corset-with-circuit-playground-and-makecode.md)
- [Circuit Playground Gizmo Ornaments](https://learn.adafruit.com/display-ornaments-with-circuit-playground.md)
- [Circuit Playground Slouch Detector](https://learn.adafruit.com/circuit-playground-slouch-detector.md)
- [Sparky the Blue Smoke Monster Automaton](https://learn.adafruit.com/sparky-automaton.md)
- [Crickit Powered Dancin' Snowman!](https://learn.adafruit.com/crickit-powered-dancin-snowman.md)
- [NeoPixie Dust Bag with Circuit Playground Express](https://learn.adafruit.com/neopixie-dust-bag-cpx.md)
- [Make It Talk](https://learn.adafruit.com/make-it-talk.md)
