Trinket Audio Player using arduino
We usually think of the Adafruit Trinket as a tiny subset of a “real” Arduino; less RAM, less code space, less I/O. But this little chip has a couple tricks up its sleeve, things its larger brethren can’t do. One of these is a high-speed PWM mode. With just a few extra components this can be used for audio output. Not simply piezo beeps and buzzes…actual sampled digital sound!
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.
So will this play MP3s and stuff?
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.
- 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)
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.
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.
Why this weird flash memory chip? Why not an SD card?
- 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.
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.
This Post / Project can also be found using search terms:
- trinket pwm