Switch Inputs

Status
Not open for further replies.
Hello,

I want to switch inputs to the teensy audio board from mic in to line in using slide switch.
What are the libraries that I need to add to do so?



Thank you
 
No libraries needed. You might send the commands to setup the SGTL5000 (see its datasheet/reference manual) inputs directly over i2c after having designated a free GPIO pin on the Teensy to sense the slide switch position and having added a corresponding event handler in your code.
 
Hi,

Can I use commands to setup multiple inputs for the sgtl5000?
Eg : int a= AUDIO_INPUT_MIC;
int b= AUDIO_INPUT_LINEIN;
void setup()
{
sgtl5000_1.inputSelect(a);
sgtl5000_1.inputSelect(b);
}

Using this can I switch inputs between line in and mic in based on the position of the switch?
 
Normally you should read switches with the Bounce library. You can also use digitalRead(), but Bounce is better because it will automatically deal with mechanical chatter in the switch.

Most of the audio library tutorial examples use Bounce to read 3 pushbuttons, so I'd suggest checking out the tutorial.

https://www.pjrc.com/store/audio_tutorial_kit.html

You should connect 1 wire from the switch to an unused digital pin and the other wire to GND. Near the beginning of your program, create a Bounce object (as in the tutorial examples), like this if using pin 2.

Code:
Bounce button2 = Bounce(2, 15);

Then in setup(), configure the pin for INPUT_PULLUP mode, like this:

Code:
  pinMode(2, INPUT_PULLUP);

Near the end of setup you will also need some code to read the switch and set up the initial state of the input select. It should look like this:

Code:
  delay(10);
  button2.update();
  if (button2.read()) {
    sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  } else {
    sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
  }

The delay may or may not be necessary, but I recommend using a short delay in case there is very little time between configuring the pin and this code. Those input pullups are weak and can take a short time to actually change the pin's voltage.

Then inside loop, you'll need code like this.

Code:
  button2.update();
  if (button2.risingEdge()) {
    sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  }
  if (button2.fallingEdge()) {
    sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
  }

Every time loop() runs, this will check if the switch has changed. If it does, the input mode is changed. Simple, right?
 
Hi

I tried using digitalRead() and I was able to switch the inputs, ut there is a lot of noise. I will try using bounce library.
 
Don't forget to use INPUT_PULLUP mode on the pin. Regular INPUT is highly sensitive.

If you use long wires or have a very noisy environment, you might need to add a real 1K pullup resistor between the pin and 3.3V. But normally the weak internal pullups are enough.
 
Status
Not open for further replies.
Back
Top