Skip to content

Speaker produces crackling/distorted audio with M5Unified #371

Open

Description

@chate

Title: Speaker produces crackling/distorted audio with M5Unified

Environment:

  • Arduino IDE: 2.3.10
  • M5Stack Board: 3.3.9
  • M5Unified: 0.2.22
  • Device: M5Stack Basic
  • OS: Mac

Issue Description:
When using M5Unified 0.2.22, the speaker produces crackling/distorted audio (buzz)
when playing tones or notes. The same speaker works cleanly with M5Stack.h library,
indicating a problem with M5Unified's speaker driver implementation, not hardware.

My code

#include <M5Unified.h>

// Note frequencies
#define C4 262
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define B4 494

#define MSEC_PER_MIN 60000
int melody[] = { G4, G4, G4, D4, E4, E4, D4, B4, B4, A4, A4, G4 };
int noteValues[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int numNotes = sizeof(melody)/sizeof(melody[0]);

int tempo = 120;  // normal tempo BPM

void setup() {
  // initialize M5Stack and speaker
  M5.begin();
  M5.Speaker.setVolume(64); // 128
  M5.Display.setTextSize(3);
  M5.Lcd.println("Press BtnA to play");
}

void loop() {
  M5.update();
  if (M5.BtnA.wasPressed()) {
    for (int i = 0; i < numNotes; i++) {
      int playTime = (noteValues[i]*(MSEC_PER_MIN / tempo));

      M5.Speaker.tone(melody[i], playTime);
      delay(playTime);
      delay(50);
    }
  }
}

Old Environment:

  • Arduino IDE: 2.3.10
  • M5Stack Board: 2.1.4
  • M5Stack: 0.4.6
  • Device: M5Stack Basic
  • OS: Mac

Code for old environment:

#include <M5Stack.h>

// Note frequencies
#define C4 262
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define B4 494

#define MSEC_PER_MIN 60000
int melody[] = { G4, G4, G4, D4, E4, E4, D4, B4, B4, A4, A4, G4 };
int noteValues[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int numNotes = sizeof(melody)/sizeof(melody[0]);

int tempo = 120;  

void setup() {
  // initialize M5Stack and speaker
  M5.begin();
  M5.Speaker.setVolume(4);
  M5.Lcd.setTextSize(3);
  M5.Lcd.println("Press BtnA to play");
}

void loop() {
  M5.update();
  if (M5.BtnA.wasPressed()) {
    for (int i = 0; i < numNotes; i++) {
      int playTime = (noteValues[i]*(MSEC_PER_MIN / tempo));

      M5.Speaker.tone(melody[i]);
      delay(playTime);
      M5.Speaker.mute();
      delay(50);
    }
  }
}

Actual Behavior:
Audio produces crackling/distorted sound (buzz) overlaid on the tone. While using old M5Stack board 2.1.4 + M5Stack libary 0.4.6 produces clean, clear audio without any distortion.

Workaround Attempted:

  • Reducing volume with M5.Speaker.setVolume(64) - doesn't fix
  • Using M5.Speaker.playRaw() instead of tone() - still produces distortion
  • Config M5.Speaker
auto cfg = M5.Speaker.config();
cfg.magnification = 8;      
cfg.sample_rate = 44100;    
cfg.dma_buf_len = 256;      
M5.Speaker.setConfig(cfg);

doesn't fix

Please help me how to fix this problem.

Activity

  1. ainyan03 commented on Oct 7, 2026

    @ainyan03
    Contributor

    Thank you for the report and the detailed sketches.

    The noise at the end of each tone has been fixed in the develop branch (#391), and the fix will be included in the next release.

    The cause was in the speaker task of M5Unified: after each sound, the internal DAC output briefly dropped and jumped back before fading out, which was heard as crackling. It did not depend on your sketch. We confirmed the fix on an M5Stack Basic.

    Please note that M5Unified produces sound in a different way from the old M5Stack library, so the result will not be exactly the same:

    • The old library drove the speaker pin directly with a PWM square wave (LEDC), at full scale.
    • M5Unified generates the waveform in software and outputs it through I2S and the 8-bit internal DAC. The level is scaled by the volume setting, and the output is faded in and out around each sound to reduce noise.

    tone() plays a sine wave by default, so it sounds softer than the old square wave. If you prefer a sound closer to the old one, you can pass square-wave data to tone() and raise the volume:

    static const uint8_t square_wav[16] = { 255, 255, 255, 255, 255, 255, 255, 255, 1, 1, 1, 1, 1, 1, 1, 1 };
    M5.Speaker.tone(melody[i], playTime, -1, true, square_wav, sizeof(square_wav));
  2. ainyan03 commented on Oct 13, 2026

    @ainyan03
    Contributor

    The fix (#391) is released in M5Unified 0.2.26. Please update the library and try your sketch again; the crackling at the end of each tone should be gone. If it is, I believe this issue can be closed.

  3. chate commented on Oct 20, 2026

    @chate
    Author

    5. The fix (#391) is released in M5Unified 0.2.26. Please update the library and try your sketch again; the crackling at the end of each tone should be gone. If it is, I believe this issue can be closed.

    I've updated to M5Unified 0.2.26, but the crackling at the end of each tone is not gone.

    One observation:
    With M5Unified 0.2.22 not all M5Stack modules have crackling sound

    • M5Stack device that have crackling sound, produce loud sound
    • some M5Stack that do not have crackling sound, produce low sound and have loud click sound and the end of each note sound.

    After update to 0.2.26, the behavior of each M5Stack device is still the same.

  4. ainyan03 commented on Oct 23, 2026

    @ainyan03
    Contributor

    Thank you for testing 0.2.26 and for the detailed observation.

    We found two more pops on the internal DAC and fixed them in #415 (merged into the develop branch, it will be in the next release):

    • at the end of each tone: the fade-out was made of a few coarse steps
    • at the start of a tone after a pause: the DAC was switched off during the pause and stepped when it was switched on again

    With the develop branch, the 12-tone sketch from this issue plays without pops between the notes on our M5Stack Basic.

    To check whether the remaining difference between your units has another cause, could you tell us:

    1. The version of each unit (printed on the label on the back, e.g. "M5Stack Basic V2.7")
    2. When the noise is heard on the loud units: at the start of a note, at the end, or during the whole note

    If you can try the develop branch, please let us know whether the crackling or the click changes on each unit.

  5. ainyan03 commented on Oct 26, 2026

    @ainyan03
    Contributor

    Thank you for your patience — the fix for the pops on the internal DAC (#415) has been released in M5Unified 0.2.28. Please update to 0.2.28 (it requires M5GFX 0.2.35 or later) and check whether the crackling or the click changes on each unit.

    If the noise remains on some units, the two questions above (the version printed on the back of each unit, and whether the noise is heard at the start, at the end, or during the whole note) would help us find the cause.

    Release: https://github.com/m5stack/M5Unified/releases/tag/0.2.28

  6. chate commented on Oct 28, 2026

    @chate
    Author

    After update to 0.2.28, the noise still remains.

    This is before update (v0.2.22) : https://youtube.com/shorts/1sP20U7R6ks

    This is after update to v0.2.28 : https://youtube.com/shorts/HLs_u3JAs2g

  7. chate commented on Oct 28, 2026

    @chate
    Author

    This is the module that has no buzz but low sound. https://youtube.com/shorts/9xNy_0Acxk0?feature=share
    Same behavior for 0.2.22 and 0.2.28.

    -chate

  8. ainyan03 commented on Oct 28, 2026

    @ainyan03
    Contributor

    Thank you for the videos and for testing 0.2.28.

    The same sketch and library version sound different on your units: on some the notes are distorted for their whole length, while another unit plays them without that distortion. To narrow down the cause, could you check the following on a distorted unit?

    1. With the old M5Stack library, does it play cleanly, or is it distorted as well? (Note that the old library drives the speaker with a full-scale square wave, so its sound is harsher and hides distortion more easily.)
    2. With M5Unified at a lower volume, e.g. M5.Speaker.setVolume(32), does it play cleanly, or is it still distorted?
    3. The version printed on the label on the back of each unit (e.g. "M5Stack Basic V2.7").

    If the behavior differs between units running the same firmware, the cause is most likely on the hardware side, either a fault or a variation between units (for example in the speaker or the amplifier circuit), rather than in the library.

  9. chate commented on Nov 3, 2026

    @chate
    Author
    1. With the old M5Stack library, does it play cleanly, or is it distorted as well? (Note that the old library drives the speaker with a full-scale square wave, so its sound is harsher and hides distortion more easily.)
      With the old M5Stack library, both unit play cleanly.

    2. With M5Unified at a lower volume, e.g. M5.Speaker.setVolume(32), does it play cleanly, or is it still distorted?
      It is still distored.

    3. The version printed on the label on the back of each unit (e.g. "M5Stack Basic V2.7").
      2AN3WM5Stack-CORE

    I've tried with 0.2.29. It is still distorted.

  10. chate commented on Nov 4, 2026

    @chate
    Author

    I have tried new hardware with 0.2.29. It looks ok. Some of the old hardwares have buzz sound.

    All hardwares work fine with old M5Stack library.

  11. ainyan03 commented on Nov 9, 2026

    @ainyan03
    Contributor

    Thank you for the answers. We think the noise you hear may be the expected sound of the default sine wave, not a fault.

    By default, tone() plays a sine wave through the 8-bit internal DAC. To reproduce the smooth waveform with only 8 bits, M5Unified adds a fine noise-shaping pattern (delta-sigma), which is heard as a hiss on top of the tone. The old M5Stack library played a square wave, which has no such hiss.

    Could you try square-wave data with tone(), as below, on your units?

    static const uint8_t square_wav[16] = { 255, 255, 255, 255, 255, 255, 255, 255, 1, 1, 1, 1, 1, 1, 1, 1 };
    
    M5.Speaker.setVolume(64);  // up to 255 for a louder sound
    M5.Speaker.tone(melody[i], playTime, -1, true, square_wav, sizeof(square_wav));

    We tested the sketch from this issue on five older M5Stack Core units (GRAY and FIRE, board revisions 2018.2A and 2018.3), comparing the default sine wave with the old library's square wave. Four units played both without distortion. On one of them we also tried square-wave data with tone(): at volume 64 and at 255 it sounded the same as the old library to our ears. The remaining unit was noisy even with the old library's method, so we think it has a hardware problem, most likely in the speaker.

    If a unit is still distorted with square-wave data, please let us know. In that case the cause is most likely on the hardware side of that unit.

  12. chate commented on Nov 9, 2026

    @chate
    Author

    Test with square wave
    Old M5Stack Core - still have buzz sound after each note.
    New M5Stack Core - clear sound, louder than sine wave.

    I have create my own library to simulate M5Stack https://github.com/chate/M5SpeakerPWM.
    The result is similar, clear and loud, in both old and new M5Stack Core units.

  13. ainyan03 commented on Nov 13, 2026

    @ainyan03
    Contributor

    Thank you for testing the square wave and for sharing M5SpeakerPWM.

    The buzz after each note on the older units probably comes from how M5Unified stops the speaker output between notes: the internal DAC output is ramped down and then switched off, and on some older units this transition may be audible. A PWM output like your library does not go through this step, which would explain why it sounds clean on both. We have not confirmed this on a unit that shows the buzz, so please take it as our current best guess.

    We are considering a separate, simpler output that plays a plain square wave through LEDC, similar to your library. We can't give a schedule yet. Until then, your library is a good way to avoid the noise. If M5.Speaker has already been used in the same sketch, call M5.Speaker.end() before driving the speaker pin with LEDC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions