I have a music player via an ESP32 and an SD card module
Structure:
SD card module (with the .wav files) -> ESP32
ESP32 pin 26 to Audio amplifier module LM386 ¹ IN and ESP32 GND to Audio amplifier module LM386 ¹ GND and from Audio amplifier module LM386 ¹ GND and VCC to the external power supply constant 5V
From audio amplifier module GND and OUT to the loudspeaker (8 Ohm 100W)
Problem:
When I want to play the .wav files from the SD card via the ESP32, I always get a loud noise when it plays and only the music of the .wav file in the background
Does any one know where the nois is comming from?
Or have any one a idea to fix that problem (Software or Hardware)
Code (is just the code snippet):
bool startAudioPlayback(const String& pfad) {
// Vorhandene Instanzen löschen, um Speicherlecks zu vermeiden
if (wav != nullptr) {
wav->stop();
delete wav;
wav = nullptr;
}
if (file != nullptr) {
delete file;
file = nullptr;
}
if (out != nullptr) {
delete out;
out = nullptr;
}
// Neue Instanzen erstellen
out = new AudioOutputI2S();
out->SetPinout(27, 26, 26); // Anpassen der Pins je nach ESP32 Setup
out->SetGain(0.1); // Setzt die Lautstärke auf 10%
out->SetRate(480000); // Common sample rates are 44100 or 48000 Hz
out->SetBitsPerSample(16); // Use 16 for CD quality audio
out->SetChannels(2); // Set to 2 for stereo
file = new AudioFileSourceSD(pfad.c_str()); // Dateipfad als C-String übergeben
if (!file->isOpen()) {
Serial.println("Fehler: Datei konnte nicht geöffnet werden");
delete file;
file = nullptr;
delete out;
out = nullptr;
return false;
}
wav = new AudioGeneratorWAV();
bool success = wav->begin(file, out);
if (success) {
Serial.println("Spiele Audio ab...");
while (wav->isRunning()) {
if (!wav->loop()) {
wav->stop();
}
delay(1); // Kürzere Verzögerung, um die Schleife effizienter zu machen
}
Serial.println("Audiowiedergabe abgeschlossen.");
} else {
Serial.println("Fehler beim Starten der Audiowiedergabe");
}
// Instanzen löschen
delete wav;
delete file;
delete out;
// Instanzen auf NULL setzen, um wiederholte Zugriffe zu verhindern
wav = nullptr;
file = nullptr;
out = nullptr;
return success;
}
[1]: https://www.amazon.de/Audioverst%C3%A4rkerplatine-200-facher-Verst%C3%A4rkung-5V-12V-Audioverst%C3%A4rkermodul-LM386-Leistungsverst%C3%A4rkerplatine/dp/B08DFTKXZB/ref=sr_1_2?dib=eyJ2IjoiMSJ9.w4JTbZVCPJIwx9uNR_-Ag4sTaXw2PY1fitWMQE0-fp8sfIWgNgZEW1pjJWMAwKi2CmQeTBXRrDkW-Sg-Tkr9UkgaHO4ujT_hYoUkE49aKWNFb_CdTAr1NPsALd9k2EArAK0qFlIi_d-IeSI4TBPvDJ-QnzE5jiatiyE8bWiczKEkhrh1KdBGLlF3U00YKVfOCvz7cHlfD2Gon_VXaA-LmJKDfcsyVIJJo1eaj3TC56M.r7B5_bZdT3-EgnMFbssroRLLSpd6CSNsR4wSWo8d3mQ&dib_tag=se&keywords=arduino%20amplifier&qid=1724055090&sr=8-2