troubleshooting simple project

Hi, I'm trying to figure out why my project for my daughter's Valentine's day box isn't working. We used this project on YouTube. I *think* I wired/soldered everything correctly. The idea is a Valentine's box that looks like a dog house. When the IR sensor is tripped, the speaker is supposed to play a sound effect of a dog barking. I 3d printed a dog house and the plan was to hot glue the sensor near the slot at the top of the house to detect when a card/Valentine was inserted into the house. The only change from the YouTuber's code is the type of sound effect (the file name of the .wav file). I have Teensy loader and Arduino IDE, which appear to be communicating with the Teensy 4.1. This is the speaker, this is the board, and this is the IR sensor.

The green wire goes from pin 12 to the speaker and the yellow wire goes from pin 29 to the IR sensor. The sd card only has 1 file on it, DOG.WAV

Do I need to format the sd card in a certain way for the Teensy to read it? Any ideas for troubleshooting the system would be very much appreciated. I am not a programmer. How can I confirm that the Teensy has the code and/or is executing it?

When I connect to power (3x AA in series), the sensor light turns on. The sensor's also reacts when something is close (2nd light comes on) but there is no sound at the speaker. At times, I have heard static from the speaker. The Teensy will blink red if I touch the manual program button, so it appears to have power.


wiring-1.jpg


Here is the code:

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

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     
AudioMixer4              mixer1;         
AudioOutputMQS           MQS1;         
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, 0, MQS1, 0);
AudioControlSGTL5000     sgtl5000_1;   
// GUItool: end automatically generated code

int IR = 29;

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


void setup() {
  pinMode(IR, INPUT);
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  delay(1000);
}

void loop() {

  if (digitalRead(IR) == LOW) {
      if (playSdWav1.isPlaying() == false) {
        playSdWav1.play("DOG.WAV");
        delay(10);
      }
  }
}
 
Last edited:
Although still not working, it is doing something different now. When I connect power, I'm not hearing as much static through the speaker and the Teensy red light blinks about 8 times. Sensor is still lit up and reacts to motion as before. Also, when I plug the usb into my laptop the Arduino IDE doesn't appear to recognize the board.

When I trip the sensor, I can hear the slightest noise from the speaker (if my ear is right next to the speaker).
 
A few notes:

1. When you post code, use the "</>" button to open a window into which you past your code:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1;
AudioMixer4 mixer1;
AudioOutputMQS MQS1;
AudioConnection patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection patchCord5(mixer1, 0, MQS1, 0);
AudioControlSGTL5000 sgtl5000_1;
// GUItool: end automatically generated code
int IR = 29;
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11  // not actually used
#define SDCARD_SCK_PIN 13   // not actually used

void setup() {
  pinMode(IR, INPUT);
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  delay(1000);
}
void loop() {
  if (digitalRead(IR) == LOW) {
    if (playSdWav1.isPlaying() == false) {
      playSdWav1.play("DOG.WAV");
      delay(10);
    }
  }
}

2. You don't seem to have either an audio adapter board, so I assume you are using the Medium-Quality Sound onboard hardware and just one channel. (PIn 12, MQSL)

3. Take out all references to SDCARD MOSI and SDCARD SCK. They are used only when connecting to an SD card through SPI. You are connecting through the built-in SDC card hardware.
4. You probably don't need SPI.H or Wire.h.
5. The SGTL5000 is part of the audio adapter, so you can't use it to control volume.

Perhaps someone else more familiar with audio, and the AudioOutputMQS driver can add some more suggestions.
 
A few notes:

1. When you post code, use the "</>" button to open a window into which you past your code:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1;
AudioMixer4 mixer1;
AudioOutputMQS MQS1;
AudioConnection patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection patchCord5(mixer1, 0, MQS1, 0);
AudioControlSGTL5000 sgtl5000_1;
// GUItool: end automatically generated code
int IR = 29;
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11  // not actually used
#define SDCARD_SCK_PIN 13   // not actually used

void setup() {
  pinMode(IR, INPUT);
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  delay(1000);
}
void loop() {
  if (digitalRead(IR) == LOW) {
    if (playSdWav1.isPlaying() == false) {
      playSdWav1.play("DOG.WAV");
      delay(10);
    }
  }
}

