Simple Sound Triggers with Arduino
This tutorial will allow you to play short samples (less than four seconds) from an Arduino with no extra hardware. It is based off the work of MIT group “high-low tech” and uses all free software. It will futher explain how to trigger the sound using sensors. It is generously translated into Chinese by Chris Zhang at Shanghai hackerspace Xin Che Jian:
此次教程将教你怎么仅用 Arduino播放一个简短的声音样品（小于４秒钟）。此次教程来源于麻省理工“high-low tech”小组，所有软件均是免费。教程也会进一步解释怎么用传感器触发声音。medium.com
The necessary software is available for MacOS, Linux, and Windows. Arduino IDE and Audacity will need to be installed. The original tutorial provides two more pieces of code:
You can either open an existing file with Audacity, or record a new one, which is what Chris did to create our ghost sound. Freesound.org is a great website to download other people’s recordings. Try it out! Use the record, stop, and play icons at the top left of the program.
When deciding what sound to use, we have to consider the very small memory of Arduino — 32kB for our program + our audio. Audacity has a “resample” ability so our sound takes up less memory. In the bottom left corner, change the project rate from 44000 to 8000. Then in the Tracks menu, select Resample, and hit OK.
When powering a speaker directly from the Arduino, it won’t be very loud. In Audacity, if your sound waves aren’t very tall (low volume, low amplitude), it’s useful to use the Normalize effect. This boosts your levels as high as possible without clipping (overpowering the speaker resulting in distorted sound). Select Effect →Normalize and set the amplitude to zero before hitting OK.
Next, click and drag a portion of your sound wave (less than 4 seconds in length) and select File →Export Selected.
In the Export menu, you’ll have the option to change the file format. This is typically the difference between mp3, wav, and flac, etc. but we’re using a more uncommon file type, so in that menu choose “Other uncompressed files” and click options. Choose WAV (microsoft) as your header , and Unsigned 8 bit PCM as the Encoding.
This results in a very small .wav file that we can finally convert to store on the Arduino. The conversion program is written in Processing so for now it requires Java. Run “EncodeAudio”, which simply asks you to select that wav file you just exported. The results are stored on your clipboard, waiting to be pasted. We’re ready to open Arduino and save the file to memory.
Open the Arduino IDE and add the PCM library downloaded above. Open File →Examples →PCM →playback. It should look like this:
That long list of integers is the example sound, we can replace it with the list of integers that resulted from Encode Audio. Triple-click on any of the numbers to highlight the entire line (thousands of numbers long) and use ctrl-v or cmd-v to paste our own sound.
Upload this code and the sound will play on a speaker connected to pin 11 and GND. You can either use a tiny speaker like in the doorbell tutorial, or if you have speakers with a headphone jack, wire it up like so:
GND gets attached to the tip, pin 11 can be attached to either (or both!) of the remaining silver bands. Be sure to start with the volume on your speakers turned way down, this will play full blast! Also probably don’t do this to your nice stereo cause I’m not sure if it’s safe, I just know it works.
With the current code, the sound will play once when the Arduino turns on: you can hit the ‘reset’ button on the circuitboard to play it again.
The simplest trigger possible is touching two wires together to connect GND to a sensor pin. In our loop we constantly check if our sensor pin is ‘LOW’ and if it is, we start the sound. So cut the ‘startplayback’ function from inside the setup and paste it inside a conditional statement in the loop, using digitalRead() to check if it’s connected to ground. To make sure it doesn’t go off when no one is at the door, we use the internal pull up resistor using pinMode(). This is our final code for using a simple switch to trigger a single sound.
Notice that I changed the name of the array from ‘sample’ to ‘ghost’ — be sure to change the name in the startPlayback function as well — it’s used twice.
You’re not limited to this, of course! You can have as many different sounds as will fit on memory (so 4 seconds total) but trigger them at different times. Copy-paste that ‘constant unsigned char sample PROGMEM’ code and give different names to different lists of numbers. You can also use an external pullup resistor of 1 megaohm (the built-in one is only 10k) to make it touch sensitive instead of having to touch two wires together. Connect the megaohm resistor between the sensor pin (A0) and 5V. Delete the pinMode function from the setup.
The following code uses booleans to play a different sound each time the sensor is touched. This one includes our ghost sounds, so you can upload this and try it yourself. Megaohm resistor between A0 and 5V, speaker on pin 11 and GND.