I2S audio output interference from analogWrite

steve9211

Member
Hi! I'm using a Teensy 4.1 and a MAX98357A amplifier breakout connected to the Teensy using I2S. My ultimate goal is to flash an LED using PWM in accordance with sound playing through a speaker. I haven't implemented FFT yet because when I try to use analogWrite, the sound output becomes garbled. Using the following sketch which does not call analogWrite, the audio is clear and everything functions how I would expect:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioPlaySdWav           playSdWav1;     //xy=230,309
AudioMixer4              mixer1;         //xy=511,399
AudioOutputI2S           audioOutput;    //xy=755,392
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord2(playSdWav1, 1, mixer1, 0);
AudioConnection          patchCord7(mixer1, 0, audioOutput, 0);

#define SDCARD_CS_PIN    BUILTIN_SDCARD
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used

void setup() {
  Serial.begin(9600);
  AudioMemory(12);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }

  pinMode(2, OUTPUT);
}

void loop() {
  if (playSdWav1.isPlaying() == false) {
    playSdWav1.play("THUNDERCLAP.WAV");
  }
  for (int i = 0; i < 255; i++) {
    // analogWrite(2, i);
    delay(10);
  }
}

However, when I call analogWrite using the following code, the sound becomes garbled/staticky but the LED fades as expected:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioPlaySdWav           playSdWav1;     //xy=230,309
AudioMixer4              mixer1;         //xy=511,399
AudioOutputI2S           audioOutput;    //xy=755,392
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord2(playSdWav1, 1, mixer1, 0);
AudioConnection          patchCord7(mixer1, 0, audioOutput, 0);

#define SDCARD_CS_PIN    BUILTIN_SDCARD
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used

void setup() {
  Serial.begin(9600);
  AudioMemory(12);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }

  pinMode(2, OUTPUT);
}

void loop() {
  if (playSdWav1.isPlaying() == false) {
    playSdWav1.play("THUNDERCLAP.WAV");
  }
  for (int i = 0; i < 255; i++) {
    analogWrite(2, i);
    delay(10);
  }
}

I've tried hard to find out what's going on and just can't figure it out. It seems like something happens when analogWrite will be called to block audio playback. Any troubleshooting advice is appreciated! Happy to post pictures and audio if someone thinks it would be helpful.
 
The mixer routing is incorrect: both the left and right WAV channels are connected to the same mixer input (input 0). It may be not the reason, but I'd fix this issue.
 
The mixer routing is incorrect: both the left and right WAV channels are connected to the same mixer input (input 0). It may be not the reason, but I'd fix this issue.
Good catch! That did clear up some of the issue. The updated code is below.

To better describe the issue, the stuttering is now gone after @Mcu32 's great catch! However, lower-volume sounds become static. The audio track is supposed to be a thunder clap. After the initial clap there is smaller rumbling that sounds as expected without calling analogWrite but that sounds like static pops when I do call it as below.

Is analogWrite and I2S known to interfere with each other? I looked hard and couldn't find anything on it, so I guess it's still something on my end but I'm stumped!

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     //xy=326,251
AudioOutputI2S           audioOutput;    //xy=652,254
AudioConnection          patchCord1(playSdWav1, 0, audioOutput, 0);
AudioConnection          patchCord2(playSdWav1, 1, audioOutput, 1);
// GUItool: end automatically generated code


#define SDCARD_CS_PIN    BUILTIN_SDCARD
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used

void setup() {
  Serial.begin(9600);
  AudioMemory(12);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }

  pinMode(2, OUTPUT);
}

void loop() {
  if (playSdWav1.isPlaying() == false) {
    playSdWav1.play("THUNDERCLAP.WAV");
  }
  for (int i = 0; i < 255; i++) {
    analogWrite(2, i);
    delay(10);
  }
}
 
I have no idea… maybe it's a hardware issue unrelated to the Teensy board? (outside the board.. sry sometimes my English leaves me)
Have you tried using a simpler code without the SD card? I'd start by trying a simple sine wave. Maybe that will help narrow down the problem.
 
Excellent idea! I removed the SD card, used another Teensy 4.1, and a different speaker. I also tried powering the amp from a power supply, not from the Teensy. I tried the tone sweep example for the Teensy library and still got the same result: the tone sounded right but with static. However, when I unplug the LED, the static immediately stops.

All of this makes me think that there is some problem with the actual PWM signal causing problems for audio playback. When the electricity starts flowing, the static starts. Does that give any other clue or give you an idea about what I can try?
 
Okay, I have solved the problem but I would like to know why! At first, the Teensy was powered by USB and provided power for the LED and speaker amp and I had the static problem. Then I got an external 5V power supply. I powered the Teensy via VIN and the speaker amp through a buck converter that I measured producing ~3.3V. The LED anode(?) was connected to buck converter ground bus (I realize this was a mistake but I don't think it affected the result).

I am now powering the LED fully from the Teensy and the amp fully from the buck converter and the problem has disappeared! I am happy with the result but I wonder if you have any idea why this might have happened. I appreciate any insight and your help so far!
 
Without knowing the exact circuit, it’s difficult to say. What is the current draw of the connected components, particularly the amplifier?

I’m not really a hardware expert myself. I think others here could answer this question better if you provide a few more details.
 
What exactly do you mean by "powering the LED fully from the Teensy"? Teensy's pins are only rated to source/sink around 4ma of current, most LEDs require more than that...
 
What exactly do you mean by "powering the LED fully from the Teensy"? Teensy's pins are only rated to source/sink around 4ma of current, most LEDs require more than that...
Good catch! Usually for my projects I use external power supplies for breakouts and other things so I didn't really think about individual pin current limits. I mean that the cathode(?) was on a digital pin and the anode(?) was on ground. I could see how that might have been interrupting audio playback if it was drawing too much power, but when the amp was fully powered separately from the Teensy, the problems stopped.

Anyway, you all are very kind to help and I don't want to needlessly take up more of your time! I was just curious in case there was a clear or easy answer.

Really, thank you all so much for helping out on the forums! You're so important to these kinds of ecosystems!
 
Pretty likely you had a "ground loop" problem.

It's a very common issue with all audio systems. MIDI uses an optocoupler input for this reason, so the input has no physical connection which can cause a ground loop. Most professional audio gear uses balanced signals, which largely cancel out these sorts of problems.

Ground loop refers to the situation where something other than your audio signal has a current which flows through the ground path your audio signal uses. If that non-audio current is "large" relative to the total impedance of the ground path, it induces small voltages at the input of your amplifier. Your amp can't know about this, it just amplifies the combined signal it receives at its input, so you end up hearing a combination of the audio you wanted and some extra noises related to whatever whatever is causing the extra current flow.

Probably not critical now that you've found a solution. And also not 100% certain this is what really happened, but it's quite likely. Hopefully this at least helps answer the lingering question about what (probably) caused this issue.
 
Another approach to reducing the effect of ground loops is to lower the resistance of the ground wiring - however note that connectors often have substantial resistance (compared to wires) and they may need upgrading too. But first try to break the loops (but not at the risk of losing protective earth safety).
 
Back
Top