Teensy 3.6 - Using both Audio ADC and analogRead - works for me!

Revalogics

Well-known member
Hi guys, I discovered a way to use both ADC (mono audio in) and analogRead without interfering with each other.
I use the original libraries supplied by Teensyduino, without modifying any files in it.

Code:
/****************************************************************
 * Teensy 3.6                                                   *
 * Serial + MIDI + Audio, 180 MHz                               *
 * Coded by Revalogics                                          *
 * Arduino IDE 1.6.12 / Teensyduino 1.33                        *
 * Running in Windows 8.1                                       *
 ****************************************************************
 * This code streams mono audio from an analog input to USB     *
 * while blinking the built-in LED while sending a value of     *
 * another analog pin to the Serial Monitor.                    *
 *                                                              *
 * This only works using Mono ADC. Stereo ADC uses both ADC0 &  *
 * ADC1 and analogRead makes the code freeze(LED doesn't blink).*
 * Setting the right channel pin in Stereo ADC does nothing     *
 * (A3 is still the right channel input).                       *
 *                                                              *
 * My assumption is maybe it is possible to use ADC0 for audio  *
 * and ADC1 for analogRead since there are two hardware ADCs in *
 * Teensy 3.x.                                                  *
 *                                                              *
 * Audio + analogRead Possibilities, Here's what I found:       *
 * Audio Input + analogRead = status                            *
 * ADC0          ADC0         code freezes, no audio, no blink  *
 * ADC1          ADC0         code freezes, no audio, no blink  *
 * ADC0 + ADC1   ADC0         code freezes, no audio, no blink  *
 * ADC0          ADC1         audio on, blinking, serial works  *
 * ADC1          ADC1         no audio, blinking, serial works  *
 * ADC0 + ADC1   ADC1         audio on, blinking, serial works  * A2, A3 & A10 only, A11 = no audio, blinking, serial works
 *                                                              *
 * DAC0/A21 also works as an audio in.                          *
 *                                                              *
 * analogRead returns 16-bit values (65535 max) because the     *
 * Audio Library sets the analog read resolution to 16.         *
 *                                                              *
 * This is useful to me because I want to sample and modulate   *
 * my voice while controlling the parameters of a synth/vocoder *
 * using a potentiometer (analog pin).                          *
 ****************************************************************
 * Source: [PDF] Kinetis K66 Sub-Family Datasheet,              *
 * in K66 Signal Multiplexing and Pin Assignments Section       *
 *                                                              *
 * ADC0 analog pins             * ADC1 analog pins              *
 *   A0 (ADC0_SE5b)             *   A12 (ADC1_SE14)             *
 *   A1 (ADC0_SE14)             *   A13 (ADC1_SE15)             *
 *   A4 (ADC0_SE13)             *   A16 (ADC1_SE4b)             *
 *   A5 (ADC0_SE12)             *   A17 (ADC1_SE5b)             *
 *   A6 (ADC0_SE6b)             *   A18 (ADC1_SE6b)             *
 *   A7 (ADC0_SE7b)             *   A19 (ADC1_SE7b)             *
 *   A8 (ADC0_SE15)             *   A20 (ADC1_SE17)             *
 *   A9 (ADC0_SE4b)             *   A22 (ADC1_SE23/DAC1_OUT)    *
 *   A14 (ADC0_SE17)            *   A23 (ADC1_SE10)             *
 *   A15 (ADC0_SE18)            *   A24 (ADC1_SE11)             *
 *   A21 (ADC0_SE23/DAC0_OUT)   *                               *
 *                                                              *
 * ADC0 + ADC1 analog pins                                      *
 *   A2 (ADC0_SE8/ADC1_SE8) Default Left/Mono input             *
 *   A3 (ADC0_SE9/ADC1_SE9) Default Right input                 *
 *   A10 (ADC1_DP0/ADC0_DP3)                                    *
 *   A11 (ADC1_DM0/ADC0_DM3)                                    *
 ****************************************************************/
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioInputAnalog adc1(A0); // must use ADC0 or ADC0 + ADC1 analog pin here
AudioOutputUSB usb1;
AudioConnection patchCord1(adc1, 0, usb1, 0);
AudioConnection patchCord2(adc1, 0, usb1, 1);

elapsedMillis LED0timer;

void setup() {
  Serial.begin(115200);
  AudioMemory(16);
  pinMode(13, 1); // 0 = input, 1 = output, 2 = input_pullup
  digitalWrite(13, 1); //0 = low, 1 = high
}

void loop() {
  if(LED0timer >= 0 && LED0timer <= 500) digitalWrite(13, 1);
  if(LED0timer > 500 && LED0timer <= 1000) digitalWrite(13, 0);
  if(LED0timer > 1000) {
    Serial.println(analogRead(A12)); // must use ADC1 analog pin here
    LED0timer = 0;
  }
}

Does this also work in you?
 
from https://www.pjrc.com/store/teensy41.html
1 analog input pins may be used for audio inputs. Using an ADC pin for audio input currently has only "experimental" software support.
Teensy 3.6 supports two (2) analog inputs for audio, while Teensy 4.1 supports only one (1), and is currently experimental based on PJRC documentations.

Code:
Teensy 4.1 Analog Channel assignments
(unchecked, based on PJRC T4.1 schematic)

ADC0  analog pins
(A10) AD_B0_12
(A11) AD_B0_13
(0)   AD_B0_03
(1)   AD_B0_02
other ADC0 pins connected to MKL02 chip onboard

ADC1  analog pins
(A0)  AD_B1_02
(A1)  AD_B1_03
(A2)  AD_B1_07
(A3)  AD_B1_06
(A4)  AD_B1_01
(A5)  AD_B1_00
(A6)  AD_B1_10
(A7)  AD_B1_11
(A8)  AD_B1_08
(A9)  AD_B1_09
(A12) AD_B1_14
(A13) AD_B1_15
(A14) AD_B1_12
(A15) AD_B1_13
(A16) AD_B1_04
(A17) AD_B1_05

I haven't got a T4.1 yet to test this, you can try changing analog input pin via:
Code:
AudioInputAnalog adc1(pin);
trying ADC0 pins, and then use ADC1 pins for analogRead(); with the old code posted to find out if ADC + Audio Input on T4.1 is possible.
Alternatively, you can try ADC1 pins for Audio ADC input, and ADC0 pins for AnalogRead(); to see if that works.
 
Back
Top