The PAM8403 is a very small and efficient Class D amplifier that can drive passive speakers with up to 3 Watts. In this tutorial we will connect a PAM8403 directly to an ESP32 and play some simple sounds using its internal DACs (digital analog converters).
Generally a passive buzzer is a better choice for simple sounds but if you need louder or better sound quality (e.g. for a door bell) driving a small speaker directly via an ESP32 and a PAM8403 is an option.
For more demanding applications such as a web radio, a Bluetooth player or an MP3 player you need to use the I2S interface of the ESP32 and connect to an external DAC and amplifier. See the Audio with PAM8403, PCM5102 and ESP32 or the Playing Audio with ESP32 and MAX98357 tutorials for more details.
Required Parts
You will need a a PAM8403 Amplifier Board, an ESP32 and one or two passive speakers. The PAM8403 can produce stereo sound but the main reason for two speakers would be the increased volume. For simple sounds, quality is of no concern and any 3 Watt speakers with 4 or 8 Ohm will do.
If you want to control the volume you can use a 10K Ohm potentiometer. In the case of stereo sound you will need either two of those or a dual gang potentiometer. Also we will use a push button for track control. See the listings below.
I picked an ESP32 lite as a microcontroller but any other ESP32 boards will be fine as well. To connect everything together a breadboard and some wires will be handy. However, for best sound quality you should solder the wires.

PAM8403 Amplifier

2 x Speaker 3 Watt 8 Ohm

10K Ohm Potentiometer

10K Ohm Potentiometer (dual gang)

Push Buttons

ESP32 lite

USB Data Cable

Dupont Wire Set

Breadboard
Makerguides is a participant in affiliate advertising programs designed to provide a means for sites to earn advertising fees by linking to Amazon, AliExpress, Elecrow, and other sites. As an Affiliate we may earn from qualifying purchases.
Technical Features of the PAM8403 module
The PAM8403 is a small, Class-D, stereo audio amplifier IC. You can buy the raw IC but it is easier to work with a PAM8403 module that has the required external components already integrated. For the technical details of the PAM8403 IC see the following link:
Class-D amplifiers use high-frequency pulse-width modulation (PWM) switching to convert an incoming analog audio signal into digital drive for speakers. Such amplifiers are highly efficient approaching 90 %. Another advantage is that they are very small and don’t get hot due.
This makes the PAM8403 module well suited for battery-powered and Arduino or ESP32 projects where both board space and power consumption are constrained.
Note, however, that the PAM8403 is a very small amplifier that can drive only 3 Watt speakers. It is not very loud and certainly not suitable for Hi-Fi audio. Typical applications are sounds for toys, door bells or small voice agents, for instance.
Electrical Characteristics and Power Delivery
The PAM8403 module accepts a single DC supply voltage within a range from 2.5 V up to 5.5 V. At this nominal 5 V supply rail, and when driving loudspeakers rated at 4 Ω, each channel can produce up to about 3 Watts of output power.
Signal Inputs, Gain, and Frequency Response
The audio input interface on a PAM8403 module has two line-level inputs for the left and right channels. The gain of the amplifier is fixed internally with a typical value around 24 dB, meaning that modest input signal levels on the order of a few hundred millivolts peak-to-peak are sufficient to drive the outputs near full power without clipping.
Pinout of the PAM8403 Amplifier Module
The picture below shows the pinout of the PAM8403 module. The speaker outputs are at the top, while audio input and power supply are at the bottom of the board. You can see the PAM8403 IC in the center of the board.

