How to Change Audio Library pins?

Hi there. I recently purchased the teensy 4.x audio board and will be using a teensy 4.1 for this project. Unfortunately, I have already used some of the pins that would have been used by the audio board, so I need to remap. (I will be using the built-in SD for the 4.1). Here is the remapping I decided on:

AUDIO BOARD => Teensy 4.1
DIN (p7) => RX7 (p28)
DOUT (p8) => TX8 (p29)

Gnd => Gnd
3.3v => 3.3v
MCLK (p23) => MCLK1 (p23)
BCLK (p21) => BCLK1 (p21)
LRCLK (p20) => LRCLK (p20)
SCL (p19) => SCL1 (pin 16)
SDA (p18) => SDA1 (pin 17)


So the main difference is that the pins for DIN/DOUT and SCL/SDA are configured to different pins on the 4.1. I downloaded the audio library and began looking through the various classes and am a bit overwhelmed on how to change these pin declarations. I'll keep at it at the meantime, but I was wondering if anyone has any recommendations to make this process go smoother, or if there is an easier way to change the pins in the library.

Thank you for your time.
 
I figured out that I can atleast modify the control_sgtl5000.cpp file and replace all "Wire" calls with "Wire1" instead. Still looking into DIN/DOUT...
 
Ok... I think I might have this now. In input_i2s.cpp, I changed the line "CORE_PIN8_CONFIG = 3; //1:RX_DATA0" to "CORE_PIN28_CONFIG = 3; // RX_DATA0 (New DIN on pin 28, RX7)"

and in output_i2s.cpp I changed the line "CORE_PIN7_CONFIG = 3; //1:TX_DATA0" to "CORE_PIN29_CONFIG = 3; // 1:TX_DATA0 (New DOUT on pin 29, TX8)"

Hopefully that will be enough to make things work. I'll report back on my findings...
 
Ok... I think I might have this now. In input_i2s.cpp, I changed the line "CORE_PIN8_CONFIG = 3; //1:RX_DATA0" to "CORE_PIN28_CONFIG = 3; // RX_DATA0 (New DIN on pin 28, RX7)"

and in output_i2s.cpp I changed the line "CORE_PIN7_CONFIG = 3; //1:TX_DATA0" to "CORE_PIN29_CONFIG = 3; // 1:TX_DATA0 (New DOUT on pin 29, TX8)"

Hopefully that will be enough to make things work. I'll report back on my findings...
This isn't going to work. The audio pins are connected to SAI1 and can't be arbitrarily reassigned. You will need to check the reference manual to see which other pins are possible substitutes, and then hope they're exposed as physical pins on the T4.1.
 
This isn't going to work. The audio pins are connected to SAI1 and can't be arbitrarily reassigned. You will need to check the reference manual to see which other pins are possible substitutes, and then hope they're exposed as physical pins on the T4.1.
Gotcha. So in that case using RX2/TX2 would be the most straightforward way since those were the original pins. I'll see if I can reconfigure my board and move some pins around...

Would my Wire1 change for SCL1/SDA1 work btw?
 
Dout and Din can only be the following pins:
  • 6 (OUT1D)
  • 7 (OUT1A), default DOUT
  • 8 (IN1), default DIN
  • 9 (OUT1C)
  • 32 (OUT1B)
  • 38 (Alternate for IN1)
  • 39 (Alternate for OUT1A)
MCLK1, LRCLK1, and BCLK1 all must be their respective pins (23, 20, and 21).

Alternatively, you could switch to using the second I2S bus:
  • 2 (OUT2)
  • 3 (LRCLK2)
  • 4 (BCLK2)
  • 5 (IN2)
  • 33 (MCLK2)
If you switch to the 2nd I2S bus, I imagine there will be a lot of changes needed.

I presume you can move the I2C pins (18, 19) to the 2nd I2C bus (17/16 or the alternates 44/45 underneath the Teensy) or the 3rd I2C bus (25, 24), providing you modify the sources to use Wire1 or Wire2 instead of Wire.

If you are modifying the library, it is generally a good idea to rename the files, so that future updates don't wipe out your work. However other parts of the library may pull in the original sources.
 
Dout and Din can only be the following pins:
  • 6 (OUT1D)
  • 7 (OUT1A), default DOUT
  • 8 (IN1), default DIN
  • 9 (OUT1C)
  • 32 (OUT1B)
  • 38 (Alternate for IN1)
  • 39 (Alternate for OUT1A)
MCLK1, LRCLK1, and BCLK1 all must be their respective pins (23, 20, and 21).

Alternatively, you could switch to using the second I2S bus:
  • 2 (OUT2)
  • 3 (LRCLK2)
  • 4 (BCLK2)
  • 5 (IN2)
  • 33 (MCLK2)
If you switch to the 2nd I2S bus, I imagine there will be a lot of changes needed.

