Question: How to Eliminate Noise in Audio Playback Using ESP32 with I2S Configuration?

I am working on an audio playback project using an ESP32 configured with I2S. The code compiles and uploads successfully, and the ESP32 is connected directly to a speaker. The audio plays fine, but there is persistent noise accompanying the audio.

Here’s what I’ve tried to resolve the issue:

  1. Amplifier Circuit: I suspected that overcurrent from the ESP32 might be causing the noise, so I built an amplifier circuit. However, the noise was still present and amplified along with the audio.

  2. Filter Circuits: I used low-pass and high-pass filters, but the noise persisted. The filters only altered the noise's intensity, not eliminating it.

I would like to understand:

What could be causing this noise?

Are there hardware or software solutions to eliminate the noise while maintaining clear audio output?

Below is the code I am using for reference:

#include <WiFi.h>
#include <AudioFileSourceHTTPStream.h>
#include <AudioGeneratorMP3.h>
#include <AudioOutputI2S.h>

const char* ssid = "Aswana_03";
const char* password = "Ashu310_";
const char* audio_url = "http://192.168.215.138:5500/mp3_song.mp3";

AudioGeneratorMP3 *mp3;
AudioFileSourceHTTPStream *file;
AudioOutputI2S *out;

float volume = 0.5; // Default volume level (range: 0.0 to 1.0)

// Smoothing factor for volume adjustments
float targetVolume = 0.5; // Target volume level
float smoothingFactor = 0.1; // Adjust how smoothly volume changes (0.0 to 1.0)

void setup() {
Serial.begin(115200);

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");

// Configure audio output to DAC
file = new AudioFileSourceHTTPStream(audio_url);
out = new AudioOutputI2S();
out->SetOutputModeMono(true);  // Use a single DAC for mono audio
out->SetPinout(0, 0, 25);     // DAC output on GPIO25
out->SetGain(volume);         // Set initial volume

mp3 = new AudioGeneratorMP3();
mp3->begin(file, out);

Serial.println("Audio playback started. Use '+' to increase volume, '-' to decrease volume.");

}

void loop() {
if (mp3->isRunning()) {
mp3->loop();

    // Smoothly adjust volume
    if (abs(volume - targetVolume) > 0.01) {
        volume += (targetVolume - volume) * smoothingFactor;
        out->SetGain(volume);
    }

    // Check for serial input to change target volume
    if (Serial.available() > 0) {
        char command = Serial.read();
        if (command == '+') {
            targetVolume += 0.1;
            if (targetVolume > 1.0) targetVolume = 1.0;
            Serial.printf("Target volume increased to: %.2f\n", targetVolume);
        } else if (command == '-') {
            targetVolume -= 0.1;
            if (targetVolume < 0.0) targetVolume = 0.0;
            Serial.printf("Target volume decreased to: %.2f\n", targetVolume);
        }
    }
} else {
    mp3->stop();
    while (1);  // Stop program after playback
}

}

Any suggestions or solutions would be greatly appreciated!

"Noise" is subjective. What you think is noise might be ignored when I hear it. Use an oscilloscope to actually identify and classify what you think is noise. Only then can something be done to minimize it.

That looks questionable.

Failing attempts are not much interesting.

Schematics might tell us why.

Likely, yes.

Not likely.

Did you get the version that has DOLBY built in?

I've done a couple projects using the ESP32 for audio playback and never noticed any noise.

When you say "speaker" do you mean a standard 8 or 4 ohm speaker? If so, that's too much of a load on the output. This could be causing distortion and might be the source of what you're hearing.

The built in DAC is only 8-bits so don't expect much in terms of sound quality. It's possible that the noise you're hearing is because of the low resolution DAC. I've only used the internal DAC for alerts (voice alarms and tones) but I never heard any noise.

An external 16-bit I2S DAC sounds much better and I have streamed music from my phone to the ESP32 and gotten high quality audio output that way.

Also, try playing back the mp3 on a PC or something with better quality sound to be sure that it's not in the source file.

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