2. You don't seem to have either an audio adapter board, so I assume you are using the Medium-Quality Sound onboard hardware and just one channel. (PIn 12, MQSL)

3. Take out all references to SDCARD MOSI and SDCARD SCK. They are used only when connecting to an SD card through SPI. You are connecting through the built-in SDC card hardware.
4. You probably don't need SPI.H or Wire.h.
5. The SGTL5000 is part of the audio adapter, so you can't use it to control volume.

Perhaps someone else more familiar with audio, and the AudioOutputMQS driver can add some more suggestions.
Thanks very much for the response.

1. I edited the original post for the code.

2. Yes, I am using pin 12 for audio output (green wire)

3. I have tried commenting out these commands (see below)

4. I tried commenting out these also

5. I also commented out the volume control command

Unfortunately, I'm getting similar results. Here is the most recent version of the code that I tried:

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

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     
AudioMixer4              mixer1;         
AudioOutputMQS           MQS1;         
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, 0, MQS1, 0);
AudioControlSGTL5000     sgtl5000_1;   
// GUItool: end automatically generated code

int IR = 29;

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


void setup() {
  pinMode(IR, INPUT);
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
//  sgtl5000_1.volume(0.6);
//  SPI.setMOSI(SDCARD_MOSI_PIN);
//  SPI.setSCK(SDCARD_SCK_PIN);
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  delay(1000);
}

void loop() {

  if (digitalRead(IR) == LOW) {
      if (playSdWav1.isPlaying() == false) {
        playSdWav1.play("DOG.WAV");
        delay(10);
      }
  }
}
 
deadline is here... anyone know what is wrong and/or why this isn't working?
I checked the IMX-RT1062 reference manual. It tells me that the MQS outputs are PWM outputs suitable for directly driving a speaker or headphones. The next steps in troubleshooting would be:


1. If you don't have the T4.1 audio shield, get rid of the "sgtl5000_1.enable();", since that chip is on the audio shield.

2. Make sure that you have the correct setup to run playSdWav1 with data from the onboard SD Card. I don't recall enough about my simple tests with audio to recall how the correct SD Card chip select is chosen.

3. Eliminate problems with your amplifier by connecting the MQS output to a simple set of headphones or directly to a small speaker.


4. Try a test that doesn't involve the SD Card, such as this one:
MQS Direct

Look as the posts following that one for more help.


4. Look at the MQS output with an oscilloscope and verify that you are getting the PWM output.
 
The word "directly" might be a bit of an exaggeration from NXP's marketing team.

Don't directly wire an 8 ohm speaker to Teensy's pins!
 
4. Try a test that doesn't involve the SD Card, such as this one:
MQS Direct

Thanks so much for responding. I started here and I ran the code in that thread and had a good [loud] tone on the speaker. So it seems that I have eliminated the MQS output and speaker as potential problems.

So, I think my problem is either the code or the ability to access the SD card. Right?

The SD card I'm using is probably pretty large (edit: it is 64GB) - is there a size limit that a teensy can read/access?
 
Last edited:
Was DOG.WAV created using the right parameters? It has to be 16-bit PCM with a sampling rate of 44100Hz.

It probably wasn't before. I just spent some time working in VLC media player and I think these parameters are set now.

1707797718649.png
 
Last edited:
I think my audio file is sorted. I also reformatted the sd card and made sure it was formatted in the correct way. Still not working.

Next, I downloaded a sample audio wav file from PJRC here. Still not working.
 
I think my audio file is sorted. I also reformatted the sd card and made sure it was formatted in the correct way. Still not working.

Next, I downloaded a sample audio wav file from PJRC here. Still not working



You could also add a LED blink during the expected playback time.
Code:
#include <Audio.h>
//#include <Wire.h>
//#include <SPI.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     
AudioMixer4              mixer1;         
AudioOutputMQS           MQS1;         
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, 0, MQS1, 0);
AudioControlSGTL5000     sgtl5000_1;   
// GUItool: end automatically generated code
const int ledpin  = 13;
int IR = 29;

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