I presume you can move the I2C pins (18, 19) to the 2nd I2C bus (17/16 or the alternates 44/45 underneath the Teensy) or the 3rd I2C bus (25, 24), providing you modify the sources to use Wire1 or Wire2 instead of Wire.

If you are modifying the library, it is generally a good idea to rename the files, so that future updates don't wipe out your work. However other parts of the library may pull in the original sources.
Makes sense. I went ahead and rewired it so DIN and DOUT are 7 & 8 respectively. Your latter point on the i2c pins is exactly what I was thinking too. I changed control_sgtl5000.cpp to use Wire1 instead of Wire.
 
DIN (p7) => RX7 (p28)
DOUT (p8) => TX8 (p29)

As others mentioned, the hardware can't map the I2S data (normally pins 7 and 8) to pins 28 and 29. But there is hardware support inside the chip to use pins 38 and 39. Remapping the signals requires writing directly to the pin mux registers.
 
So I just bought the sd card and loaded up the example music files. The music starts playing but very quickly goes to harsh static. I am running jumper cables from my teensy 4.1 to the board. Would this be the cause of the issue? I am using a Sandisk Ultra Plus 32gb sd card (this is what I had lying around). This is the sketch I am running:

Code:
// Advanced Microcontroller-based Audio Workshop
//
// http://www.pjrc.com/store/audio_tutorial_kit.html
// https://hackaday.io/project/8292-microcontroller-audio-workshop-had-supercon-2015
//
// Part 1-3: First "Hello World" program, play a music file
//
// WAV files for this and other Tutorials are here:
// http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html

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

AudioPlaySdWav           playSdWav1;
AudioOutputI2S           i2s1;
AudioConnection          patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection          patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;

// // Use these with the Teensy Audio Shield
// #define SDCARD_CS_PIN    10
// #define SDCARD_MOSI_PIN  11
// #define SDCARD_SCK_PIN   13

//Use these with the Teensy 3.5 & 3.6 SD card
#define SDCARD_CS_PIN    BUILTIN_SDCARD
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used

// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN    4
//#define SDCARD_MOSI_PIN  11
//#define SDCARD_SCK_PIN   13


void setup() {
  Serial.begin(115200);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  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);
    }
  }
  delay(1000);
}

void loop() {
  if (playSdWav1.isPlaying() == false) {
    Serial.println("Start playing");
    playSdWav1.play("SDTEST2.WAV");
    delay(10); // wait for library to parse WAV info
  }
  // do nothing while playing...
}
 
Last edited:
Yah I think the wire length is the issue. Was able to hookup my teensy directly and that did the trick. Guess I'll rewire my setup. Not a big deal. Thanks for the help everyone.
 
Had the chance to get back into the swing of things. Hooked up the Adafruit MAX9744 audio amplifier with the 3.5mm audio jack between it and the Teensy audio board. I'm powering the amplifier with 12v and the teensy with 5 volts through VIN. Everything works great! At least until now.

Every once and a while when I turn on the teensy/audio board and amp, the audio board doesn't do anything and I hear a faint, but consistent thump sound from the speaker. This failure happens almost randomly. Can't trigger any audio, can't get my other non-audio components working-- just nothing.

I suspect a few things. One, maybe the audio board seems to fail in the setup() every once in a while? Secondly, there may be something happening when both the audio board and the amplifier are turned on at the same time, causing this startup failure. I'm not sure why, but unplugging and replugging the 3.5 jack seems to help.

Perhaps its a power issue? I'm using a 12v battery that splits into two directions: one direction steps the voltage down to 5v for the teensy and the other direction has 12v go directly to the audio amp. As I mentioned, this setup seems to work most of the time, but not all the time. Or perhaps the amount of other non-audio stuff happening in setup() might be mucking up the audio's setup.

I tried adding a delay in the setup to maybe give the sd card more time to load, but that didn't seem to do anything. I'll keep diagnosing the issue and come up with some better language to describe what's happening, because right now it is difficult to do so.
 
This is variable instantiation before the setup:
Code:
AudioPlaySdWav           playWav1;
AudioOutputI2S           audioOutput;
AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
AudioControlSGTL5000     sgtl5000_1;

// Use these with the Teensy 3.5 & 3.6 & 4.1 SD card
#define SDCARD_CS_PIN    BUILTIN_SDCARD
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used

This is my setup code for the audio board:
Code:
//TEENSY AUDIO
AudioMemory(8);
sgtl5000_1.enable();
sgtl5000_1.volume(1);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) while (1) delay(500);

For reference, the other non-audio peripherals include a Pololu Maestro. When I move my servos, I do actually hear a faint noise produced by the speaker which is a little odd... But again, this does not happen all the time which is the strange thing.
 
Are you using Line Out, or Headphone connector from Audio Adaptor?

