fpga4fun where FPGAs are fun

PWM DAC 3 - One-bit DAC

Take one pin of an FPGA, connect a speaker and listen to an MP3? Easy. Here, we'll use a PC to decode an MP3, and then send the decoded data to an FPGA that is configured as a one-bit DAC.

Audio output

We require a DAC (digital-to-analog converter) to connect the FPGA (digital) to a speaker (analog).
The conventional approach would be to use a resistor ladder (see here), or use a dedicated DAC IC, like the venerable DAC-08.

Since the frequency at which FPGAs run is so fast compared to the frequencies required in the audio domain (MHz compared to kHz), a one-bit DAC is a better choice.

Basically, to create the analog output, we smooth out the PWM or sigma-delta modulator output pulses using a low-pass filter. A sigma-delta modulator is better because of its higher-frequency output content, with which a first-order low-pass RC filter is usually enough.

Playing an MP3

The first step is to decode the MP3. The decoded MP3 data is called "PCM" data. To keep things simple, we send the PCM data through the serial port of the PC to the FPGA. The maximum rate possible through the serial port is 115200 baud (i.e. about 11.5KB per second), so the music has to be down-sampled to 11kHz 8-bit. All that is easily done by a PC. Here's the software used (with source code).

And for the HDL code, we simply modify the sigma-delta modulator so that the PWM data input comes from the serial port.

module PWM(
  input clk, RxD,
  output PWM_out
);

wire RxD_data_ready;
wire [7:0] RxD_data;
async_receiver deserializer(.clk(clk), .RxD(RxD), .RxD_data_ready(RxD_data_ready), .RxD_data(RxD_data));

reg [7:0] RxD_data_reg;
always @(posedge clk) if(RxD_data_ready) RxD_data_reg <= RxD_data;

////////////////////////////////////////////////////////////////////////////
reg [8:0] PWM_accumulator;
always @(posedge clk) PWM_accumulator <= PWM_accumulator[7:0] + RxD_data_reg;

assign PWM_out = PWM_accumulator[8];
endmodule

Now it's time to connect a speaker to the FPGA. There are 3 basic ways to do it.



  • 1. A speaker is connected directly to the FPGA. Since the speaker is mostly inductive, we control the current inside the speaker. That works, but is only recommended with high-impedance input speakers (or earphones).
  • 2. An RC filter is used, then comes a speaker or amplified-speaker. That works only if the speaker has a high-enough input impedance so that it doesn't disturb the RC filter.
  • 3. An RC filter followed by a power op-amp. That's the way to go to get the best music quality.

That's it! Listen to your favorite MP3 through an FPGA.