ledMode seems to break script??

Hello all,

I’m trying to make an LED come on when a certain frequency comes into the onboard microphone. The following piece of code is a mish-mash but works to at least tell me what frequency the input sound is, but whenever I uncomment the ledMode (x,OUTPUT) the recording stops. Am I doing something screwy or does Pin 13 getting set to OUTPUT somehow screw up the other outputs?

I’m using Teensy 3.2 with soundshield

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

int LedPin = 13; // LED Pin- for Teensy 3.2 with soundshield = Pin 13
int ListenFrequency = 3000; // Threshold frequency


// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=352,135
AudioAnalyzeNoteFrequency notefreq1;      //xy=600,228
AudioOutputI2S           i2s2;           //xy=626,144
AudioConnection          patchCord1(i2s1, 0, notefreq1, 0);
AudioConnection          patchCord2(i2s1, 0, i2s2, 0);
AudioConnection          patchCord3(i2s1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=525,305
// GUItool: end automatically generated code

//---------------------------------------------------------------------------------------
void setup() {
  // pinMode (LedPin, OUTPUT);
  digitalWrite (LedPin, LOW);
   
  AudioMemory(30);
  Serial.begin(9600);
   
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(50);
  delay(1000);
    /*
     *  Initialize the yin algorithm's absolute
     *  threshold, this is good number.
     */
    notefreq1.begin(.15);

    if (notefreq1.read() >= ListenFrequency){
    digitalWrite (LedPin, HIGH);  
    }
    while (notefreq1.read()< ListenFrequency) {
      digitalWrite (LedPin, LOW);
    }
}

void loop() {
    // read back fundamental frequency
    if (notefreq1.available()) {
       float note = notefreq1.read();
       float prob = notefreq1.probability();
       int cpuusage = AudioProcessorUsageMax();
        Serial.printf("CPUusage: %i | Note: %3.2f | Probability: %.2f\n", cpuusage, note, prob);
    }

}

You've #included the SPI library. That would indicate that one of the components you're using is using SPI. Pin 13 is part of the SPI bus. It's the clock or CLK pin. So you can't use the LED on that pin.

Thanks MorganS,
I tried commenting out the SPI.h (as suggested) and SD.h (as I'm not using it either) and the sketch still stops output to the monitor as soon as pinMode (13, OUTPUT) is uncommented. Actually, all OUTPUT stops, I have a headset that the mic transmits through normally, but that stops as well.

Essentially, set pinMode to OUTPUT and all OUTPUT stops

Is this the audio board you are using? That page says it uses pin 13 as the line-in input for the R channel. Perhaps it actually checks that that pin is an input?

Thanks MorganS,
Yep, that is the board and it does appear that pin 13 is set line in. I didn't even look at the schematic because I followed through the tutorials that used BOTH LED's and sound, but not together. So, next step, I need to add an LED to the breadboard that is addressable by a different pin? Any suggestions?

Any other pin can drive the LED through a suitable resistor. If you can, choose a pin with PWM capability, so you can use analogWrite() to dim the LED.

Thanks MorganS,
When if all else fails, follow the directions or read the schem. LED to pin 20 (doesn't appear to be used for anything so far)

vagrant1963:
Thanks MorganS,
When if all else fails, follow the directions or read the schem. LED to pin 20 (doesn't appear to be used for anything so far)

Be sure and include a 200 ohm or so resistor in series with the LED.

Thanks ChrisTenone,

Already digging out a 220ohm along with the LED :slight_smile:

On LEDs I want to be very bright, I use a 75 ohm, medium bright 220 or 330, and 1K if it's just an indicator. LEDs are often the most power hungry for projects with no moving parts.