Making Music with the Discrete DAC

24. April 2016 Projects 2
Making Music with the Discrete DAC

Maybe music is overstating this a bit, but it’s creating sound (and makes for a second alliteration in the title). Measuring a constant voltage is one thing, but can the discrete DAC put out a waveform? After all, that’s the main idea behind using a digital-to-analog converter (DAC) rather than pulse-width-modulation (PWM).

So I added a simple output stage consisting of an op-amp and a speaker. Here’s what that looks like.discrete-dac-speaker

The LM358 is a dual op-amp, though I’m only using one of them in this. It’s wired as a simple voltage follower and does that reasonably well (it cuts off at 4V, taking off the top of the waveform). Its role is to decouple the speaker from the shift register outputs and not overload them.

The Arduino shifts bits into the shift register at a rate of about 35,000 per second. There are a few waits in the program that probably aren’t necessary and would make this much faster. I’m waiting for one microsecond after toggling the clock lines up and down, and again when toggling the line that shifts the bits to the outputs. It looks like Arduino’s digitalWrite() function is really slow, so writing directly to the ports could probably speed this up by a factor of 10 or so.

Either way, this gives me roughly 7,000 samples per second. I created the values for a 20-sample sine wave and had the program push those out. That results in a decent sine wave on the output, and a roughly 330Hz tone in the speaker.

five-bit-dac-sine-unfiltered

These are highly-processed pictures from my crappy little JYE Tech scope. I tried downloading the images directly from it, but was not successful. Either way, it shows that the DAC works nicely, but the time between samples is long enough to have visible steps. So I inserted a 100nF capacitor between the output and ground.

five-bit-dac-sine-100nF

Smoother, but also reduced the amplitude by about half. Let’s see if 10nF is enough to smooth but not cost so much in swing.

five-bit-dac-sine-10nF

Bingo! We’re losing less than 10% here, and the wave looks much better. Sone of the stepping here is due to the low resolution of the scope (eight bits), so it’s hard to say how smooth this really is. But it clearly looks better than without the cap.

Just as a test, I also connected the speaker to the PWM output that I wrote the same values to. It was roughly recognizable, but mostly I could hear the harmonics from the PWM signal. For a fair comparison, I would have to filter the PWM signal. PWM works well for some uses (like dimming LEDs), but not for others.


2 thoughts on “Making Music with the Discrete DAC”

  • 1
    D.C. on April 27, 2016 Reply

    Please add an RSS feed to your blog so people can subscribe without email. Thanks!

    • 2
      Robert Kosara on April 27, 2016 Reply

      There is a feed! There’s no link on the page, but any RSS reader should find it if you give it the site’s URL.

      Here’s the direct link, too: https://shift-register.org/feed

Leave a Reply to Robert Kosara Cancel reply