The USBAudio class enables the mbed to be recognized as an audio device. With this interface, you can receive and send audio packets from and to a computer (play a music,...) over USB. For instance you can connect a speaker or an I2S/I2C chip to the mbed and play the stream received from the computer.

The USB connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Change the default sound board

To send audio packets to the mbed, you have to change the default sound board used by the Operating system.
On Windows, you can do this by clicking on:

  • control panel
  • Hardware and Sound
  • Manage audio device in the Sound section
  • Select the Mbed Audio device and press Set default

Hello World

Import program

// Hello World example for the USBAudio library
#include "mbed.h"
#include "USBAudio.h"
Serial pc(USBTX, USBRX);
// frequency: 48 kHz
#define FREQ 48000
// 1 channel: mono
#define NB_CHA 1
// length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
#define AUDIO_LENGTH_PACKET 48 * 2 * 1
// USBAudio
USBAudio audio(FREQ, NB_CHA);
int main() {
    int16_t buf[AUDIO_LENGTH_PACKET/2];
    while (1) {
        // read an audio packet *)buf);
        // print packet received
        pc.printf("recv: ");
        for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
            pc.printf("%d ", buf[i]);


Import library

Public Member Functions

  USBAudio (uint32_t frequency_in=48000, uint8_t channel_nb_in=1, uint32_t frequency_out=8000, uint8_t channel_nb_out=1, uint16_t vendor_id=0x7bb8, uint16_t product_id=0x1111, uint16_t product_release=0x0100)
float  getVolume ()
  Get current volume between 0.0 and 1.0.
bool  read (uint8_t *buf)
  Read an audio packet.
bool  readNB (uint8_t *buf)
  Try to read an audio packet.
bool  write (uint8_t *buf)
  Write an audio packet.
bool  readWrite (uint8_t *buf_read, uint8_t *buf_write)
  Write and read an audio packet at the same time (on the same frame)
void  attach (void(*fptr)(void))
  attach a handler to update the volume
template<typename T >
void  attach (T *tptr, void(T::*mptr)(void))
  Attach a nonstatic void/void member function to update the volume.

More examples

The following program is sending to a speaker all audio packets received. This means that you can play a music on your computer and listen it on your Mbed.

Import programUSBAUDIO_speaker

USBAudio speaker example

The USBAudio playback example sends back to the computer all audio packets received. You can then listen for incoming audio packets with audacity for instance.

Import programUSBAudioPlayback

USBAudio example: playback

In details

Audio packet length

In this section, I will explain what kind of packets are received according to the frequency and the number of channels.

An audio packet is received each millisecond. So let's say that a frequency of 48 kHz has been chosen with 2 channels (stereo). Knowing that each sample of a packet are 16 bits long, 48 * 2 bytes will be received each millisecond for one channel. In total, for 2 channels, 48 * 2 * 2 bytes will be received.

Compute the length packet

AUDIO_LENGTH_PACKET = (FREQ / 500) * nb_channel

How to interpret an audio packet ?

The read() function fills an uint8_t array. But these data has to be interpreted as 16 bits signed data (PCM). Then PCM values can be handled according to the number of channels.

MONO: single channel


STEREO: 2 channels

When there are 2 channels, values for channel 1 and values for channel 2 will alternate as explained in the following diagram:


1 related question:


30 Nov 2011

Could you please add microphone code, we have lots of speaker code for lpc1768 but no basic usb pc microphone code:)

01 Dec 2011

Hello this does not work with windows 7, Could you please tell me how do I get it to work with windows 7 thanks.

Not getting recognized


01 Dec 2011


I am using Windows 7 32 bits and I have no problem with the Hello World and the speaker program. I will try tonight on a 64 bits machine.

Have you tried another USB class (USBMouse for instance), to see if the problem is specific to the USBAudio class ?



01 Dec 2011

I have Windows 7 32 bit too and USB examples like mouse, keyboard, hid... works great.

02 Dec 2011

I have it working now. I managed to get a full retail copy of windows 7, I was using a lite version of windows 7:)

01 Jan 2012

Hallo everyone..

I got the USB connection working OK and I can see it functioning on PC...

thanx it works well...

please does anyone know how to get that audio Out to (p18) instead ?

as I need to use I2C_one, for an IO expander...and I2C_two, to control audio fader control circuit

01 Feb 2012

Sam can you help me with this.

02 Feb 2012

writeNB takes four arguments:

  • the first argument is the endpoint
  • the second: the buffer to write
  • the third: the number of bytes to be written
  • the fourth: the maximum number of bytes that can be written on the endpoint specified

In your case, you can use:

  USBDevice::writeNB(EP3IN, (uint8_t *)&feedback_value, 4, PACKET_SIZE_ISO);


02 Feb 2012

If the feedback endpoint is an interrupt endpoint, then (on the LPC1768) do you need to use EP1IN and MAX_PACKET_SIZE_EPINT in the writeNB ? This from USBEndpoints_LPC17_LPC23.h

02 Feb 2012

Jonathon Fletcher wrote:

If the feedback endpoint is an interrupt endpoint, then (on the LPC1768) do you need to use EP1IN and MAX_PACKET_SIZE_EPINT in the writeNB ? This from USBEndpoints_LPC17_LPC23.h

not sure but I think Isoc transfer is allowed on limited endpoints, 3, 6, 9 and 12 logical endpoints for LPC17xx USB device.

I think it is easier for me to some how get

if (interface == 1 & alternate == 1) {  

working, the interrupt is seeming like to much of a task for me, I admit it I can't code my way out of a paper bag:)

18 Mar 2012


I am getting this error, when using the updated USBDevice libs

"integer conversion resulted in truncation" in file "/main.cpp", Line: 19, Col: 40

USBAudio audio(FREQ, NB_CHA, 0x7180, 0x7500);

can you help please, when will you update the new main.cpp file, to go with the updated USBDevice lib.

19 Mar 2012

Hi Philips Philips,

The modif has been done. Note that you have now a speaker and a microphone in the new USBAudio library.


22 Mar 2012

Samuel Joseph wrote:

Hi Philips Philips,

The modif has been done. Note that you have now a speaker and a microphone in the new USBAudio library.


Hi thanks very much Sam,

You have not modif the main.c for both microphone and speaker yet tho:( I have never seen an example for both microphone and speaker yet, and after searching the internet for 3 days now I can not find any examples:(

22 Mar 2012


Just pushed a playback example: the mbed sends back to the computer all audio packets received. The program is here and is also provided in the "More examples" section in this page.


22 Mar 2012

How about a Record example Sam, so I can record something into Sweep audio editor or audacity or sony sound forge.

27 Sep 2012

will you ever make the code availble so the mbed can be a normal usb soundcard with mic and speaker, how much would it cost money to add the microphone to the speaker code.

27 Sep 2012


I have this project in my workspace:

Import programUSBAudio_micro

USBAudio example using a microphone

I don't really know the state of this program but it's worth trying it :)

Cheers, Sam

28 Sep 2012

Hi Samuel,

I will absolutely try this. Tahnk You very much.

21 Jan 2013

i have a problem: i have roasted my old mbed and got a new one, i have reinstalled the serial driver and flashed the exactly same software as in the old one, inserted it in the (undamaged) PCB again and its generates an "unidentified USB device" message when connected on secondary USB (D+/D- [p31,p32]) for the USB audio device.

whith the old mbed it worked just fine and i just dont know what went wrong

using win7 home premium 64bit and an mbed LPC1768

i'd be glad for a fast response :)

[im using an school login for working, hence the different name]

fg Thomas Steiner

Posting new comments for this page has been disabled