Repository navigation
Open
Description
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 oftone()- 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.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Speaker: keep the DMA fed at the hold level while waiting for new data
ainyan03 commentedon Oct 7, 2026
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:
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 totone()and raise the volume:ainyan03 commentedon Oct 13, 2026
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.
chate commentedon Oct 20, 2026
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
After update to 0.2.26, the behavior of each M5Stack device is still the same.
ainyan03 commentedon Oct 23, 2026
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):
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:
If you can try the develop branch, please let us know whether the crackling or the click changes on each unit.
ainyan03 commentedon Oct 26, 2026
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
chate commentedon Oct 28, 2026
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
chate commentedon Oct 28, 2026
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
ainyan03 commentedon Oct 28, 2026
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?
M5.Speaker.setVolume(32), does it play cleanly, or is it still distorted?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.
chate commentedon Nov 3, 2026
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.
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.
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.
chate commentedon Nov 4, 2026
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.
ainyan03 commentedon Nov 9, 2026
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?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.
chate commentedon Nov 9, 2026
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.
ainyan03 commentedon Nov 13, 2026
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.Speakerhas already been used in the same sketch, callM5.Speaker.end()before driving the speaker pin with LEDC.