Audio library and analogRead() not working together on Teensy 4.0

Status
Not open for further replies.

guzu

Well-known member
As the title says, I noticed that my Teensy 4.0 gets stuck when I upload a code which has both the audio library code (see code bellow) and analogRead() lines in it.
In this situation, the Teensy is stuck and does not show as connected to the computer. It is not recognized by the computer.
I can still reset it with the 15 sec button press and I can upload code if I press the button after the Arduino IDE compiles the code.

Here are two examples of code:

This works:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>

// GUItool: begin automatically generated code
//AudioInputI2S            i2s1;           //xy=126,88
//AudioOutputI2S           i2s2;           //xy=448,94
//AudioConnection          patchCord1(i2s1, 0, i2s2, 0);
//AudioConnection          patchCord2(i2s1, 0, i2s2, 1);
//AudioControlSGTL5000     sgtl5000_1;     //xy=259,148
// GUItool: end automatically generated code


void setup() {
  Serial.begin(9600);
  AudioMemory(8);
  //sgtl5000_1.enable();
  //sgtl5000_1.volume(0.5);
  //sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  //sgtl5000_1.micGain(36);
  delay(1000);
}

void loop() {
  Serial.println("TEST");
  Serial.println(analogRead(A10));
  delay(1000);
}
notice all the commented out lines

Also this works:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=126,88
AudioOutputI2S           i2s2;           //xy=448,94
AudioConnection          patchCord1(i2s1, 0, i2s2, 0);
AudioConnection          patchCord2(i2s1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=259,148
// GUItool: end automatically generated code


void setup() {
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(36);
  delay(1000);
}

void loop() {
  Serial.println("TEST");
  //Serial.println(analogRead(A10));
  delay(1000);
}

This does not work:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s1;           //xy=126,88
AudioOutputI2S           i2s2;           //xy=448,94
AudioConnection          patchCord1(i2s1, 0, i2s2, 0);
AudioConnection          patchCord2(i2s1, 0, i2s2, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=259,148
// GUItool: end automatically generated code


void setup() {
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(36);
  delay(1000);
}

void loop() {
  Serial.println("TEST");
  Serial.println(analogRead(A10));
  delay(1000);
}
Notice I did not comment out the analogRead line.

Could this be a bug? I tried installing the Teensydoino 1.54 beta 9 but still the same result. I am using the latest Arduino IDE and Teensyduino.
What can I do to resolve this?

Thank you.
 
There is a classic trip-up where audio reads and analog reads happening simultaneously require careful pin choices to end up on different ADCs. This is addressed in the link above, and also:
https://forum.pjrc.com/threads/46710-combining-Teensy-Audio-and-Pedvide-s-ADC-libraries
with specific 4.0 info here: https://forum.pjrc.com/threads/58387-Teensy-4-0-which-pins-for-which-ADC
this is also generally helpful:
https://forum.pjrc.com/threads/25532-ADC-library-update-now-with-support-for-Teensy-3-1

However I don't think this is your case, as you are using the sgtl5000 for audio input. Your case is almost exactly one of the included example programs: audio tutorial Part 1-5 : Do More While Playing, which reads an analog pin to set mic gain. Does that compile and run OK for you (uncomment the last two lines, change analog to A10 or move your pot)?
 
However I don't think this is your case, as you are using the sgtl5000 for audio input. Your case is almost exactly one of the included example programs: audio tutorial Part 1-5 : Do More While Playing, which reads an analog pin to set mic gain. Does that compile and run OK for you (uncomment the last two lines, change analog to A10 or move your pot)?

I am indeed using the audio board (SGTL5000). The program is the one in Examples > Audio > Tutorial > Microphone check, with the added serial print of the analogRead. It compiles and I can upload it. But after upload the Teensy 4.0 stops responding. I uploaded the exact same code on a Teensy 4.1. There the program and Teensy run ok after uploading.

//edit
I also tried installing the Arduino IDE 1.8.12. Still the same result.
 
Last edited:
The issue is now resolved. I did a fresh install of the Arduino IDE and Teensydoino and now all works ok.
Thank you all for your input.
 
Status
Not open for further replies.
Back
Top