-
Notifications
You must be signed in to change notification settings - Fork 5
Audio Example DAC on Teensy 3.2, 3.5, or 3.6
MichaelMeissner edited this page Apr 1, 2020
·
5 revisions
The Teensy 3.1 and 3.2 has a single DAC (digital to analog converter) on pin A14 that can be used to play sounds. The Teensy 3.5 and 3.6 have two DAC converters on pins A22 and A21. This example uses the first DAC on A21.
The DAC pin needs to be amplified. This code assumes that the Teensy 3.2 has the normal or LC prop-shields mounted, which provide amplification for the DAC pin. The code does not assume the use of either prop shield for Teensy 3.5 or 3.6.
While the Teensy LC does have a DAC on pin A12, a lot of the Audio library support will not run on the Teensy LC.
Neither the Teensy 3.0 or nor the Teensy 4.0 have a DAC.
/*
Demo of the audio sweep function.
The user specifies the amplitude,
start and end frequencies (which can sweep up or down)
and the length of time of the sweep.
Modified to eliminate the single DAC on the Teensy 3.2 and the first DAC on
the Teensy 3.5 or 3.6. The Teensy 4.0 does not have a DAC.
Pins: Teensy LC Teensy 3.2 Teensy 3.5/3.6
DAC: A12 A14 A21
*/
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// Define USE_PROPSHIELD if the Teensy uses the prop shield, and we want to
// enable the onboard amplifier. Assume the LC/3.2 will have the propshield,
// but the 3.5/3.6 won't.
#if defined(__MKL26Z64__) || defined(__MK20DX256__)
#define USE_PROPSHIELD 1
#endif
// GUItool: begin automatically generated code (edited by meissner afterwards).
AudioSynthToneSweep tonesweep; //xy=102,174
AudioAmplifier amp; //xy=324,172
AudioOutputAnalog dac; //xy=452,189
AudioConnection patchCord1 (tonesweep, amp);
AudioConnection patchCord2 (amp, 0, dac, 0);
// GUItool: end automatically generated code
const float t_ampx = 0.8;
const int t_lox = 10;
const int t_hix = 22000;
const float t_timex = 10; // Length of time for the sweep in seconds
void setup(void)
{
// Wait for at least 3 seconds for the USB serial connection
Serial.begin (9600);
while (!Serial && millis () < 3000)
;
AudioMemory (8);
amp.gain (0.5f);
// Enable the prop shield speaker
#if USE_PROPSHIELD
pinMode (5, OUTPUT);
digitalWrite (5, HIGH);
#endif
Serial.println("setup done");
if (!tonesweep.play (t_ampx, t_lox, t_hix, t_timex)) {
Serial.println ("ToneSweep - play failed");
while (1)
;
}
// wait for the sweep to end
Serial.println ("Tonesweep up started");
while (tonesweep.isPlaying ())
;
// and now reverse the sweep
Serial.println ("Tonesweep down started");
if (!tonesweep.play (t_ampx, t_hix, t_lox, t_timex)) {
Serial.println("ToneSweep - play failed");
while (1)
;
}
// wait for the sweep to end
while (tonesweep.isPlaying ())
;
Serial.println("Done");
}
void loop(void)
{
}
Teensy is a PJRC trademark. Notes here are for reference and will typically refer to the ARM variants unless noted.