void setup() {
  pinMode(IR, INPUT);
 pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
//  sgtl5000_1.volume(0.6);
//  SPI.setMOSI(SDCARD_MOSI_PIN);
//  SPI.setSCK(SDCARD_SCK_PIN);
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  delay(1000);
}

void loop() {

  if (digitalRead(IR) == LOW) {
      if (playSdWav1.isPlaying() == false) {
        digitalWriteFast(pinled, HIGH);   // LED ON
        playSdWav1.play("DOG.WAV");
        delay(3000);
        digitalWriteFast(pinled, LOW);   //  LED OFF        
      }
  }
}

That should turn the LED on for three seconds when the sound is supposed to be playing.
 
I tried the LED as described above - connected to pin 13 of the teensy. It does NOT light up when the sensor is activated.

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

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     
AudioMixer4              mixer1;         
AudioOutputMQS           MQS1;         
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, 0, MQS1, 0);
AudioControlSGTL5000     sgtl5000_1;   
// GUItool: end automatically generated code
const int ledpin  = 13;
int IR = 29;

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


void setup() {
  pinMode(IR, INPUT);
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
//  sgtl5000_1.volume(0.6);
//  SPI.setMOSI(SDCARD_MOSI_PIN);
//  SPI.setSCK(SDCARD_SCK_PIN);
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  delay(1000);
}

void loop() {

  if (digitalRead(IR) == LOW) {
      if (playSdWav1.isPlaying() == false) {
        digitalWriteFast(ledpin, HIGH);   // LED ON
        playSdWav1.play("DOG.WAV");
        delay(3000);
        digitalWriteFast(ledpin, LOW);   //  LED OFF       
      }
  }
}
 
I also tried to connect the LED to pin 29 (input from IR sensor). The LED is on and then turns off when the sensor is activated.
 
Maybe try a much simper test without the audio stuff, like this:

Code:
void loop() {
  if (digitalRead(IR) == LOW) {
        digitalWriteFast(ledpin, HIGH);   // LED ON
        delay(3000);
        digitalWriteFast(ledpin, LOW);   //  LED OFF      
  }
}

Also focus on the WavFilePlayer example, of course with the filename edited to DOG.WAV. If that known-good code doesn't give audio output, you need to resolve the problem before making everything more complicated with unknown code. Get the known-good example working first!

The path to success for these sorts of technical problems is testing each piece first, so when you build up a more complicated program you're building with known-working pieces.
 
The LED does light up when the ground is connected to the pin 13 and the + lead of the LED is connected to power - this also causes an orange light to illuminate on the teensy. The status of the IR sensor does not effect the LED in this condition.
I tried the LED as described above - connected to pin 13 of the teensy. It does NOT light up when the sensor is activated.
 
Maybe try a much simper test without the audio stuff, like this:

Code:
void loop() {
  if (digitalRead(IR) == LOW) {
        digitalWriteFast(ledpin, HIGH);   // LED ON
        delay(3000);
        digitalWriteFast(ledpin, LOW);   //  LED OFF     
  }
}

Also focus on the WavFilePlayer example, of course with the filename edited to DOG.WAV. If that known-good code doesn't give audio output, you need to resolve the problem before making everything more complicated with unknown code. Get the known-good example working first!

The path to success for these sorts of technical problems is testing each piece first, so when you build up a more complicated program you're building with known-working pieces.

Same result - the LED does light up when the ground is connected to the pin 13 and the + lead of the LED is connected to power - this also causes an orange light to illuminate on the teensy. The status of the IR sensor does not effect the LED in this condition.
 
The status of the IR sensor does not effect the LED in this condition.

Maybe something isn't connected properly? Can you show another photo? The picture in msg #1 is from the top, which makes the connection to Teensy hard to see. Is the wire on pin #29 actually soldered to Teensy, or just sitting loose inside the hole?
 
Maybe something isn't connected properly? Can you show another photo? The picture in msg #1 is from the top, which makes the connection to Teensy hard to see. Is the wire on pin #29 actually soldered to Teensy, or just sitting loose inside the hole?

Everything is soldered, except for the LED that is not shown in this picture.

IMG_880221.jpg
 