Protection, Thermal Behavior, and Module Layout
The PAM8403 IC incorporates several protection mechanisms to improve reliability. These include thermal shutdown, short-circuit protection to limit current in the event of an overload on the speaker outputs, and under-voltage lockout.
The module’s passive component network supports stable PWM operation and suppresses electromagnetic interference (EMI) to acceptable levels without requiring external filter components.
Because the class-D chip runs efficiently, it does not normally require an external heatsink for typical loads.
Technical Specification
The following table summarizes the Technical Specification of the PAM8403:
| Parameter | Specification |
|---|---|
| Amplifier Type | Class-D stereo audio amplifier |
| Number of Channels | 2 (Left and Right) |
| Supply Voltage Range | 2.5 V to 5.5 V DC |
| Typical Output Power | Up to ~3 W per channel into 4 Ω at 5 V, THD+N = 10 % |
| Output Power @ 8 Ω | ~1.8 W per channel into 8 Ω at 5 V, THD+N = 10 % |
| Total Harmonic Distortion + Noise | ~0.15 % at 1 W into 8 Ω, 5 V supply |
| Gain (Closed-Loop) | ~24 dB |
| Signal-to-Noise Ratio (SNR) | ~80 dB |
| Quiescent Current | ~8–16 mA at 5 V |
| Shutdown Current | < 1 µA |
| Power Supply Ripple Rejection (PSRR) | ~-58 dB at 1 kHz |
| Efficiency | ~80–90 % depending on load |
| Operating Temperature | -40 °C to +85 °C |
| Protection Features | Short-circuit protection, thermal shutdown |
| Load Impedance | 4 Ω to 8 Ω recommended |
| Typical Switching Frequency | ~260 kHz |
Connecting the PAM8403 to an ESP32
In this section you will learn how to connect a PAM8403 amplifier module to an ESP32. Let’s start with a quick look at the overall architecture of the system we are going to build.
The digital audio signal is created by the software running on the ESP32. The ESP32 has two internal DACs (digital analog converters) that are connected to GPIO 25 and GPIO 26, respectively. The DACs convert the digital audio signal into to an analog signal, which is then amplified by the PAM8403 to drive one or two speakers. The picture below shows the architecture of the system:

Note that the pins GPIO 25 and GPIO 26 for the DACs are fixed and cannot be changed via software.
Mono Sound
The simplest circuit is for mono audio output with a single speaker. Connect the speaker to the R+ and R- pins of the PAM8403. Next connect GPIO25 to the R (right channel) input of the PAM8403. Finally, connect G and 3V to the power supply pins (+, -) of the PAM8403. Don’t worry that the power supply pins for the PAM8403 are labelled with 5V. The PAM8403 works with 3.3V or 5V.

Alternatively to the right channel you could also use the L+ and L- pins for the left channel and connect GPIO26 to the left channel input (L) of the PAM8403. You just need to tell the software, which channel to use.
Make sure that you match the polarity of the output pins with the polarity of the speaker pins. Typically the speaker pins are marked with “+” and “-” signs or have differently shaped pins (thinner for minus).

If there are no markings, you can connect a 1.5V AA battery and if the membrane moves outwards, the plus pole of the battery indicates the plus pole of the speaker.
Mono Sound with Volume Control
Despite the small 3 Watt speaker the PAM8403 can get pretty loud. You could control the volume via software but it usually affects the sound quality negatively. The better option is to add a resistor or a potentiometer and reduce the voltage of the input signal. The circuit below shows you how to connect a 10K potentiometer for volume control:

Start by connecting ground (G) of the ESP32 to one of the outer pins of the potentiometer. Connect the other outer pin of the potentiometer to GPIO25 and connect the middle pin (the wiper) to the right channel (R) input of the PAM8403.
Swap the connections to the outer pins if you want to change the direction for turning the volume up. I used a 10K Ohm linear potentiometer but you could also use a logarithmic potentiometer, which provides a more natural relation between the perceived loudness and the setting of the potentiometer.
Note that you also can get the PAM8403 module with a volume control knob (potentiometer) already integrated (Amazon), if you don’t want to build the circuit yourself.
Stereo Sound
The PAM8403 supports stereo sound. Connecting the second channel works the same as connecting the first channel. The only difference is that you need to connect GPIO26 to the left input channel (L) of the PAM8403. Then connect the second speaker to the L+ and L- outputs of the PAM8403 as shown below:

You don’t need to connect ground (⊥) of the PAM8403 to ground (G) of the ESP32.
Stereo Sound with Volume Control
Volume control for stereo works the same as for mono. You will need a second potentiometer for the other channel. With two potentiometers you can control the volume of the right and left channel independently. Alternatively, you can get a dual gang potentiometer, which allows you to set the volume for both channels simultaneously. The circuit below shows you how to connect two potentiometers for volume control:

A dual gang potentiometer would be connected in the same way, since it is just two single gang potentiometers stack on top of each other with a single axle and turning knob.
Installing Libraries
In some of the following code examples we will use the arduino-audio-tools library by Phil Schatzmann. To install it go to the arduino-audio-tools repo, click on the green “<> Code” button and then “Download ZIP” to download the library as a ZIP file as shown below:

Then open a Sketch, go to Sketch -> Include Library -> Add .ZIP Library … to install the downloaded ZIP library (arduino-audio-tools-main.zip):

