Surprising DAC behavior

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
mathieu
Posts: 88
Joined: Fri Nov 10, 2017 9:57 pm

Surprising DAC behavior

Post by mathieu » Sat Jan 20, 2018 12:48 am

Hi all,

I've noticed something unexpected (to me) when using a DAC to drive a speaker through a class D audio amp breakout. Here is the code:

Code: Select all

import wave
from pyb import DAC, delay

dac = DAC(1)
f = wave.open('test.wav', 'r')
fr = f.getframerate()
total_frames = f.getnframes()
buf = f.readframes(total_frames)

dac.write_timed(buf, fr, mode=DAC.NORMAL)
delay(1000)

dac.deinit()
dac.write_timed(buf, fr, mode=DAC.NORMAL)
When running the above code, the first playback is much louder and much noisier than the second one, and subsequent uses of dac.write_timed() behave like the latter.

Is this the expected behavior? Can somebody offer an explanation of what might be happening? Thanks for educating me.

- Mathieu

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Surprising DAC behavior

Post by pythoncoder » Sat Jan 20, 2018 6:58 am

I don't think it's normal practice to deinit the DAC without re-declaring it.

The Pyboard DAC hardware has a hardware analog buffer on its output which reduces the output impedance at cost of reduced accuracy. The buffer is enabled for write_timed and disabled for individual writes (if I remember correctly). My guess is that issuing deinit is confusing the firmware and causing subsequent calls to write_timed to run with the buffer disabled. Depending on the input impedance of the amplifier this will attenuate the signal.
Peter Hinch
Index to my micropython libraries.

User avatar
mathieu
Posts: 88
Joined: Fri Nov 10, 2017 9:57 pm

Re: Surprising DAC behavior

Post by mathieu » Sat Jan 20, 2018 11:00 pm

Thanks, that makes sense.

Is there a high-level API (i.e. a micropython function) to disable or enable this hardware analog buffer? I tend to prefer the sound output without it. Or is it just a matter of adding a resistor in series out of the DAC pin, which would (I think) increase the apparent output impedence (but leave the reduced accuracy as is)?

- Mathieu

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Surprising DAC behavior

Post by pythoncoder » Sun Jan 21, 2018 7:12 am

Alas there is no library function to turn the buffer on or off.

I think your idea of a series resistor is the simplest and achieves the same thing as disabling the buffer. The output impedance of the chip with buffer disabled is 15KΩ maximum, so a resistor of somewhere up to that value should do the trick. But all you're doing is reducing the volume.
Peter Hinch
Index to my micropython libraries.

Post Reply