Adafruit Mono 2.5W Class D Audio Amplifier - PAM8302


PAM8302 breakout board from Adafruit

The PAM8302 is a 2.5-W mono filter-free class D audio power amplifier with a differential input. Class D amplifiers use PWM for power efficiency. This one uses a 250 kHz PWM output with an H-bridge output driver to drive a small speaker. It has been used in cellphones and tablets. The breakout board has a tiny volume control pot and an optional power shutdown pin that can be used to turn it off. This demo will use it as is, without additional modifications.
Class D is probably the best option for new designs. It is more power efficient and does not need as many large external capacitors as the linear options such as the LM386 audio amp IC require. Pricing for the surface mount PAM8302 part is also less than the LM386. Any PWM output pin or a D/A output pin can be used to provide the audio input from the mbed.

Hello Audio World Demo

Just about any speaker can be used. Wires could be soldered on a small speaker that could be plugged into a breadboard or attached to the screw terminals on this module.
Small Sparkfun Speaker

Sparkfun also has a handy speaker that with plug into a breadboard, but be careful not to overdrive it. The volume control pot might be needed, or the input audio level could be reduced in hardware or software. On small speakers like this, use the 3.3V and not the 5V power supply for the amp power pin. Sparkfun also has an audio jack breakout board that could be used for earphones or headphones.
Small breadboard friendly speaker from Sparkfun


The small Sparkfun PCB speaker works great in a breadboard and is very handy, but when removing it from a breadboard be careful to pry the bottom up first with a something flat like a screwdriver. If you pull it out hard using the top black metal speaker cover, the PCB on the back cover can pull off and break the tiny speaker wires soldered to the pins inside the back cover.


gndgnd and A -
Vout (3.3V) or 5VVin
p26 (PWM)A +
4-8ohm(screw terminal)+
4-8ohm(other screw terminal)-
Any DigitalOut px(optional)SD (low for shutdown)


This audio amp board can draw too much power and an external power supply is needed at high volume levels. The mbed LEDs will blink on and off when drawing too much power. Be careful when adjusting the volume control pot it is easy to bend and make a bad connection on the pot wiper. A very tiny 1/16 inch flat blade screwdriver is needed to adjust the pot (CCW turns volume down). If using multiple power supplies, don't forget to tie the grounds together.

This demo program will play a song on the speaker using a PWM output pin. If an application needs to play a sound or a long sequence of notes (i.e., a song) and continue to execute other code, a timer interrupt can be used so that the call returns immediately and the song continues to play using timer interrupts while other code can execute in main. The new SongPlayer class in this example program sets up the timer with the Timeout API and plays a song passed to it in two arrays. The note array contains the frequency and a duration array that contains the duration of each note. The example uses p26 for the speaker PWM output. The song plays while the LED flashes back in main. The class in SongPlayer.h sets up the timer interrupt for the next note's duration and sends the new note frequency data to the PWM hardware. A note with a 0 duration ends the song. If long songs are used, making the arrays "const" will save RAM by putting the data in flash.
If the amp is placed in standby mode by pulling the SD pin low with a DigitalOut pin, the amp will power down (with no connection it defaults to "on"). The song demo plays once. Hit reset to play again.

#include "mbed.h"
#include "SongPlayer.h"

// Song test program - plays a song using PWM and timer interrupts
// for documentation see
// can be used to play a song, if you have the notes and durations
// for musical note frequencies see

//Set up notes and durations for sample song to play
// A 0.0 duration note at end terminates song play
float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
                 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
                     0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0

DigitalOut led1(LED1);
int main()
// setup instance of new SongPlayer class, mySpeaker using pin 26
// the pin must be a PWM output pin
    SongPlayer mySpeaker(p26);
// Start song and return once playing starts
    // loops forever while song continues to play to end using interrupts
    while(1) {
        led1 = !led1;

Import programsong_demo_PWM

Plays a song using PWM and timer interrupts. Returns once song starts.

When PWM is used for audio output, the PWM clock is typically around 10 times the highest frequency in the audio output.


This code example increases the PWM clock frequency for audio output use. This can interfere with other devices (i.e., RC servos) using PWM outputs that expect slower clocks. Most mbed platforms have only one PWM clock for all PWM outputs. If this is an issue, the other code can be modified or the D/A output pin can be used instead, but a significantly faster update rate is required when using the D/A for audio output of simple tones instead of PWM. The mbed cookbook also has some software PWM options.

There is also an mbed wavefile player audio code example (along with more audio examples) that reads *.wav files from the SD card and uses the D/A output pin or it can be modified to use a PWM output. A USB flash drive can also be used, but the mbed's internal flash drive is a bit too slow for audio files.

For simple devices that require only new a few seconds of audio, it is possible to use on-chip flash memory instead of a *.wav file on an uSD card for audio playback. See Using Flash to Play Audio Clips for examples and tools to use.

Please log in to post comments.