Trinket Audio Player using arduino

Trinket Audio Player

Overview

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.

Trinket Audio Player

So will this play MP3s and stuff?

No. It’s a very simple circuit that just plays a short “raw” audio loop. For more sophisticated audio projects, check out our VS1053 Codec board.

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:

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:

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:

Installing Arduino libraries is a frequent stumbling block. If this is your first time, or simply needing a refresher, please read the All About Arduino Libraries tutorial. 

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?

Good question! There are a couple of reasons: 

  • 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.

Loading Sounds

Sound files for this project need to be in WAV format, uncompressed (PCM), 8- or 16-bit resolution. Mono, stereo or multi-channel are all acceptable…the software we’ll use in a moment will automatically convert to 8-bit mono if needed. 

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:

For voice recordings, 8 KHz is often a sufficient sample rate. For music, 16 KHz or more. Generally, higher sampling rates will produce better-sounding audio, but it requires more space. Also, the way the playback circuit works, there’s diminishing returns above 25 KHz. Experiment! 

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.

Trinket Audio Player1

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).

Leave a Comment

Your email address will not be published. Required fields are marked *