# Using a MCU's UART as DAC

Many simple MCUs do not provide DACs despite having PWM output. For low clock frequencies like 8MHz, PWM does provide very poor quality if used for audio purposes. E.g., TI mentions in application notes that 32kHz PWM with 8 bit timing resolution would result in an analog signal quality compared to 4-5 bit resolution DAC.

I wonder if one would be better off using UART TX at the highest possible baud rate - e.g. some MCUs would push out up to 400kBit/s at 16MHz.

Compared to the PWM method with its 8 or 16 MHz time base, we have only 400kHz timing resolution for each bit, but on the other hand, we may do far more level switchings in some given interval.

• 32kHz PWM does 64k switches per second with an 16Mhz timing resolution.

• 400kBaud UART allows for 400k switches per second with 400k timing resolution.

If we convert 50k samples per second to 50k 8-bit dithering patterns, what effective audio signal quality can be expected?

• The theory is nice. But implementing it in practice wouldn't be easy... UARTs have no built-in facilities to send pulse trains with variable frequencies. Much hacking would be involved, if possible at all. – Eugene Sh. Jun 13 '16 at 13:23
• This is really not clear. Is the idea to have the UART TX pin as a PWM (which means having a RC filter directly on the TX pin) ? Or use the UART to transmit samples to an external DAC ? – dim lost faith in SE Jun 13 '16 at 13:23
• @EugeneSh. Then, how could UART provide better audio quality ? On all MCUs I've met, PWM frequency is way higher than the UART max baud rate. The 400kBaud is to be compared to the 8MHz main PWM frequency, not to the 32KHz sample rate frequency. I don't understand his maths. – dim lost faith in SE Jun 13 '16 at 13:30
• @dim For some reason the OP is assuming that the PWM frequency is 32KHz... – Eugene Sh. Jun 13 '16 at 13:31
• Delta-sigma is by far the best way to do this. – pjc50 Jun 13 '16 at 13:43

This is a little bit confusing, but I'll try my best to answer.

I think you are confusing the number of discrete combinations of bits, with the average level.

If we convert 50k samples per second to 50,000 8 bit dithering patterns

If we do this, we would have to be able to generate 28=256 different voltage levels after filtering. To do this, we must have room for 256 bits, meaning that you must generate a bitstream of 50k*256=12.8 Mbps. Far more than your 400 kbps.

This is because with 8 bits, there are 256 different combinations, but because you are oversampling and filtering, it's only the number of bits that counts, and that will be from 0 to 8. 0xAA has the same average level as 0x55, and 0xF0 etc.

A PWM with a faster rate will always generate a "better" signal. It's the raw bit rate that counts. Your raw PWM bit rate for a 32 kHz PWM signal with an 8 bit timing resolution is closer to 8 Mbps.

• Ah ok, so using regulary 8 bit dithering patterns and assuming linear conversion, I will only get 8 different values, so it's only 3 bit like quality. – dronus Jun 13 '16 at 22:44
• If so, I would be better of with 32kHz PWM with 8 bit resolution, which should theoretically give up to 8 bit but results in about 5-6 bits after filtering – dronus Jun 13 '16 at 22:45
• @dronus Yes, you will be far better off using the PWM, both quality-wise, and implementation-wise. – pipe Jun 14 '16 at 6:25

While the fixed-value start and stop bits at the output of the UART will impose some minor limitations on what you can achieve, it would certainly be possible to use a software delta-sigma modulator to produce the rest of the data bits from the analog value that you want to convert. You could even write the modulator so that it accounts for the fixed bits and optimizes the rest of the data around them.

However, 400 kHz is still not a great sampling rate for this kind of application, and you'll be trading off resolution for bandwidth. For example, if you want "telephone grade" audio at an effective 8-kHz sample rate, that's only 50 output bits per sample, or about 5-6 bits of effective resolution.

If you want "CD-quality" audio, you'd need to be operating the UART at several MHz in order to get both the bandwidth and resolution you need. For example, an oversampling ratio of 256x and a sample rate of 44.1 kHz (typical values for audio-grade DACs) would work out to 11.2896 MHz.

• This basically sums it up. People often misunderstand what is meant when a frequency is mentioned. Some clock using frequency X doesn't mean you can output an accurate stream at frequency X. 400 kHz is enough to carry audio, but it won't be the quality you expect. – Mast Jun 13 '16 at 18:56
• One may also attempt something similar using MOSI on SPI. – Nick Alexeev Jun 13 '16 at 19:58
• @vaxquis: Yes, that author made that statement, but he never justified it or otherwise explained what "software-induced errors and uncertainties" might actually be. Having done it myself on several occasions, I can assure you that a software implementation can meet the timing and accuracy requirements just as well as a hardware implementation. – Dave Tweed Jun 16 '16 at 10:42