How can I play a WAV file from an Arduino Nano ESP32 using an LM386?

I have a arduino nano esp32 connected to an sd card modul and can read wav files.

I also have connected an LM386 to the digital pin D9. But i find no way to play a wav file. The TMRpcm does not work and seams very old.

Have someone an example for me to play a wav file from the nano esp32.

Very first hit on an internet search using the phrase "esp32 audio output", among many others:

I don't know if TMRpcm supports the ESP32 but the ESP32 has a DAC so you shouldn't be using TMRpcm (which isn't true analog).

I've never actually done this but WAV files are "simple". You read the samples and write them to the DAC at the correct sample rate (44,000 samples per second, etc.), and that's about it.

The complications are... You read bytes from a file and you may have 16 or 24-bit audio, or stereo, and the bytes have to be "re-assembled" correctly.

Once you have the samples they have to be re-scaled so the number of bits matches the actual DAC.

And since the DAC can't put-out negative voltages, the sample data has to be biased before it's sent to the DAC (except for 8-bit WAV files which use unsigned data which is already biased).

On the hardware side, the LM386 has way too much (voltage) gain so you'll need a voltage divider or volume control on the input. You do need the "power gain" to drive a regular speaker.

Another way to play WAV files on ESP32 - using it I2s digital audio output. To play digital sound you will need a I2s audio module, like a MAX98357A
Please see this tutorial:

Thanks for the explanations and the new ideas. I will try it with the MAX98357A.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.