In the last section, we set up a GPIO pin with a Pulse Width Modulation (PWM) mode of a timer to create a simple audio tone, and observing that waveform on an oscilloscope. We then amplified that tone with a crude single-transistor amplifier. In this section, we will connect the MircoSD card to the Starter Kit in order to play audio stored in a file and introduce the onboard Digital-to-Analog Converter (DAC) for single-ended and differential modes. We will also connected a Class D differential amplifier to the output of our DAC.
Drive Digital Audio Files to Your Speaker
Now that we have proven that we can make sounds using only digital outputs, we will re-use our MicroSD card example from chapter 13 and send those files through the onboard DAC to produce audio data that is more analog and requires no filtering for adding an amplification stage.
Download the lightsaber sound profiles from the repo (originally found on freesound.org thanks to Joe Barlow) and load them from your computer into your MicroSD card. These sound files are already in the .wav format, and that format will work for our needs in this chapter. If you had found sound effects in any other format, such as .mp3, you would need to use the Audacity program as discussed in the last chapter to convert those sounds to .wav format or build a .mp3 software decoder into your firmware solution.
Note that the FF library used by MicroSD library requires all of your filenames to be in the “8.3” naming format, which harkens back to the 1980’s DOS days. This means that your names must be named with fewer than eight characters, followed by a period, and then three more characters. If you use longer names or don’t follow this format, your files won’t open. So I renamed the files from freesound.org to a few files called swing0.wav and idle1.wav on my SD card and placed those in the source code repo. If you still have the voice tracks on the card from before, it’s OK to leave them on there so that you have some voice tracks for experiments. Then, connect the MicroSD card reader to the Starter Kit as follows:
Starter Kit MicroSD Card Breakout
PC2 – US2_TX, Location 0 DI
PC3 – US2_RX, Location 0 DO
PC4 – US2_CLK, location 0 SCK
PC5 – US2_CS, Location 0 CS
To ensure that the MicroSD card is working, load the sound_effects_player.c file, set a breakpoint in dac_helpers.c in the open_file function, and verify that you see “RIFF” in the wavHeader variable. This ensures that all of those connections are correct and at least the first file it is looking for is on the card. If you get stopped on a DEBUG_BREAK command, fix the hardware connections and try again.
Connect the Onboard Differential DAC
Some of the EFM32 family, including our Wonder Gecko on the Starter Kit, contain an onboard DAC peripheral and integrated Op Amp that can be used to play audio. The DAC is 12 bits, which is a little bit shy of what is considered to be high quality audio, but certainly good enough for our sound effects purposes. But the nice part about the DAC is that it can output an analog voltage, i.e. a voltage that is not limited to either 0 V or 3.3 V, and it can drive to any of 212 values in-between those two voltages, or 4096 steps. This means that an audio filter design is not required on the MCU outputs, and we can send the data from a DAC directly to any ordinary amplifier circuit.
The DAC is differential, which means that it can double the effective volume of your sounds by driving the opposite voltage to each pin of the speaker. Before, we simply connected one pin of the speaker to ground, but now we will connect and drive the two pins of the speaker to 0 V or 3.3 V on every edge, resulting in up to 6.6 V of total signal swing as seen by the speaker.
Note that sometimes in embedded development you need more MCU pins than are available. If this is the case and you need to save pins, you could use the Single Ended mode of the DAC and then send the single-ended signal through an inverter circuit. Just be sure to send both speaker inputs through the same circuit so that the signal timing will be identical. In addition, the way that you interpret and feed data to the DAC registers changes depending on single-ended or differential mode, so you will have to take that into account. This is beyond the scope of this chapter and left as an exercise for the reader.
The integrated Op Amp in the EFM32 is automatically used by the DAC and does not need to be configured separately, other than for configuring and routing the DAC output pins to alternate locations. For differential mode DAC, two of the integrated Op Amps will be used, leaving one of them free for other purposes. The Op Amp is still limited to 20 mA of GPIO pin drive and 3.3 V power supply voltage. It cannot add any more power to the circuit than those limitations.
Connect the TI TPA2005D1 Class D Amplifier
The volume from the integrated differential DAC good for sending to other onboard audio devices, but perhaps it is not loud enough to drive the volume that you want on our little speaker. Since the DAC is driving an analog voltage, it is easy to amplify using many types of available amplifiers. Keep in mind that all amplifiers are limited to the source voltage and current requirements on your board. In our case, we can use either 3.3 V or 5 V on the Starter Kit, and up to 500 mA of power from the USB port on the connected computer. If we exceed 500 mA of power draw through our amplifier, the connected computer will likely shut down the USB port to avoid damage.
Each GPIO on the Wonder Gecko is capable to drive 20 mA at 3.3 V on our Starter Kit. By driving differentially from two pins, this looks like 20 mA over 6.6 V to our speaker, or 132 mW of power. If we want to drive higher than that, we can turn to a differential amplifier, such as the TI TPA2005D1.
The TPA2005D1 is listed as a 1.4 W differential Class D mono amplifier. It can operate at up to 6 V, which means that it is able to drive up to 12 V of differential voltage to our speaker, while delivering up to 242 mA of current per output pin.
The gain of the audio amplifier is set by that are in series with the signal that is coming from our DAC. The gain is the amount of amplification factor that you wish to achieve with your amplifier. The recommended setting is a gain of two, which should double your volume, and it is set by choosing 150 kΩ resistors for those two series resistors RI with the following equation:
If you set these resistors to something too low, for example 0 Ω resistors, the amp will protect itself by going into shutdown mode. Start with 150 kΩ and then go from there. I have successfully run it at a gain of six by using 50 kΩ resistors.
The gain resistors on the TPA2005D1 are set to 50 kΩ with a pair of surface-mount 0603 size resistors just to the left of the input pins. If you would like to change the gain, you must remove those resistors or calculate the parallel resistance by adding new through-hole resistors over top the surface-mounted 0603 resistors. For example, by adding two new 150 kΩ resistors on top of the surface-mounted 0603 resistors, you will drop the resistance to 75 kΩ for a gain of four. Just be sure to use 1% resistors and apply the gain to both inputs because the differential amplifier works best if things are evenly matched.
The SHUTDOWN pin on the TPA2005D1 is important to designs that wish to save power. When the amplifier is enabled (SHUTDOWN pin is high), the amplifier will be consuming up to 4.5 mA of power. But with the amplifier disabled (SHUTDOWN pin is low), the amplifier will only consume up to 50 µA of power.
Connect the TPA2005D1 as follows:
Starter Kit TPA2005D1 Card Breakout Speaker
5V PWR +
GND PWR -
3V3 S (the shutdown pin)
PB11 IN +
PB12 IN -
OUT + Speaker +
OUT - Speaker –
Note that the polarities of PB11/PB12 and Speaker +/- are not important. All that matters is that you have two wires from PB11 and PB12 going to IN + and IN – on the TPA2005D1 and two wires going from OUT + and OUT – going to the two likely unlabeled pins on your plastic speaker.
In the next section, we will learn how to configure the differential DAC on the EFM32 to play higher quality audio from a MicroSD card.