Build your own lightsaber (sounds)! - Part 1

by <a href="http://community.silabs.com/t5/Welcome-and-Announcements/Community-Ranking-System-and-Recognition-Program/m-p/140490#U140490"><font color="#000000"><font size="2">Ninja</font></font> </a> lynchtron on ‎11-09-2016 11:05 PM - last edited on ‎02-21-2017 11:45 AM by Administrator Nari

14_title.png

In the last chapter, we learned all about how to create sound using an external I2S chip and digital sound files.  In this chapter, we will get a little bit more in depth with sound and generate simple tones with a GPIO pin.  Then, we will use the integrated 12-bit DAC within the Wonder Gecko to produce sound with a more analog nature and run that through an external audio amplifier.  We will blend together sounds from multiple lightsaber sound effects and trigger those sounds with the accelerometer from chapter 10, resulting in our very own lightsaber sound effects generator.

 

Sound effects for consumer gadgets can be more forgiving than high-end musical audio.  We can get by on just a bare GPIO pin if we are careful about the type of sounds that we try to reproduce.  For some gadgets, perhaps a few beeps, clicks, and scratchy noises will do the trick.  As we move to voice tracks that have a wider range of audio frequencies, more fidelity is needed.  The 12-bit DAC on the Wonder Gecko (also available in most EFM32 models, check the datasheet) can be used to generate sound effects, voices, and lower-quality music.

 

The EFM32 can drive up to 20 mA on a single GPIO pinThis is probably not going to do the trick for the volume that we need on a small speaker.  If we tried to drive a speaker directly from a GPIO pin, it could damage the GPIO or just result in low volume audio.  In order to boost the volume, we need to amplify it in either voltage swing, current capacity, or both, to produce the desired power and sound levels for the situation.

 

Since we have already learned how to use an accelerometer with I2C in chapter 10, we can dust that off now and use it to detect motion that triggers lightsaber sounds that are stored on the MicroSD card that we used in the last chapter.   We will drive all of that out of the DAC on the EFM32 and then on to a TI differential mono amplifier.  But before go there, we will take a step back just a bit and learn about how to make the most rudimentary sound with nothing but a GPIO pi and, a speaker.

 

The source code for all examples in this book can be found on github, here.

 

Materials Required for this Chapter:

Sound and Amplifier Theory

Audio theory will melt your brain.  When you go looking for information on how to build audio circuits for your electronic gadgets, you will quickly run into discussions of physics, calculus, and all sorts of horrendous-looking mathematical expressions.  But all I want to do is play some sound effects!  I hear you.  I ran into this problem when I first tried to build my first audio circuit.  I still have a lot to learn, but I will share with you some simple things that I have learned that should give you a good foundation to learn more. 

 

In the old days when we played music on a record player or a cassette tape in a tape player, the audio information was stored and retrieved in a completely analog format.  The resulting audio signal consisted of sine waves and were fed into an analog amplifier,  then routed through a speaker and turned into sound, which we can hear.  


Today, most of our music that we enjoy is stored or transmitted as a digital representation of an analog signal.  So, digital is the starting point for your gadget sound effects.  In order to turn that digital information into an analog signal suitable for amplification, a Digital-to-Analog Converter (DAC) is used, such as the I2S DAC that we learned about in the last chapter.  A DAC is capable of outputting an analog voltage, that is, a voltage that is not a binary 0 or 1, but some small division of voltage in between.  Any discussion of DAC’s then requires the explanation of sample rates and bit counts and the minimum number of those to accurately reproduce the audio for our ears.

 14_analog_vs_digital.png

On the one end of the spectrum, we have the old-fashioned and warm tones of the old analog audio format, and on the other end, we have the precisely calculated, perfect-in-every-way digital recordings of the modern era.  Yet, there is middle ground!  By using nothing more than a GPIO pin and purely digital signaling, we can produce analog sound.  The reason why this works is because of filtering.  The digital pulses that we will use to generate audio from a GPIO are fast-switching square waves, and the impedance of the audio speaker coil and magnet is an inductor, which is a low-pass filter.  It simply won’t allow the high-frequencies of the digital signal to make it through the speaker coil.  The impedance of a typical 8Ω speaker limits the switching frequency, so that what is left on the speaker cone is an average voltage over time.  If we can alter the average voltage value many times a second, we have a cheap DAC.

 

Since we have to control the GPIO many times per second, we will use the EFM32 timer circuits connected to a GPIO in Pulse Width Modulation (PWM) mode to make the quick changes necessary from instant to instant in order to reproduce a sinusoidal waveform.  The rule of thumb is to run a sample rate that is 10 times as fast as the frequency of sound that we want to create and a PWM switching frequency is that is 10 times as fast as the sample rate.  So if we want to create a 4 kHz tone for example, then we should use a 40 kHz sample rate and a 400 kHz PWM switching frequency.  We will vary the PWM duty cycle every audio sample, which will then run for 10 PWM clocks, before the PWM duty cycle is changed for the next sample.

 

Once we have created an analog signal from the digital audio data, we need to amplify it if we want to be able to hear it loud and clear.  This is another area of electronics where your research can cause your head to explode.  There is an enormous amount of information and opinions on the Internet, and it can get you sidetracked from finding your way to a solution.  There are many ways to amplify a signal.  The type of amplification that we will be learning about in this chapter is known as Class D, but also goes by the name of PWM or simply digital amplification.

 

Class D amplifiers use the same PWM principle that we have just learned about, but are connected to more powerful current and voltage sources than the source audio device.  They are up to 95% efficient because at times where there is no audio data, the PWM duty cycle is 50%, and the output signal stays quiescent on both inputs to the speaker.  If there is no change in output voltage to either pin of the speaker, then there is no current consumed by the speaker.  Class D amps are more efficient than Class A, B, or AB, which are all more analog in nature and can waste power when there is no audio data to amplify.  Later in the chapter, we will use TI’s TPA2005D1 Class D differential mono amplifier.

 

In the next section, we will create a simple audio tone on a GPIO pin, examine that waveform on an oscilloscope.

 

 PREVIOUS | NEXT