Overview
You could make an electronic greeting card with your own customized message or song, add a background soundtrack to a model train diorama, or create the world’s smartest whoopee cushion.
Parts Needed
There are two phases to this project. The first (using a regular Arduino) loads sound data onto a flash memory chip, the second (using Trinket) plays it back. Both stages use some parts in common:
-
Winbond 25Q80BV serial flash memory (1 megabyte DIP). This can store about one minute of music or two minutes of voice, depending on quality.
Note this chip is discontinued! If you use a different chip you may have to modify your code, see here for an example. - Breadboard(s) and jumper wires, or proto boards and related soldering paraphenalia
For the “loading” stage:
- Arduino Uno or similar board
- Capacitor: one 0.1 μF
- Resistors: 3 each 470 Ohm and 1K Ohm
- You can optionally add an LED (any color) and 220 Ohm resistor for a status indicator
Not all of these parts are available from Adafruit. You may be able to swap out for different parts you already have on-hand or can acquire locally; the “Loading Sounds” page has some guidance on alternative parts selection.
For the “playback” stage:
- Adafruit Trinket 3.3V (NOT 5V!)
- Capacitors: one 10 μF, 2 each 0.1 μF
- Resistor: one 68 Ohm
- 10K potentiometer
- Battery (or can use USB power)
and either:
- Headphone jack and headphones or portable amplified speaker
or:
The “Sound Playback” page likewise has some guidance on alternative parts selection. There’s a lot of wiggle room, not everything needs to be a super-precise value.
Software Needed
You'll need sound files in WAV format. You can search for downloadable examples on the internet (movie quotes, cartoon sounds, etc.), or record or convert something from your music collection using software such as Audacity (free download).This project uses both Processing and the Arduino IDE. Both look very similar when running, which can lead to confusion, so make sure you’re loading the right code in the right editor! Processing is for writing code to run on your computer, while Arduino is for writing microcontroller code.
Download version 2.0 (or later) of Processing from processing.org (our software won’t work with the 1.5 version, if you currently have that installed).
If this is your first time using Trinket, work through the Introducing Trinket guide first; you need to install and then customize some settings in the Arduino IDE. Once you have it up and running (test the “blink” sketch), then download and install the TinyFlash library:
When properly installed, in the Arduino IDE you should then have access the rollover menu File→Sketchbook→Libraries→Adafruit_TinyFlash
The “examples” folder included with the library contains all the code for this project; there’s nothing else to download.
- The flash chip is super affordable, so you can make it a permanent part of a small project. As it’s in DIP chip form, you don’t need to buy a special breakout board like you would for an SD card.
- Reading a FAT-formatted SD card with this tiny microcontroller is incredibly difficult; a single SD block would fill the chip’s entire RAM! We have seen projects that do this, so it’s not impossible but is nonetheless quite challenging. Perhaps we’ll revisit this idea in the future.
Page last edited October 10, 2013
Loading Sounds
If your audio is in a different format, you can convert it with a tool like Audacity (free), Adobe Audition ($$$) or you may already have a utility on your computer that can produce something. Even iTunes can do this, if you tweak the import settings:
The Winbond flash chip we’re using has a capacity of 1,048,576 bytes (1 megabyte, often called “8 megabit” because marketing). Six bytes are used to store data about the length and sampling rate of the audio, leaving 1,048,570 bytes for the audio data itself. Each byte is one audio sample.
To estimate the maximum duration of audio you can store on the chip:
Max. duration (in seconds) = 1,048,570 ÷ sampling rate
e.g. with 16,000 Hz (16 KHz) music:
1,048,570 ÷ 16,000 = ~65.5 seconds
If your source audio file is too big for the available space, the end will be truncated to fit.
Chip Loading Circuit
Because the Trinket can’t support traditional serial I/O, we’ll make temporary use of a regular Arduino board to help transfer data to the Winbond chip. Later we move it to a playback circuit.
We suggest using an Arduino Uno, because it has easy access to the SPI pins used to communicate with the flash chip. This can be done with a Leonardo, Mega or other board, but you’ll need to adapt the wiring to use the 6-pin ICSP header (MISO, MOSI and SCK pins, specifically).
The serial flash chip is a 3.3 Volt device, whereas the Arduino is 5 Volts. To avoid unleashing the Blue Smoke Monster, it’s necessary to power the chip from the Arduino’s 3.3V (not 5V) pin, and then use level-shifting circuitry to drive the control lines. There are chips that do this; use ’em if you got ’em, or a simple approximation can be made using resistors to create a voltage divider: output signal pins from the Arduino each connect to a 470 Ohm resistor, the other end connected both to an input pin on the flash chip and a 1K resistor to ground (see diagrams below). If you don’t have exactly these values of resistors on-hand, that’s okay, it’s not rocket surgery…you can substitute other values with approximately a 1:2 ratio, for example 1K and 2.2K (or use two 1K resistors in series for the latter). 1K/2.2K is about the upper limit on values; don’t go higher than this. Won’t harm anything, just won’t work very reliably. Also, note that the data output line back into the Arduino (pin 12, blue wire) connects directly, there are no resistors on this line.
An LED and 220 Ohm resistor can be added between pin A0 and GND to provide a simple status display. The LED will blink to indicate an error, or flickers during the data transfer. This is not essential and can be left out of the circuit if you don’t have the parts.
Finally, there’s a 0.1 μF capacitor between the flash chip’s VCC supply and GND. In a pinch you can get by without this, but it’s good form to have it there, keeps electrical gremlins away.
File→Sketchbook→Libraries→Adafruit_TinyFlash→AudioLoader
Select the board type and serial port from the Tools menu, and upload this code to the board. If you don’t have the flash memory chip installed (or if it's positioned incorrectly, or turned around), the status LED should blink. If it’s working properly, you won’t see anything from the LED, but you can check the serial monitor (at 57600 baud). It should display:
HELLO 1048576
Transferring Audio
Close the Arduino serial monitor if you still have it open; the other code won’t work if it’s there.Launch Processing and load the AudioXfer sketch. It’s inside the Adafruit_TinyFlash folder that you downloaded earlier, in a sub-folder called “Processing.” Sorry to make you hunt through the Arduino folder for this, but it was less troublesome that requiring a separate download.
portname = "/dev/tty.usbmodem1a1331"; // bypass scan
AudioXfer is a pretty barebones program…other than the file selection, there is no fancy GUI, it just prints text to the console. You’ll see a long line of dots (and the LED will flicker) as data is transferred to the Arduino and written to the chip. It can take several minutes to load a 1 megabyte chip to capacity. So you may want to test with just a short sound at first.
If all goes well, the software reports “done!” If an error was encountered, you’ll instead see a message with some indication of the problem.
Once a sound is successfully loaded, disconnect the Arduino from USB, remove the flash memory chip from the breadboard or socket, and then we’ll move it over to the playback circuit…
Page last edited October 10, 2013
Sound Playback
There’s a relationship between the capacitor and resistor values and the resulting cutoff frequency. Given any two values, we can compute the third. Already having a ton of 0.1 microfarad capacitors around, I just needed to know the corresponding resistor to achieve the desired 25 KHz cutoff (you could also do it the other way — some resistor you have around, determining a suitable capacitor). Rather than bore you with the math, you can just whip out the Circuit Playground app for iOS (select “Circuit Calculators,” then “RC Cutoff Filter,” or you can Google search for “low pass filter calculator” resources on the web and plug in the two known values you have.
The filtered output is then fed into a 10K potentiometer for volume adjustment (you can leave this part out, but the volume will always be at the maximum) and then through a 10 microfarad capacitor that provides “AC coupling” — so the audio waveform is centered at 0 Volts rather than 1.65V (one half the Trinket’s operating voltage). The output is split to both the right and left channels of a 1/8" phono jack, to which headphones or an amplified speaker can be connected.
It’s very important that a 3.3V Trinket is used; this voltage is directly compatible with the flash memory chip. Adding the level shifting circuitry would defeat the smallness of the Trinket.
Trinket Pin #4 (audio output) MUST BE DISCONNECTED before code can be uploaded to the board! If you solder the circuit permanently in a proto board, it's strongly recommended that the Trinket be socketed, or add a jumper between pin 4 and the RC filter so it can be disconnected when new code is uploaded to the chip.
File→Sketchbook→Libraries→Adafruit_TinyFlash→TrinketPlayer
Select “Adafruit Trinket 8 MHz” from the Tools→Board menu. Disconnect pin 4 (or remove Trinket from the circuit), press the reset button, then click Upload. After uploading, assuming all else is wired properly, your audio should start playing immediately. As the code is currently written, the sound will loop forever. You could change this to stop after the music plays, then use the reset button to restart.
This code only works on the Trinket. It uses special registers and will not compile on the Uno or other Arduino boards.
Page last edited October 10, 2013