If using Line Out, that should work.

If using Headphone connector on Audio Adaptor, this connector "ground" pin is not Ground. It's a reference voltage for the audio chip output and is designed to be connected to a set of headphones (a floating ground), not to a power supply ground referenced input of an external amplifier. This info is written on Audio Adaptor PJRC webpage, and it's written on bottom of same pcb.

If you need to use the audio output from Audio Adaptor headphone connector, then you need to install 220uF a capacitor in series in both the left and right signal outputs and then connect external amplifier ground to Audio Adaptor digital ground (same thing as power supply ground in this case). Leave the Audio Adaptor headphone connector ground disconnected (floating).

See similar thread:
Audio Adaptor connected to wrong ground
 
Are you using Line Out, or Headphone connector from Audio Adaptor?

If using Line Out, that should work.

If using Headphone connector on Audio Adaptor, this connector "ground" pin is not Ground. It's a reference voltage for the audio chip output and is designed to be connected to a set of headphones (a floating ground), not to a power supply ground referenced input of an external amplifier. This info is written on Audio Adaptor PJRC webpage, and it's written on bottom of same pcb.

If you need to use the audio output from Audio Adaptor headphone connector, then you need to install 220uF a capacitor in series in both the left and right signal outputs and then connect external amplifier ground to Audio Adaptor digital ground (same thing as power supply ground in this case). Leave the Audio Adaptor headphone connector ground disconnected (floating).

Yah i'm using the headphone output connector, so that is probably why. Thanks for the advice. I'll start getting into the process of making those changes.
 
Are you using Line Out, or Headphone connector from Audio Adaptor?

If using Line Out, that should work.

If using Headphone connector on Audio Adaptor, this connector "ground" pin is not Ground. It's a reference voltage for the audio chip output and is designed to be connected to a set of headphones (a floating ground), not to a power supply ground referenced input of an external amplifier. This info is written on Audio Adaptor PJRC webpage, and it's written on bottom of same pcb.

If you need to use the audio output from Audio Adaptor headphone connector, then you need to install 220uF a capacitor in series in both the left and right signal outputs and then connect external amplifier ground to Audio Adaptor digital ground (same thing as power supply ground in this case). Leave the Audio Adaptor headphone connector ground disconnected (floating).

See similar thread:
Audio Adaptor connected to wrong ground
I think I might just solder on some pins and use the LINE OUT instead of reconfiguring the headphone jack and connect to the LINE-IN on my audio amp. Seems easier. So knowing that I shorted the VGND and GND, have I permanently mucked up my board, or can I still use it? Thanks for your time
 
The Audio Adaptor might have survived the shorting of VGND and GND. I looked at Adafruit amp pcb schematic and they added capacitors on their L/R connector signal inputs. This inclusion may have saved the Audio Adaptor. However, their AGND is connected to GND using a ferrite bead (essentially shorting grounds together from a DC perspective).

I'd still go ahead and try the Line Outs. They're on separate SGTL5000 pins, capacitor AC-coupled already, ground referenced, and likely still usable. You'll know pretty quickly after you re-configure your circuit if Audio Adaptor is a happy camper. It shouldn't mis-behave if all is well. And I think you'll hear an improvement in audio quality (bonus).

Let us know how it goes.
 
The Audio Adaptor might have survived the shorting of VGND and GND. I looked at Adafruit amp pcb schematic and they added capacitors on their L/R connector signal inputs. This inclusion may have saved the Audio Adaptor. However, their AGND is connected to GND using a ferrite bead (essentially shorting grounds together from a DC perspective).

I'd still go ahead and try the Line Outs. They're on separate SGTL5000 pins, capacitor AC-coupled already, ground referenced, and likely still usable. You'll know pretty quickly after you re-configure your circuit if Audio Adaptor is a happy camper. It shouldn't mis-behave if all is well. And I think you'll hear an improvement in audio quality (bonus).
Sounds good. I'll report back once I have everything hooked up.
 
The Audio Adaptor might have survived the shorting of VGND and GND. I looked at Adafruit amp pcb schematic and they added capacitors on their L/R connector signal inputs. This inclusion may have saved the Audio Adaptor. However, their AGND is connected to GND using a ferrite bead (essentially shorting grounds together from a DC perspective).

I'd still go ahead and try the Line Outs. They're on separate SGTL5000 pins, capacitor AC-coupled already, ground referenced, and likely still usable. You'll know pretty quickly after you re-configure your circuit if Audio Adaptor is a happy camper. It shouldn't mis-behave if all is well. And I think you'll hear an improvement in audio quality (bonus).

Let us know how it goes.
Works perfectly now! Thank you for the help.
 
Good to hear you've got it working.
Always a nice solution when you don't have to move Teensy pins around to solve a problem (much harder to do when Audio Adaptor is soldered onto Teensy).
 
Back
Top