Modifying the hardware to add the LEDs
We're really racing along now. All we have to do is add 14 LEDs -- seven for each channel. Just to make things visually interesting, I decided to use two red LEDs for the lower-frequency bands, three orange LEDs for the middle-frequency bands, and two yellow for the higher frequencies.
Each LED requires a current-limiting resistor, which is sometimes called a ballast resistor. Otherwise, we could easily blow it up. The data sheet for a LED will detail a number of parameters, including its forward voltage (Vf) and its forward current (If). The data sheet may also specify minimum, typical, and maximum values, in which case we'll use the typical values. All my LEDs had a typical forward voltage drop of 2 V and a typical forward current of 20 mA.
The formula we use to calculate the value of the current-limiting resistor is R = (Vsupply - Vf)/If. Since I'm using a chipKIT MAX32, my Vsupply is 3.3 V, so this gives R = (3.3 V - 2.0 V)/0.02 A (where 0.02 A = 20 mA), resulting in a resistor value of 65 Ω. The thing is that resistors come in a range of values. The closest value to what I need is 68Ω, so that's what I used.
The color code for a 68Ω resistor (the three colored bands on the resistor) is blue-gray-black. You can find all sorts of information about resistor color codes on the Internet, including this rather nice resistor color code converter. Another useful tool is this LED resistor calculator, which rounds the result to the closest resistance value that is actually available.
As an aside, if I'd been using an Arduino Mega with its 5V supply, I would have required 150Ω current-limiting resistors with color codes of brown-green-brown.
Some electronic components, like the resistors and capacitors we used earlier, are nonpolarized, which means it doesn't matter which way we connect them. (Some capacitors are polarized, but we didn't use any of them.) However, LEDs are polarized components; they can be connected only one way round. If you look closely at a LED, you will see that one lead is longer than the other. The longer lead is the anode or positive terminal; the shorter one is the cathode or negative terminal. The cathode also has a flat side on the body of the LED. Keeping this in mind, the following image reflects the addition of the LEDs.
In each case, we start with one side of a 68Ω resistor connected to the '-' (ground) rail at the top of the breadboard. The other side of the resistor is connected to the cathode (negative) side of the LED (the one with the flat side). The anode side of the LED is connected -- via a flying lead -- to a PWM output from the microcontroller. Once again, let's do a quick reality check in the form of a photo of the real boards.
If you are unfamiliar with the PWM concept, you can discover a lot of information on the Internet, so we will cover this very briefly. A LED can be either on or off; it cannot be dimmed in the same way as an incandescent light bulb. If we wish to vary a LED's brightness, the solution is to turn the LED on and off very quickly. If it's on only 50% of the time, it will appear to be only half its full brightness. If it's on only 25% of the time, it will appear to be only a quarter of its full brightness, and so forth. If we turn the LED on and off again thousands of times a second, we won't be able to detect any flickering. It will simply appear as though we are brightening or dimming the LED.
In the case of a PWM output on our microcontroller, we can assign it a value between 0 and 255, where 0 means the pin will be LOW (off) all the time and 255 means the pin will be HIGH (on) all the time. A value of 64 means the pin will be HIGH 25% of the time, 128 means it will be HIGH 50% of the time, 192 means it will be HIGH 75% of the time, and so forth.
Page 1: Introducing the MSGEQ7
Page 2: Creating the first-pass hardware
Page 3: Creating the first-pass software
Page 4: Modifying the hardware to add the LEDs
Page 5: Modifying the software to drive the LEDs
To Page 5 >