Teensy 4 + HiFiBerry DAC

Status
Not open for further replies.

J_Sanders

Active member
Hi all,

Like many Teensy 4 users, I've been eager to funnel all of that computing power into some slick HD audio.
This past year, HiFiBerry released two next-gen audio cards for Raspberry Pi - DAC2 Pro and DAC2 HD.
DAC2 Pro builds on their PCM-5122 powered DAC+ Pro board. It can play 192kHz, 24-bit audio with claimed quality metrics of 112dB SNR and -93db THD+N. It sports an onboard headphone amp, and costs $45 USD.
DAC2 HD is a totally new card using an older DAC, PCM-1796. The DAC is far less integrated, so the board is packed with supporting components - for an improved 123db SNR and -108db THD +N. MSRP: $99 USD.

Both cards have precision clock sources and ultra low noise regulators to filter the DAC power supply.

How great would it be if Teensy 4.X could talk to the world via these monsters?
I developed a simple breakout board to do just that:

T4.jpg

The board is available in two versions - one mounting above Teensy, and one below.

Design files and Arduino code are available on Github here, and the boards can be ordered from OSHPark (here and here). The only additional components are raspberry pi pin headers, with Adafruit part numbers given in the repository.

The Arduino example code is a simple sine wave sweep, using Paul's audio library.
By setting a macro, it can be configured to run in I2S master mode, where Teensy generates clock signals, or I2S slave mode using HiFiBerry's precision clocks.
The example code also configures the headphone amp on DAC2 Pro.
The example code runs on DAC+ Pro and DAC2 Pro, but not on DAC2 HD. The breakout board has the necessary connections for the HD, but I haven't been able to get the code working yet. The working examples have a few remaining quality issues, especially with Teensy in I2S slave mode - but I hope to iron these out in the next few weeks. Issues are noted in the repository, and I'll discuss them in depth in future posts.

Full disclosure - In parallel, I'm developing an audio board using these components for Bpod, a behavior measurement platform used in Neuroscience research. Using code heavily inspired by Paul's audio library, the board plays a library of 192kHz stereo waveforms from microSD on trigger with only 1 sample of latency (using preloaded psRAM buffers), and outputs an isolated TTL signal to precisely indicate playback start and end. This project will also be open source, once it meets the necessary specs to be useful in its application.

I'll be glad to share more technical info with anyone who's interested (HiFiBerry I2C sniff logs captured with PulseView, etc)!

-J
 
Good news - the board now works with HiFiBerry DAC2 HD!
I also worked out the remaining issues with I2S slave mode on DAC2 Pro.
The updated code is on Github, here.

The signal quality looks great if both channels have the same sine waveform generated by the audio library.
Unfortunately I'm still unable to get separate waveforms to play cleanly on the left and right channels.
For instance, this works:
Code:
AudioSynthWaveformSine   sine1;
AudioSynthWaveformSine   sine2;
AudioConnection          patchCord1(sine1, 0, i2s1, 0);
AudioConnection          patchCord2(sine1, 0, i2s1, 1);
But this produces a chopped overtone (at the speed of audio library packets?):
Code:
AudioSynthWaveformSine   sine1;
AudioSynthWaveformSine   sine2;
AudioConnection          patchCord1(sine1, 0, i2s1, 0);
AudioConnection          patchCord2(sine2, 0, i2s1, 1);