If this is the first time you program an ESP32 board from your Arduino IDE, you will need to install the ESP32 core as well. For details see the Install ESP32 core in Arduino IDE tutorial.
Code: Testing left and right Channel
Once you have connected the PAM8403 to the ESP32 you can test that everything is working correctly using the following code:
/*
www.makerguides.com
Libraries:
- ESP32 Core 3.3.7
*/
#define DAC_CH1 25 // Right channel
#define DAC_CH2 26 // Left channel
void setup() {}
void loop() {
for (int i = 0; i < 180; i++) {
uint8_t amp = 128 + 127 * sin(2.0 * PI * i / 180.0);
dacWrite(DAC_CH1, amp);
dacWrite(DAC_CH2, amp);
}
}
It generates a continuous sine wave and plays it over the right and left channel of the PAM8403. You should hear a tone coming from the left and the right speaker. You can comment out the “dacWrite(DAC_CH1, amp)” or the “dacWrite(DAC_CH2, amp)” line to disable the left or right channel.
This code is sufficient for a simple test but does not create a reliable tone. Since the waveform is generated as fast as the CPU can run the frequency is undefined and unstable. In the next example we use a library to create stable sounds with a specified frequency.
Code: Creating Tones with different Frequencies
The following code generates a tone with a 440 Hertz frequency. It uses the arduino-audio-tools library, which you need to have installed beforehand.
/*
www.makerguides.com
Libraries:
- ESP32 Core 3.3.7
- [arduino-audio-tools](https://github.com/pschatzmann/arduino-audio-tools)
*/
#include "AudioTools.h"
AnalogAudioStream dac;
SineWaveGenerator<int16_t> sine;
GeneratedSoundStream<int16_t> sound(sine);
StreamCopy copier(dac, sound);
void setup() {
auto cfg = dac.defaultConfig(TX_MODE);
cfg.sample_rate = 44100;
cfg.channels = 2;
cfg.bits_per_sample = 16;
dac.begin(cfg);
sine.begin(cfg, 440.0); // 440 Hz
//sine.setFrequency(1000.0); // 1000
//sine.setAmplitude(100); // 0...32767
}
void loop() {
copier.copy();
}
The main objects of the code are an AnalogAudioStream object representing the hardware audio output, a SineWaveGenerator that produces waveform samples, and a StreamCopy object that transfers generated samples to the DAC. Together, they form a simple audio pipeline where data flows from the generator to the output.
In the setup function we configure the parameters of the audio stream. The sample rate sets how many samples per second are generated. The channels = 2 setting configures stereo output and the bits_per_sample = 16 parameter defines the resolution of each audio sample. Note, however, that the DACs of the ESP32 have only 8 bit resolution and will downscale the audio signal.
Once configured, dac.begin(cfg) initializes the hardware with these parameters. The sine wave generator is then started with sine.begin(cfg, 440.0). The value 440.0 specifies the frequency in hertz, producing the standard A4 tone. The commented lines show you how to change the frequency or amplitude, if necessary.
In the loop() function, copier.copy() continuously moves audio samples from the generator to the DAC. While the code is more complex than the simple test example before, it also allows you to build more advanced sound applications.
Code: Playing a Melody
In this last example we let the ESP32 play a simple melody: “All My Little Ducklings” or “Alle meine Entchen” as it is known in German:
/*
www.makerguides.com
Libraries:
- ESP32 Core 3.3.7
- [arduino-audio-tools](https://github.com/pschatzmann/arduino-audio-tools)
*/
#include "AudioTools.h"
// Note Definitions (Frequencies in Hz)
#define NOTE_C4 261.63
#define NOTE_D4 293.66
#define NOTE_E4 329.63
#define NOTE_F4 349.23
#define NOTE_G4 392.00
#define NOTE_A4 440.00
AnalogAudioStream dac;
SineWaveGenerator<int16_t> sine;
GeneratedSoundStream<int16_t> sound(sine);
StreamCopy copier(dac, sound);
void playNote(float freq, int durationMs) {
sine.setFrequency(freq);
unsigned long t = millis();
while (millis() - t < durationMs) copier.copy();
// Separation between notes
sine.setFrequency(0);
unsigned long g = millis();
while (millis() - g < 40) copier.copy();
}
void setup() {
auto cfg = dac.defaultConfig(TX_MODE);
cfg.sample_rate = 44100;
cfg.channels = 2;
cfg.bits_per_sample = 16;
dac.begin(cfg);
sine.begin(cfg, 0.0f);
}
void loop() {
// Phrase 1: Alle meine Entchen...
playNote(NOTE_C4, 400);
playNote(NOTE_D4, 400);
playNote(NOTE_E4, 400);
playNote(NOTE_F4, 400);
playNote(NOTE_G4, 800);
playNote(NOTE_G4, 800);
// Phrase 2: Schwimmen auf dem See...
playNote(NOTE_A4, 400);
playNote(NOTE_A4, 400);
playNote(NOTE_A4, 400);
playNote(NOTE_A4, 400);
playNote(NOTE_G4, 800);
// Phrase 3: Schwimmen auf dem See... (Repeat)
playNote(NOTE_A4, 400);
playNote(NOTE_A4, 400);
playNote(NOTE_A4, 400);
playNote(NOTE_A4, 400);
playNote(NOTE_G4, 800);
// Phrase 4: Köpfchen in das Wasser...
playNote(NOTE_F4, 400);
playNote(NOTE_F4, 400);
playNote(NOTE_F4, 400);
playNote(NOTE_F4, 400);
playNote(NOTE_E4, 800);
playNote(NOTE_E4, 800);
// Phrase 5: Schwänzchen in die Höh'
playNote(NOTE_D4, 400);
playNote(NOTE_D4, 400);
playNote(NOTE_D4, 400);
playNote(NOTE_D4, 400);
playNote(NOTE_C4, 800);
delay(5000);
}
This code builds directly on the previous example by using the AudioTools pipeline to generate and stream a sine wave to the DAC. But instead of playing a constant tone, it dynamically changes the frequency over time to create a melody.
The key addition is the playNote() function, which turns the continuous tone generator into a simple note player. When called, it sets the sine wave frequency using sine.setFrequency(freq). It then enters a timed loop where copier.copy() is repeatedly called, just like in the previous program’s loop().
After the note duration has elapsed, the function briefly sets the frequency to zero, which produces silence and acts as a separator between notes. This small pause makes the melody sound more natural.
In the loop() function, playNote() is called repeatedly with different frequencies and durations, corresponding to musical notes such as C4, D4, and A4.
The sound quality is good enough for a toy or you could use it to build a simple door bell. Below is an example for a door bell sound:
void loop() {
playNote(783.99, 150, 30);
playNote(659.25, 150, 30);
playNote(523.25, 300, 50);
playNote(392.00, 800, 200);
delay(5000);
}
And that’s it. Now you know how to create sounds and simple melodies with an ESP32 and amplify them with a PAM8403.
Conclusions
In this project, you learned how to play audio using the ESP32 and the PAM8403 to generate simple sounds.
You can play music from memory on an ESP32 with the PAM8403 as amplifier but it is complicated. You would need to convert the music to WAV and then to a data structure. See this ESP 32 Based Audio Player tutorial for details.
Furthermore, the DACs of the ESP32 have a resolution of only 8 bits. This is low compared to typical audio DACs that use 12, 16, or more bits. Consequently the sound quality not very good especially when reducing the volume in software.
For more demanding audio applications such as a web radio, a Bluetooth player or an MP3 player with Hi-Fi quality you need to use an I2S capable DAC. See the following tutorials for more information:
- TDA7379 Class AB Audio Amplifier with ESP32
- High-Power ESP32 Audio with TPA3116D2 and PCM5102
- Audio with PAM8403, PCM5102 and ESP32
- Stereo Amplifier with TPA31110 XH-A232, PCM5102 and ESP32
- Playing Audio with ESP32 and MAX98357
If you have any questions, feel free to leave them in the comment section.
Happy Tinkering ; )
FAQ
Q: Why does my PAM8403 audio sound noisy or distorted?
The PAM8403 is a Class-D amplifier, which means it switches rapidly to generate audio and is therefore very sensitive to noise on the power supply and signal lines.
When it is used together with the ESP32’s internal DAC, which has limited resolution and introduces noticeable quantization noise and non-linearity.
The result is often audible hiss, buzzing, or distortion, especially if the wiring is not optimized or the power supply is not properly filtered.
Q: Should I add capacitors to the PAM8403 power pins to improve sound quality?
Yes, adding proper decoupling capacitors is one of the most important steps. Since the amplifier draws fast switching currents that can create voltage dips and noise on the supply line, which directly affects the audio output quality.
Use this wiring:
VCC -- 100nF -- GND VCC -- 100µF -- GND
The 100nF ceramic capacitor should be placed as close as possible to the power pins of the amplifier to suppress high-frequency switching noise.
The larger electrolytic capacitor provides energy storage and smooths slower voltage fluctuations, and together they stabilize the supply voltage and significantly reduce unwanted noise in the audio signal.
Q: Could I add capacitors on the input between ESP32 and PAM8403 for better sound?
Yes, because the ESP32 DAC output contains a DC offset and high-frequency noise components. You could use a coupling capacitor and a simple filter network to clean up the signal before it reaches the amplifier. Below is an example circuit:
ESP32_DAC -- 1µF --+-- AMP_IN | 10kΩ | GND
The series capacitor blocks DC voltage from the DAC, while the resistor to ground defines a stable reference level for the amplifier input. This combination forms a high-pass filter that improves signal stability and reduces distortion.
You could also add an optional noise filter:
AMP_IN -- 220pF -- GND
This small capacitor removes high-frequency noise from the DAC output, which helps reduce audible hiss and improves clarity, especially at low volume levels.
Q: Can a separate power supply help with better sound?
Yes, because the ESP32 generates digital switching noise and RF interference that can travel through the shared power supply and directly enter the amplifier. Using a filtered or separate supply can significantly improve sound quality. Here is a simple filter example:
5V -- 10Ω --+-- AMP_VCC | 220µF | GND
The resistor limits noise currents coming from the ESP32 side, and the capacitor provides a local energy reservoir for the amplifier. This effectively creates a low-pass filter for the power line, which reduces ripple and noise and results in cleaner audio output.
Q: Is the ESP32 internal DAC limiting my sound quality?
Yes, the internal DAC is usually the main bottleneck in this setup, because it has a fairly low resolution of 8 Bit and limited accuracy. This introduces quantization noise and distortion that cannot be fully removed by filtering. Even with perfect hardware design, the audio quality will still be constrained by the DAC performance.
Q: How can I increase volume without distortion?
To increase volume while keeping the sound clean, you need to ensure that the signal amplitude from the ESP32 is as large as possible without clipping. At the same time you need to provide the amplifier with enough supply voltage and must ensure the speaker load is appropriate.
The DAC range is: 0 … 255 (8-bit DAC). You should use most of this range to maximize signal strength, but avoid hitting the limits continuously, as that causes clipping.
You should power the amplifier with 5V if possible because it can deliver more output power compared to 3.3V operation.
The PAM8403 provides the most power (3W) into 4 Ohm speakers. If you are using 8 Ohm speakers, you will get less volume (roughly 1.5W).
Finally, a speaker sitting on a desk will sound quiet and thin. Mounting the speaker in a sealed or ported box drastically increases the perceived volume and bass response.
Q: What is the best way to improve both volume and sound quality?
The most effective improvement is to replace the analog DAC output with a digital I2S audio interface. This removes the low-quality DAC stage inside the ESP32 and allows the use of higher-quality external audio hardware that produces a much cleaner signal.
Q: What is an I2S amplifier and how do I connect it?
An I2S amplifier integrates a high-quality digital-to-analog converter and a power amplifier in a single device. It receives digital audio data directly from the ESP32 and converts it to a speaker signal with much better quality and lower noise. A common example is the MAX98357A.
Because the signal stays digital until the final stage, this setup avoids most analog noise problems and produces clearer and louder sound with minimal external components.
Q: What if I want stereo sound with better quality?
If you need stereo output and higher fidelity, you should use an external I2S DAC that provides separate left and right analog signals, which can then be amplified using a suitable amplifier. A good example is the PCM5102A.
ESP32 (I2S) --> DAC --> Amplifier --> Speakers
This setup provides much higher resolution and better channel separation, resulting in clearer and more detailed sound.
Q: What wiring practices improve sound quality?
Good wiring is very important, because noise can easily couple into the signal path if wires are too long or poorly arranged. You should keep signal wires short, avoid running them parallel to speaker wires, and use a proper grounding scheme.
ESP32_GND ----+---- AMP_GND | DAC_GND
This ensures that noise currents do not flow through sensitive signal paths and helps maintain a clean reference ground for the audio signal.
Q: What setup gives the best result overall?
The simplest high-quality setup is:
ESP32 --> I2S amplifier (MAX98357A) --> speaker
Have a look at the Playing Audio with ESP32 and MAX98357 tutorial for more information.
For even better performance and true stereo sound, a more advanced setup is:
ESP32 --> PCM5102A DAC --> amplifier --> speakers
See the High-Power ESP32 Audio with TPA3116D2 and PCM5102 or TDA7379 Class AB Audio Amplifier with ESP32 tutorial, for instance.
Stefan is a professional software developer and researcher. He has worked in robotics, bioinformatics, image/audio processing and education at Siemens, IBM and Google. He specializes in AI and machine learning and has a keen interest in DIY projects involving Arduino and 3D printing.

