One of the first concerns for producing musical sounds from a computer is converting the stream of numbers computed by the algorithm into a stream of voltages uniformly spaced in time. The uniform timing is the responsibility of the computer (in my case, an Arduino board), and I’ll cover that in a later post, but the conversion to voltages is best done with a digital-to-analog converter (DAC). (One can also try using the pulse-width modulation of the Arduino’s digital pins—I’ll cover that in a later post also.)
When I first built hardware for digital sound, DACs were expensive parts. My first board used a DAC0801LCN, an 8-bit DAC that is (amazingly) still available. It came in a 16-pin DIP and needed a fairly large power-supply voltage (±4.5v to ±18v). It also uses up a lot of output pins on the Arduino (each of the 8-bits is a separate pin).
The DIP packaging was pretty nice, though, as I could use wirewrap sockets and wire the board without much trouble. (I can’t find my old hobbywrap wirewrap tool, and wire wrapping seems to have gone out of style with hobbyists—too bad as it was a very cheap, low-tech way to connect circuits, with less trouble than soldering and more robustness than breadboards.)
For my Arduino, I wanted a DAC that I could plug into the Arduino board directly, without ribbon cables or other complications, and that would not use up many of the I/O pins on the Arduino.
Modern DACs are mostly serial-interface devices, to cut down on pin count, so the resolution of the DAC is no longer tied to the number of pins used to interface to it. Since DACs have gotten dirt cheap in the past 30 years, I figured I’d go with a 12-bit DAC instead of an 8-bit one. This may have been a foolish choice, as a 16-bit DAC would not have cost much more and might have simplified the interface software (12 bits are not aligned to 8-bit words).
I ended up choosing a Texas Instruments part (TLV5618A) which has 2 12-bit DACs in an 8-pin DIP. Three pins are used for the serial digital interface, two for the analog outputs, two for power, and one for an analog reference input. TI recommends a 2.048 voltage reference when using a 5v power supply, probably to make the voltage steps be a uniform 1mV.
If I’d been thinking clearly, I would have simply hooked the reference pin up to the 3.3v power supply on the Arduino (the data sheet says that the reference could be as high as Vdd-1.5v, which would be 3.5v), but instead I used a pair of resistors (a 22kΩ and a 15kΩ) to make a voltage divider that produces about 2.03 volts. I also put a capacitor between the reference pin and ground, so that it wouldn’t be too sensitive to noise on the power supply. Someone who cared more than me would probably use a zener diode to set the reference voltage more precisely with less noise, but I had resistors and capacitors on hand and would have had to wait several days to buy a zener diode. (It would also irk me to pay more for shipping than for the 40¢ part.)
The outputs are connected (via 2.2kΩ series resistors) to a headphone jack. The DACs are capable of powering a small headphone (like an earbud), or you can use powered computer speakers or a headphone amplifier. The series resistors are to keep from accidentally shorting out the DACs.
I soldered up the board (including a socket for the DAC). One of the things I don’t like about the Ladyada prototyping shield is that the only access to the Arduino pins is through a set of female headers. I’m thinking of removing the both those headers and the male ones that connect to the Arduino on the bottom, and using long-tail female headers to both connect to the Arduino board and provide the ability to stack on top of the protoboard. That would free up the holes where the female connectors are now to solder wires to. Ladyada sells the connectors as shield stacking connectors for Arduino, so I’m a little annoyed that they didn’t design them into the prototyping shield in the first place. It would be a trivial change to their design, not even requiring a new PC board—just different header pieces and new instructions.
In a later post I’ll talk about how to communicate with the DAC over the serial link.