Using custom code derived from the audio library (i.e. synthesizing the sine directly in the I2S DMA channel's ISR) I can get clean signals out of both channels simultaneously, so it's not a limitation of the hardware. I'll post some example code later on when I have time to mark it up nicely - with sampling rates up to 192kHz.

-J
 
But this produces a chopped overtone (at the speed of audio library packets?):

Code:
AudioSynthWaveformSine   sine1;
AudioSynthWaveformSine   sine2;
AudioConnection          patchCord1(sine1, 0, i2s1, 0);
AudioConnection          patchCord2(sine2, 0, i2s1, 1);

Would you like me to investigate? I need a complete program I can copy into Arduino, even if the rest seems "trivial".
 
Hi Paul!

The overtone was fixed by increasing AudioMemory to 50.
With AudioMemory as high as 250, I'm still getting crackling on the right audio channel on the HiFiBerry boards.
The original sketch was the example code, here.
This is the same sketch with the HiFiBerry code stripped out, and set up for the SGTL5000 with Teensy in I2S master mode:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <utility/imxrt_hw.h>

#define SINE_FREQ_MAX 20000
#define SINE_FREQ_MIN 20

uint32_t sineFrequency = 1000;

AudioOutputI2S i2s1; 

AudioSynthWaveformSine   sine1;
AudioSynthWaveformSine   sine2;
AudioConnection          patchCord1(sine1, 0, i2s1, 0);
AudioConnection          patchCord2(sine2, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;  

void setup() {
  AudioMemory(50);
  sine1.amplitude(0.5);
  sine2.amplitude(0.5);   
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
}

void loop() {
  sineFrequency += 1;
  if (sineFrequency > SINE_FREQ_MAX) {
    sineFrequency = SINE_FREQ_MIN;
  }
  sine1.frequency(sineFrequency);
  sine2.frequency(sineFrequency);
  delayMicroseconds(500);
}

I'm not sure how to configure SGTL5000 as master for use with AudioOutputI2Sslave (looks like a new call to enable() with a clock argument? clock = fs didn't work for me)

Thanks!

-J
 
Is that now about the SGLT or HiFiBerry?

If you have crackling noise other effects with the Audio Board, there is something wrong with your hardware...

Increasing the Audiomemory does not help... the additional memory is not used.
If you have the newest Audio Library from GitHub, you can use the SGTL master mode this way (on T4):

Edit: Code removed, there were some parts missing. Best is to look here:

An example where this is used, is here: https://github.com/FrankBoesing/Tee...avFilePlayer_pitch/T4_WavFilePlayer_pitch.ino
 
Hi Frank B,

You were correct - it was a hardware issue!

The last code I posted was a stab at adapting the problematic code from the HiFiBerry to the SGLT, albeit in Teensy-as-I2S-master mode.
Using your code, I was able to run the code in Teensy-as-slave mode with no issue.
I also discovered that the crackling issue only exists for the top-mount version of my HiFiBerry breakout board - the bottom mount version plays with no issue.
I'm not sure yet whether it's the board design or the particular Teensy that's soldered into it. For now, I added a warning to Github + OSHPark.

One thing I didn't pay much attention to in designing this breakout board, is trace impedance matching on the I2S bus lines. In any case, it will take some debugging.

On a separate topic, here's a spectrogram of a 20Hz-80kHz frequency sweep from Teensy 4.1 --> HiFiBerry DAC2 HD running at 192kHz and synthesizing the wave directly in the I2S ISR (code here):
FreqSweep_HD.jpg

The data was acquired directly from the DAC output with a 16-bit NI USB-6211 DAQ board - so of course I'll need to capture it with a high-end 24-bit audio interface at some point to see the nuances. The noise at ~65kHz exists even with the DAC board unplugged, so it can be disregarded. The ultrasonic range looks pretty clean, so with a bit of tweaking this may be one way for Teensy to talk back to the bats.

Thank you for helping with this!
 
Last edited:
So, you're using the DAC for ultrasonic sounds?
Have you tried the PT8211? It's capable of 384khz (according to the datasheet) and cheap.
The bats will not notice that, I think..... (otherwise...RUN! :)
 
I'm not sure that a 100-ohm resistor could account for the difference - the two (working and buggy) boards have identical wiring, with the only difference being trace length. I used thick traces, so I expect the difference to be very minor - but I'll definitely have to measure it. It's definitely not an issue with the master clock line though - Raspberry Pi doesn't have an MCK pin on its GPIO header. The DAC has to generate MCK from another clock line with clock dividers.

Personally I'm not after bats (though it sounds like a lot of fun from the various threads on here!) I'm prototyping a low latency sound server for Neuroscience experiments. The device has to produce sound with high SNR and low distortion across the rat and mouse hearing ranges (~500Hz-80kHz). So far it looks like T4.1 + HiFiBerry DAC2 HD fits the bill, if I could only solve that pesky sdFAT/SDIO issue.

That being said, I'll have to check out PT8211. How's its sound quality vs. SGTL? It seems to be markedly absent from major distributors...
 
I think $45 and specially $99 is alot overpriced for a IC that only costs $5 each.
Even with all the components and PCB it would never reach the prices. No its because its made specially for raspberry pi I think.
 
My ten cents: you're paying for the peripherals and proprietary board layout, not the DAC IC. There are plenty of breakout boards for audio DACs, but they don't do high end power supply filtering, analog output filtering, precision clocking, etc. Implementing that stuff properly is art. In my hands, performance of HiFiBerry DAC2 HD looks qualitatively similar to similarly priced sound cards for PC. The $45 DAC2 Pro is much less impressive (I'm actually in a dialogue with TI to sanity check my measurements), but the clock ICs and headphone amp provide a convenient package for $15 more than their DAC+ Standard (incidentally also compatible with the Teensy HiFiBerry breakout board).
 
Awesome, thanks Frank!

A small update - I discovered that the crackling issue on the top-mount version of the board only affects DAC2 HD - not DAC2 Pro or DAC+.
I've updated OSHpark and Github notes to reflect this.

The solution turned out to be power supply decoupling - a 10uf ceramic capacitor between 5V and GND where power leaves the breakout board to the HiFiBerry board.
Not sure why the bottom-mount version of the board didn't need this, or whether 10uf is the optimal value here. Anyhow it works - and updated boards are coming soon!

Updated guidance: until a revised PCB is released, if you're using DAC2 HD, use the bottom-mount version (or solder in a 1206 cap as shown below).

From the experienced EE people in the crowd - any opinion on 10uf vs some other value, and ceramic vs. other types for decoupling a whole board (which presumably has its own appropriate decoupling at each IC)?

CapSolved.jpg
 
Last edited:
I just posted v1.1 of this board in the repository, solving the previous stability issue with DAC2 HD.
The fix was to add a 4-component USB power supply filter borrowed from an FTDI circuit note, figure 2.5 here.
As of now, there are no known issues with the interface - and the audio quality is +++
 
Status
Not open for further replies.
Back
Top