Ask MAKE is a weekly column where we answer reader questions, like yours. Write them in to email@example.com drop us a line on Twitter. We can’t wait to tackle your conundrums!
George writes in:
I’ve been wanting to make a prank project, that I can leave somewhere to play back a recorded sound and flash some lights. I’ll use LEDs for the lights, but don’t know how to play back sound. Do you have any suggestions?
Aha, this sounds like it could be a fun project. I’m going to assume you are using a microcontroller to monitor an input (sound? light? time? vibration?), and then initiate a sequenced event. It’s a bit late for Halloween, but this would be a good way to make lawn props that react when someone comes near your house.
Because playing back sound takes a reasonable amount of memory and speed, it’s not something that can be done easily with a standard microcontroller. Instead, the best way to handle this would be to hook up another device, that can be started by the micro and then do the heavy lifting of actually playing back a sound. At least three possibilities come to mind: using a Wave Shield, hacking an MP3 player, or hacking a cheap toy with a sound recorder.
If you are using an Arduino, the Wave Shield might be an ideal solution to your problem. It is an expansion module that allows you to play sounds off of an SD card, and there is a nice library to control it as well. It runs in at about $22 bucks, which seems pretty reasonable for what you get.
If you aren’t using an Arduino, or already have an old MP3 player and don’t want to spend the bucks on a project you will only use once, then you can try to use that. The best way to do this is probably to wire an optocoupler to the play button on the MP3 player, and then trigger that with your microcontroller. If you have more than one sound track that you want to trigger, you could also wire up the next button, but that might get tricky. The bonus for going this way is that you could recycle some electronics junk that would otherwise go to the scrap heap.
Either of the two above solutions are great if you are only thinking of making one or two of the devices, but what if you want to make a bunch of them? In that case, it might be more economical to try hacking a cheap toy, such as this one. Somehow these are still available, and are less than $2 in quantity. The sound quality probably won’t be anywhere as good, but hey, the’re cheap! If you are in a rush, you could also try hacking a voice recording card that you can pick up at a local store. Good luck!
In the Maker Shed:
12 thoughts on “Ask MAKE: Playing back a recorded sound”
This is mostly bad advice. Yes, these approaches will work, but for short, moderate quality sound reproduction (on the order of a few seconds, which I assume is what George had in mind, given the prank nature and the flashing lights) most any modern microcontroller is up to the task.
For example, the Arduino Duemilanove is available with a 32kbyte program ROM. This is usable for data storage, which can be read via the LPM instruction. A program to monitor a sensor and play sound is trivial, and along with the bootloader should take no more than 3kB (generously). This leaves 29kB of flash for storing audio. 8-bit audio at an 8kHz samplerate (giving 4kHz maximum audio frequency) is sufficient for, e.g., moderate quality voice reproduction, and you can fit almost 3.5 seconds of it into 27kB. (Note, 8051/8052-derived micros also support this via MOVX, and even the Microchip DSPics will let you read data from program memory, though they are pretty gimpy at doing so because of how their memory is organized.)
Playback is as simple as loading the 8-bit data into one of the 8-bit PWM SFRs, updating the value 8kHz, then low pass filtering the resulting output with an R-C network. Use an op-amp to drive a small speaker and you’re done. Alternatively, you can use the PWM output to control a half-bridge or H-bridge driving the speaker with an L-C filter, effectively making a class-D audio stage.
With regard to processing power, using the PWM output and updating at 8kHz takes almost none. In fact, I’ve designed a small board with a very similar purpose to George’s that uses the microcontroller for playback and also to implement the controller for a dc/dc converter. Most of the processor power goes to the dc/dc converter feedback loop; the audio is almost an afterthought by comparison. I have an incomplete writeup (for now, just soldering instructions plus BOM, board, and code) of my design at http://web.jfet.org/awkwardalarm/
Any of these is a far more enlightening approach than “buy this solution, it’ll just do it for you!”
Ok, that’s a fair criticism, I shouldn’t have been so quick to overlook using a micro to generate sound. Here’s a sketch for Arduino that uses a timer-based PWM to play back PCM (pulse-control modulation) audio:
If you need higher quality or longer sounds, though, I think an external module is going to be the best way to go.
A cheap 30 second recording device you could hack is:
I’m sure an Arduino could trigger it via a relay.
Please post your creation when you’re done. Others cold learn a lot!
Not cheap, but full on features and easy to control via serial COM:
You could use an HK828 chip:
They aren’t cheap though (at least not down in this part of the world).
I used one to make this: http://www.youtube.com/watch?v=WN1riSZcjKs
I have previously done it myself using a micro and some memory then clocking out 8 bit sample bytes into a buffer then into a R-2R resistor D to A converter. That worked pretty well. I’ve done that using a PIC 16f84 (obsolete now) and also a 6502!
Yeah! I remember the A Team music box, it’s awesome! Thanks for chiming in.
Comments are closed.