A few suggestions, apart from Paul's excellent one of trying the known-good WavFilePlayer example...
  • you still have references to sgtl5000 in your code - remove those
  • you're not allocating much AudioMemory() - try 20 blocks or so rather than 8
  • you haven't initialised the SD card - add the code in green
  • just in case, add an extra AudioConnection to the other MQS output
Rich (BB code):
#include <Audio.h>
//#include <Wire.h>
//#include <SPI.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     
AudioMixer4              mixer1;         
AudioOutputMQS           MQS1;         
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, 0, MQS1, 0);
AudioConnection          patchCord6(mixer1, 0, MQS1, 1);
AudioControlSGTL5000     sgtl5000_1;   
// GUItool: end automatically generated code
const int ledpin  = 13;
int IR = 29;

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


void setup() {
  pinMode(IR, INPUT);
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
//  sgtl5000_1.volume(0.6);
//  SPI.setMOSI(SDCARD_MOSI_PIN);
//  SPI.setSCK(SDCARD_SCK_PIN);
 while (!(SD.begin(SDCARD_CS_PIN))) 
  {
    // stop here, but print a message repetitively
      Serial.println("Unable to access the SD card");
      delay(500);
  }
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  delay(1000);
}

void loop() {

  if (digitalRead(IR) == LOW) {
      if (playSdWav1.isPlaying() == false) {
        digitalWriteFast(ledpin, HIGH);   // LED ON
        playSdWav1.play("DOG.WAV");
        delay(3000);
        digitalWriteFast(ledpin, LOW);   //  LED OFF       
      }
  }
}
 
Thank you so much @h4yn0nnym0u5e

I tried the suggestions described above, except for the "while" statement (because I wasn't sure what it was doing and wanted to make incremental changes). These changes fixed the LED so that it would light up at the appropriate time. But still no sound.


Then I added this:
Code:
 while (!(SD.begin(SDCARD_CS_PIN)))
  {
    // stop here, but print a message repetitively
      Serial.println("Unable to access the SD card");
      delay(500);
  }

Now, it is fully working. I owe you a beer.

Did the code need the extra delay or the print command?
 
Final working version of code:

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

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     
AudioMixer4              mixer1;         
AudioOutputMQS           MQS1;         
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, 0, MQS1, 0);
AudioConnection          patchCord6(mixer1, 0, MQS1, 1);
//AudioControlSGTL5000     sgtl5000_1;   
// GUItool: end automatically generated code
const int ledpin  = 13;
int IR = 29;

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


void setup() {
  delay(2000);
  pinMode(IR, INPUT);
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
  AudioMemory(20);
//  sgtl5000_1.enable();
//  sgtl5000_1.volume(0.6);
//  SPI.setMOSI(SDCARD_MOSI_PIN);
//  SPI.setSCK(SDCARD_SCK_PIN);
while (!(SD.begin(SDCARD_CS_PIN)))
  {
    // stop here, but print a message repetitively
      Serial.println("Unable to access the SD card");
      delay(500);
  }
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  delay(1000);
}

void loop() {
  if (digitalRead(IR) == LOW) {
      if (playSdWav1.isPlaying() == false) {
        digitalWriteFast(ledpin, HIGH);   // LED ON
        playSdWav1.play("DOG.WAV");
        delay(3000);
        digitalWriteFast(ledpin, LOW);   //  LED OFF       
      }
  }
}
 
Thank you so much @h4yn0nnym0u5e

I tried the suggestions described above, except for the "while" statement (because I wasn't sure what it was doing and wanted to make incremental changes). These changes fixed the LED so that it would light up at the appropriate time. But still no sound.


Then I added this:
Code:
 while (!(SD.begin(SDCARD_CS_PIN)))
  {
    // stop here, but print a message repetitively
      Serial.println("Unable to access the SD card");
      delay(500);
  }

Now, it is fully working. I owe you a beer.

Did the code need the extra delay or the print command?
Incremental is always good...

The print and delay only kick in if SD card initialisation fails. I've sometimes found it doesn't work first time, and altered the canonical code you find in the WavFilePlayer example and elsewhere so it tries multiple times at 500ms intervals, rather than just repeatedly printing an error message without re-trying (which always seemed a bit pointless to me).
 
Back
Top