Application Board Waveplayer Demo

Here is a wave player demo setup for the mbed application board. It is based on the earlier mbed cookbook wave player code. It reads a wave file from a USB flash drive, and outputs audio to the tiny onboard speaker (very low volume) and the analog audio out jack. The audio out jack can be connected to a set of PC speakers for more volume. A wave file for the demo is available at sample.wav. Copy it to the USB drive before running the demo and insert the USB flash drive into the application board's USB connector.

The board's speaker is connected to a PWM output pin (not a D/A output). It is possible to output audio using PWM and this technique is actually used in a Class D Audio Amplifier. The PWM frequency is set to about ten times the highest frequency of the audio signal. The duty cycle is then set by the analog sample value. A series of high speed digital pulses is produced, but the average voltage value of the digital signal is the desired analog level.The volume is very low on the tiny speaker (about the same as an earphone) and you will need to hold the board close to your ear to hear it.

Boom Box Upgrade with PC speakers

For more volume, plug in a set of PC speakers to the analog out jack on the mbed application board. The sound level will be similar to what you hear when playing audio on the PC. The audio out signal is coming from the mbed's D/A (DAC) and it has been adjusted to the lower voltage levels typically seen on PC audio signals. PC speakers have a built-in audio amplifier circuit to drive the larger speakers.

More background information can be found in the tutorial on using a speaker for audio output.


When using the mbed as a USB host, 15k Ohm pull down resistors are needed on the D+ and D- signals, as per the USB specification. This can be achieved by switching both switches the DIP switch to the "USB Host" position


#include "mbed.h"
#include "USBHostMSD.h"
#include "wave_player.h"
//mbed Application board waveplayer demo
//Plays the wave file "sample.wav" on the USB flash drive
//Outputs to onboard speaker (but at very low volume)
//and the Audio Out jack for connection to a set of amplified PC speakers (at higher volume)
//Needs a USB flash drive inserted with the wav file on it to run

//Analog Out Jack
AnalogOut DACout(p18);
//On Board Speaker
PwmOut PWMout(p26);

wave_player waver(&DACout,&PWMout);

int main()
    USBHostMSD msd("usb");
    FILE *wave_file;
    //setup PWM hardware for a Class D style audio output
    printf("\n\n\nHello, wave world!\n");
    // wait until connected to a USB device
    while(!msd.connect()) {
    //open wav file and play it
    //end of program
    while(1) {};

Import programAppBoard_Waveplayer

A wave player demo setup for the mbed application board. Reads a wave file from a USB flash drive, and outputs to the onboard speaker (very low volume) and the analog audio out jack that can be connected to a set of PC speakers for more volume. A wave file for the demo is available at Copy it to the USB drive. See for more details and instructions.

mbed application board boom box audio demo using PC speakers to play the audio file from the USB flash drive. The green PC speaker audio connector is plugged directly into the board's audio out jack.

Using Headphones

A set of PC headphones can also be plugged into the board’s audio out jack. Modify the line below at the end of wave_player.cpp to boost the volume a bit by removing the “>>3”.

    //scale down Analog voltage by 8 for PC speakers

Without a driver circuit or audio amplifier, the volume will probably still be a bit low on most headphones.

Ideas for further enhancements

Put several files on the USB stick. Read the directory and display files names on the LCD display. Develop a user interface using the joystick to select files to play.

Use another direction on the joystick or one of the blue pots to add a volume control.

Using a breadboard, add support for MP3 and other audio format files. Sparkfun makes an MP3 decoder chip breakout board and there are code examples in the cookbook. There are also some software only MP3 player code examples in the cookbook, but memory for the RAM buffers is a bit limited. There is probably not enough RAM left to also support networking and/or the RTOS in an application, if mbed is doing software only MP3 decoding.

Play short audio files using on-chip Flash memory using a conversion tool that reads *.wav files and extracts the data value to setup a C array. See Using Flash to play Audio clips.

Here is a quick demo using the on-board speaker with PWM:

Import programflash_audio_player_8bitPWM

Plays audio samples from an array stored in flash. Values are from an audio *.wav file that was automatically converted to a C array data source file. 8-bit samples used in this version and a PWM output pin (instead of AnalogOut)

3 comments on Application Board Waveplayer Demo:

22 Jan 2016

Hi. i tried the code and it compiles with an error. Warning: Statement is unreachable in "USBHost/FATFileSystem/ChaN/ff.cpp", Line: 1689, Col: 14

What is the problem? I have placed the DIP switches in the Host position and that did nothing.

22 Jan 2016

It Ok now. Was using a 32Gig flash drive. Used a smaller 1Gig drive and it works. if i play a large file the play rate is slowed bu 1/4 maybe. Can this be fixed?


19 Oct 2016

Might need to down sample that wave file to 8-16K per second using Audacity - see the using a speaker for audio output wiki page for a bit more info on that. Other issues are likely in the mbed USB file system driver library. Could try to update to the most recent version of it.

Please log in to post comments.