Testing the hardware, integration and workflow to build a Teensy Synth
Create Folder AudioTest/
Create AudioTest.ino:
// =============================================================================
// AudioTest.ino — Teensy 4.0 + Audio Shield hardware test
// =============================================================================
// Outputs a 440Hz sine wave to both audio outputs.
// No extra libraries needed — just the built-in Teensy Audio library.
//
// Compile:
// arduino-cli compile --fqbn teensy:avr:teensy40 \
// --build-property "build.usbtype=USB_MIDI_SERIAL" .
//
// Upload:
// arduino-cli upload --fqbn teensy:avr:teensy40 --port usb:0/140000/0/1 .
//
// Monitor:
// arduino-cli monitor --port /dev/cu.usbmodemXXXX --config baudrate=115200
// =============================================================================
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
// ── Audio objects ─────────────────────────────────────────────────────────────
AudioSynthWaveform sine;
AudioOutputI2S audioOut;
AudioControlSGTL5000 audioShield;
// ── Patch cords ───────────────────────────────────────────────────────────────
AudioConnection cord1(sine, 0, audioOut, 0); // left
AudioConnection cord2(sine, 0, audioOut, 1); // right
// ── Config — tweak these to test ─────────────────────────────────────────────
const float FREQUENCY = 440.0f; // Hz
const float AMPLITUDE = 0.4f; // 0.0–1.0 (keep below 0.8 to avoid clipping)
const float VOLUME = 0.6f; // headphone / line-out level
// =============================================================================
void setup() {
Serial.begin(115200);
delay(200);
AudioMemory(12);
audioShield.enable();
audioShield.volume(VOLUME);
sine.begin(AMPLITUDE, FREQUENCY, WAVEFORM_SINE);
Serial.println("=== AudioTest ===");
Serial.printf("Frequency : %.1f Hz\n", FREQUENCY);
Serial.printf("Amplitude : %.2f\n", AMPLITUDE);
Serial.printf("Volume : %.2f\n", VOLUME);
Serial.println("You should hear a 440Hz tone.");
}
// =============================================================================
void loop() {
// Report CPU + memory usage every 2 seconds
static elapsedMillis t;
if (t >= 2000) {
t = 0;
Serial.printf("CPU: %.1f%% Mem: %d blocks\n",
AudioProcessorUsageMax(),
AudioMemoryUsageMax());
AudioProcessorUsageMaxReset();
AudioMemoryUsageMaxReset();
}
}
you should hear a steady 440Hz tone and see this in the monitor every 2 seconds:
=== AudioTest ===
Frequency : 440.0 Hz
Amplitude : 0.40
Volume : 0.60
You should hear a 440Hz tone.
CPU: 2.3% Mem: 2 blocks