Thursday, April 16, 2015

A Simple Headphone Amplifier

I was recently presented with a situation that may have need for a headphone amplifier.  The incoming signal will be from a mixing console, either an Aux or Sub output, and possibly in parallel with a power amplifier providing audio elsewhere.  And that pretty much defines the application.
So time for a little background research.
Mixing Consoles  I'd expect that most people know what a mixer/mixing console is, but for those who don't: a mixer takes in all the sound sources (microphones, guitars, keyboards, pre-recorded or streamed audio, etc.) of a performance (music, theater, radio show, etc.) and puts it all together into a either a single mono or stereo feed for the house speakers or for a broadcast (they are used in studios for all parts of recording too, but I'm pretending we're just considering the live case).  The mixer allows you to set the volumes of each part, pan the sound to the left or right speaker, and usually allow some basic EQ.  Most mixers beyond your basic ones will also have a whole host of auxiliary inputs and outputs for adding extra effects, sending out monitor mixes (so the musicians can hear what they're playing or singing), and pretty much any other reason you could think to send different mixes around.  It's these auxiliary outputs that will be of interests to us.  If you really want to know more, just google "mixing consoles" and you'll find plenty of info.
Just to get a feel for the console outputs, I looked at the specs for a number of mixer manufacturers including Mackie, Peavey, Yamaha, and Behringer and found that typical outputs are around 22dBu max output level with an source impedance of 120R.  I'll go into what that means later.
Amplifier Or perhaps more specifically, a power amplifier.  Amplifiers do pretty much what you expect; make something bigger.  But what actually is that "something"?  Well often within electrical circuits it will simply be a voltage.  However, when you want to make that voltage do something in the real world you need power, and that is where things get interesting.  You will need more than voltage alone.  Take the starter motor in your car, for instance.  The vehicle battery is only 12V but can pump out the 1.5HP (746Watts/HP) or so to get the engine started.  The 48V phantom power from a mixing console?  It can usually illuminate a few LED's.  That source impedance I mentioned earlier will come into play....
What I am showing above is the scenario I just presented.  You should notice that there is a series resistance internal to both the car battery and the mixing console phantom power supply.  In the case of the former, the internal resistance is simply a fact of life due to battery chemistry and physical interconnects and ideally is as low as possible.  For the latter, the internal resistance is intentionally added to protect the internal circuitry from supplying larger currents than intended (including short circuit loads) as well as possibly protect musicians from handling a faulty microphone that might now expose them to 48V.  I've been zapped in the face by 120V due to a combination of bad guitar amp wiring and a microphone.  It sucked.
Anyhow, for the car battery we have a low internal resistance compared to the load.  And we see that most of the power is delivered to the starter motor.  Looking at the values, the battery is pumping out 200W + 1000W for a total of 1200W.  That's quite a bit.  If you were to take a multimeter and probe the battery terminals, you would see 10V instead of 12V as shown in the diagram.  When you first turn on the starter its resistance is closer to a dead short meaning that you could measure something like 4V across that battery.  That's why all your dash lights dim.  Another thing to note is that the 200W internally absorbed turns into heat.... so don't crank on it too long.  But you get the idea, voltage sources have internal resistances that absorb some of the total power and drop some of the voltage.
The phantom power example just demonstrates the opposite.  In this case, the internal resistance dominates, leaving little for the actual load (the LED).  R_internal limits the available current and in turn limits the deliverable power.
This brings us to the "Maximum Power Transfer Theorem":  to deliver the most power, the load needs to be equal to the source impedance.  However, that doesn't mean that is the goal of a power amp design.  Let's look back to the battery.  If R_Starter was set equal to R_Battery we'd see 1800W delivered to the starter.  But we'd also see 1800W delivered to the battery's internal resistance.  Not so good.  At these power levels this is woefully inefficient.  You can afford to throw away a few mW lighting an LED, but 1800W?  You can run a microwave off of less.  And here we are just letting it go to waste.  Not to mention that it's all turning into heat....  Something to consider for design later.
Our amplifier will need to be able to deliver enough current at a given voltage to power the load: headphones.

Headphones  Seems kinda self explanatory.  The headphones convert the electrical signal to physical vibration.  Sound.  While the mechanism is actually rather simple, I won't go into how they work here as it's not critical.  Feel free to investigate on your own.  But what kind of voltage levels do you throw at headphones?  What kind of impedance do they have?  What is loud?
Well I own a pair of Sennheiser EH-150's.  Nothing fancy, but a starting point.  Fortunately, Sennheiser has specs on their website.  These headphones have a 32R impedance and a sensitivity of 115dB.  That's loud.  Even if you don't understand the math.
Let's look at the impedance first.  The mixer has an internal impedance of 120R, and from the previous discussion, we know that the smaller impedance of the headphones will mean that it'll be difficult to deliver power to them.  At least we've got a feel for that side of things.
Now let's consider the sensitivity spec.  Often this spec is given as the output in dB @ 1mW, but Sennheiser actually specs it as the output dB @ 1Vrms.  This will actually be convenient for later analysis.  So we know that a 1Vrms signal will output 115dB.  I want to stop here as this post is getting long and further discussion may need more math primers.  But, we've got a better understanding of real world values and considerations for the problem at hand.

Monday, April 13, 2015

Virtual Whammy Bar Part 6

Now that the hardware is complete it's time to program our PIC16F916 chip. In reality, I was actually writing short program snippets for each of the sub-circuits previously covered to verify their operation. After a little work it was all pieced together and tweaked to produce the working software.

So here is the general operation:

Not a whole lot going on here. There isn't even a state machine implemented, though the interrupt routine kinda resembles one.
Maybe a point of interest, when the IR strobe is turned on, the timer is set for a very short time to allow the circuit to settle. The ADC conversion is then started and the timer is then set for a much longer interval, much longer than the conversion takes in fact. It's no big deal to let the ADC result go slightly stale, as it's still too fast to be perceptible. The overall sequence of turning the IR LED on, starting the ADC, and doing something with the result happens around 120 times a second (about 8.33ms). This is actually slightly more frequent than necessary; while you can send about 1000 MIDI messages per second, 100% utilization is unrealistic if you want real-time feel. For example, if you play 100 notes simultaneously from a keyboard, the resultant audio will not be simultaneous as the last note will turn about 100ms later than the first one. And yes, that is quite noticeable. I know that I've played a keyboard thru a PC with 25-30ms latency (delay) and the disconnect between my fingers and ears makes it very difficult maintain tempo and rhythm. But my main point is that firstly you don't want to update the MIDI channel faster than you can send messages across that channel, and secondly it is not preferable to flood the channel with messages as the other side also has to respond to all those messages. Conversely, you don't want to update too slowly due to my little story above. So 8.33ms is fast enough to seem instantaneous, but 10-15 would suffice.
The LED digit displays are driven via a typical though crudely implemented software PWM. Each digit is only active half the time (since I only have one display's worth of output) but at a fast enough rate that they look continuously illuminated. I've also included adjustable brightness, so that while each digit is "active" half the time, they may only be illuminated for a portion of the time they are active, hence a PWM drive. Shown below is "half" brightness.
Not much else of interest software-wise. The buttons are checked on a software maintained timer that is.... loosely consistent. The ADC result is checked against the previous result so as not to continually send the same value if unchanged. Also, only the upper 7 bits of the 10 bit result are used (MIDI values are typically 0-127) so the three LSB's are sorta like a noise filter. I can explain the "User Interface" in a later post, but I've touched on all I've wanted to for now.


Friday, October 17, 2014

PS3 Cooling Fan Part 2

The first step of any project is to figure out the top level layout.  I have added the arbitrary constraint that this design will not use a microcontroller or any other high level component.  Just want to do something more.... analog.  Previously we figured out the building blocks, and actually worked off of a mental layout that is pretty much shown below.
It's really quite basic, but shows each part in need of design.  I did add the Fan Driver as an afterthought because we will need a means of driver large current to the fan.  But this shouldn't be too complicated, probably just a MOSFET.  Actually, we might as well knock this part out first since it will be simple.
OK, so slightly more than just a MOSFET.  Barely.  A diode is placed in parallel with the fan to prevent a small catastrophic catastrophe.  Let's consider this simplified model:

The fan has been replaced by an inductor and our PWM source (V2) is 100Hz with a 50% duty cycle.  On the right I'm showing conceptual ON and OFF state circuits where our MOSFET goes between a very small and very large impedance.

Pushing current through an inductor is a lot like starting your bike from a stop while in a high gear.  If you apply some pretty stiff force you can actually accelerate pretty quickly though you will feel quite a bit of resistance from the bike.  And as you get up to speed you'll start to approach a steady velocity, and the bike will feel much easier to pedal.   The inductor behaves somewhat similarly.  When a voltage is applied, the current has to "accelerate" over time to a steady state.  The graph below shows an inductor being charged then discharged several times.
Unfortunately the bike analogy falls a little short because charging and discharging the inductor has the behavior.  You can coast on a bike to a stop, or apply brakes, but neither reflects the behavior of pedaling.  The important thing to grasp here is that current through an inductor can not change instantaneously, even though the voltage applied can (in an ideal world).  It's almost like the current has a momentum.

So what happens when a branch get stuck in your spokes?  You flip over the handle bars.  The bike has stored up energy while at speed, and when the wheel is suddenly stopped it is dissipated in the form of you flying through the air and landing on the pavement.  This is the same as the MOSFET turning off.  An open circuit is basically an infinite resistance, or in my little example above, a very large resistance.  Let's suppose the device is switched on, given our values we'd see 24A flow once the inductor is charged up (I = 12V / .5Ω).  Now the MOSFET is suddenly turned off.  We know that the current through the inductor won't change immediately, so what happens to voltage across the MOSFET?  Well, Ohm's law: V = 24A * 5MΩ.  That's 120 megavolts!  Where there's smoke, you're spending a buck on a new transistor.  My example is totally unrealistic for many reasons, but the concept is there.  The circuit above simulated with the an IRF510 and with the fan inductor having a series resistance of 10Ω yielded this result:
Sure enough, there are voltage spikes.  The green signal is our PWM signal that turns the MOSFET on and off; the blue is the Drain pin of the MOSFET.  And as predicted, when turned off there is a very large but brief voltage.

Back to the backwards diode.  Now when the MOSFET turns off, the current has somewhere to go.  It just cycles back through the motor until the stored up energy is depleted.  No more voltage spikes.
This is commonly known as a freewheeling diode or a flyback diode.  It is good practice to throw them on any inductive load being driven by a switched DC source, such as relay coils, solenoids, and DC motors in general.  That's enough for the Fan Driver circuit.  Next time we'll start to figure out how to control it.

Monday, October 13, 2014

Virtual Whammy Bar Part 5

At this point it might be easiest just to show you the full schematic and break it down.
And there we have it, nothing too crazy. And yes, I'm not showing bypass caps and such (well honestly, I still need to add them to the real thing, but I know they should be there). So let's get into the remaining parts.

The MIDI interface looks like a standard serial connection, but it's not quite. MIDI uses a current loop so our output has a send and return path for the signal (contacts 4 & 5). On the input side of whatever device we are controlling will be an optoisolator providing... well... isolation. If you visit MIDI.org Elec Spec you will see the complete electrical interface. You may wonder why I didn't use the two inverters they show on the spec and here is why: that circuit is dated 1985. I'm guessing that at the time there was concern about how much current an I/O pin could sink. Dunno, I was maybe two feet tall at the time. But given the resistors in the current loop as 220Ω each and a forward voltage drop across the optoisolator of about 1.3V we would have 5.6mA. For a short across contacts 4 & 5 there is still only 11.4mA. According to the PIC16F916 datasheet, any pin can sink up to 25mA so long as all the I/O combined do not sink a current in excess of 90mA. I think we're safe.

On the right hand side we have two buttons with a simple RC filter for debouncing. The filter the cutoff frequency just wants to be as low as possible while still being faster than the rate that the buttons are sampled. This should allow for quick transitions while smoothing out any noise. There are only two buttons because that is all I had at the time.


The ICSP header allows for programming and debugging the chip in circuit via a Microchip programming tool such as an ICD3 or PICkit3. Not much to say here, if you're interested just google it or head to Microchip's website.

Lastly we have the two 7 segment LED displays. Again, I only had two on hand, but that was enough. I used the entirety of Port B of the PIC16 as a single byte output to keep things simple. I didn't pay much attention to how they were wired to the microcontroller, so long as they were wired the same. Either way, I'd have to make a conversion table in software to display numbers or characters, so my table is just.... unique. Also, I switch between which display is being illuminated by means of the "Digit" signal. When "Digit" is high, the ONES display is active, and when "Digit" is low, it becomes the TENS display. All I do is switch back and forth between them around 100 times per second and they both appear constantly on. This is easily done by alternating every time I check the buttons or check the position sensor. You may notice that "Digit" only drives Q2, and that the base of Q1 is tied to Q2's collector. How does the TENS display work then?
If you look at the simplified circuit shown and make a few approximations this can be easily understood. We'll assume that SIGNAL is 5V, Q2 is off(ie, "Digit" is low), the voltage drop across each diode is the same, and that the current gain of Q1 is 100. Since no current flows into Q2, we can formulate the following equations (again based on approximations):

Vb = Vc
Va = 5-(Ir * R) = V(D2) + Vb = V(D1) + Vc
Ir = Ic + Ib
Ic = 100*Ib

Let's just consider the current formulas. Since Ic is 100 times larger than Ib (and that is the minimum spec'd gain of a 2n3904), very little current is diverted from illuminating the TENS segment to drive Q1. So if Ic is 1mA, Ib is only 10uA, which also explains why the ONES segment doesn't appear to illuminate. Kinda neat. Didn't even need to consider the rest of the equations. This was initially a breadboard mistake that I had to evaluate after the fact to determine why it worked. Of course the real operation varies slightly from the approximation. The voltage drop over D2 will likely be smaller than D1 since it won't be driven into saturation, subsequently making Vb larger than Vc. What does this change? Not enough to care about.

So that completes the hardware portion of things. Software is up next.

PS3 Cooling Fan Part 1

Back in college I bought myself a Playstation 3 which has gotten plenty of use.  While I play games less and less, it still gets plenty of on time for dvd's and blu-ray's, Netflix, Pandora, and whatnot.  Other than a failed optical drive while still under warranty, it hasn't had any problems.
Then recently, while watching a movie, the PS3 beeped and turned off, leaving a blinking power light.  Subsequent attempts to unplug it and power it back off yield the same result.  Well now what?  While the price of a new system isn't that bad with the release of a new generation of consoles, I still payed a lot for the system I have when I bought it.  Not to mention all accumulated files and progress stored on the drive which is not as easy to recover as one would expect.  Off to the internet!
After a little searching around I found a solution.  Apparently the CPU and/or the GPU can heat themselves up so much that they can weaken their solder joints.  I believe they're BGA packages, so you can't bust out the soldering iron.  Fortunately, YouTube has a solution:

Watch PS3 Red Light Fix on YouTube

The basic rundown is that you have to pull the system apart and get the "motherboard" for lack of a better word.  Then you take a heat gun and warm up the board then focus on the CPU and GPU chips with high heat.  This should reflow the solder on the BGA pins and fix any broken connections.  Then apply some good quality of thermal paste and reassemble.  While I can't verify that the causes I've suggested above are actually the problem, I can vouch for the solution.  It does work.  I've done it on more than one system..... even the same one twice.... which gets us to the project at hand.
While first using the system, I had a small desk fan blowing across the system to force a little more air over the heatsink for the PS3.  After awhile I got a little more confident in the fix and stopped using the fan.  So one night while watching Netflix I fell asleep, and woke up later..... only to have the flashing red light again... Well, time to pull it apart and reflow again.  What I need is a fan that turns on and off with the PS3, and while we're at it, speeds up when the system gets hotter.  And a circuit that will make this work.

The Project:

Alright, the desk fan is out.  It's too big and clunky, and yes it could probably just be turned on and off with the PS3 via a relay, but what fun is that?  Now on to what I have laying around.  How about a pair of 92mm case fans?  These Nidec Beta V's (TA350DC M35105) fans are rated up to 5,700 RPM and 1.8A.  I'm going to stress at this point that I have no experience driving fans like these.  Sure I've driven some basic DC motors by varying the voltage or using a PWM, but this required a little more thought.  A PWM drive is the starting point.
OK, the fans are taken care of, but what about a heat sensor?  Well I don't have any thermistors and couldn't buy any locally.  But I did recall from my engineering courses that semiconductor devices have a negative temperature coefficient.  Sure enough, after a little research, I found that a silicon diode drop will vary around -2mV per °C.  This means that if your diode drops 600mV at room temperature, when heated 10°C above ambient, it will only drop 580mV.  And this behavior is pretty linear across temperature and consistent from part to part.  And yes, resistors also have a positive temperature coefficient, but it never crossed my mind to use one.  The change in voltage will be dependent upon the applied voltage for one (the voltage will proportionally change the same regardless the applied voltage, but it's a matter of dealing with uV or mV for actual change in voltage; larger applied voltage = larger observable change).  Also, the temp coefficients can be pretty small with resistors, and I have no idea what they are for the ones I have.
Now we've got fans and a candidate for a temperature sensor, how are we gonna power our circuit and get it to turn off and on?  Well the PS3 is really just a computer packed to look like a gaming system.  It has to support various configurations and use some modular componentry such as the hard drive, optical drive, USB, wireless, Bluetooth, etc.  And like a PC, it has a dedicated power supply module that is similar to ATX supplies.  When the system is off there is 5V standby power to illuminate an LED and operate a soft switch circuit.  When the appropriate signal is applied to the power supply, it turns on the main 12V rail and the PS3 fires up.  All we have to do is tie into the 12V portion of the supply.
We have a pretty good start: fans, temperature sensor, drive mode (PWM), and power supply.  Time to design.

Saturday, September 27, 2014

Another ATX bench supply....

So for a long while I've wanted to make a bench power supply from an old ATX supply from an old computer.  As I don't have a lot of equipment on hand, I've had to resort to scavenging wall transformers or power bricks to do my projects.  I'm still using a 1A wall supply a teacher had us modify(just cut off the barrel plug and added banana plugs) in our introductory electronics course in college.  But while I can get a decent variety of voltages by hacking up a unused supply I have to keep a box of these around and most can only offer up to 1A or so.  And negative voltages?  Not without some work.
These limitations can be frustrating.  It's a hassle to rummage through the box of supplies to find the right voltage with enough juice.  Once I was trying to diagnose the cabin blower fan for a friend's car in the middle of a cold winter.  I removed the fan from under the dash and wanted to test it.  But when it's -10F and dark outside, you'd prefer not to stand outside and try wiring it up to the vehicle battery.  However, testing indoors is a little tough when you need a +12V supply that can provide, say 10 or 20 amps.  It would have been nice to have that ATX supply handy...  In the end I had to confirm the operation of the fan out in the cold.  Turned out the fan worked, but the contacts in the fuse panel had roasted themselves to the point of melting some of the plastic and were essentially open circuits.  A little dis-assembly and sanding of the contacts got things working again... so far.
Well back to the project at hand: the ATX bench supply.  Initially I did a search of the internet and found that many people have done just what I planned.  So I plodded on without reading their write-ups cause I wanted to do my own thing.  I grabbed a 250W unit out of an old Dell and got to it.
The first task: turn the ATX supply on.  This I had already accomplished while troubleshooting some motherboards for various people.  All it really took was a web search for the ATX power spec which provided this little diagram:

This pretty much tells us everything we need to know.  The main Molex plug that normally goes to the motherboard includes all the signals and voltages.  All the other wires going to plugs for various drives, fans, and whatnot aren't really necessary and can be removed altogether, which I did.  So turning this thing on.  You'll notice as signal called 'PS_ON#'.  When this signal is connected to ground our supply fires right up.  Design wise, the project is kinda done.  The rest is physically rigging the supply up to be practical.
You can see that I've added a couple LED's for indication.  There is always a low current standby 5V source when AC is plugged in (via 5VSB), which will be designated by the yellow LED.  When the outputs settle, the PWR_OK signal goes high, which I use to drive the red LED on.  I didn't find how much current the PWR_OK signal could source, so to play it safe I just drove a transistor with it.  Not much else to really say.
Unmodified power supply.


Wire bundle from power supply.  Most redundant wires were removed.

Terminal strip mounted, dry erase marks for switch and LED drilling.

Internal modified wiring with switch and LED board mounted.  Used a spare piece of proto board cut from another project, held in place using hot glue.

Power supply reassembled.  Future modification would be to drill a new hole to feed the wires up in order to clean things up.  Other than that, works just as intended.

Tuesday, August 12, 2014

Virtual Whammy Bar Part 4

I would like to quickly go over the power supply section. Though not shown on our basic layout, we have one, and it's a given that any project will need to consider how power will get to the circuit.
Didn't do anything special here, just a plain 7805 linear regulator. Almost straight off the datasheet. Again, used caps I had available. As an aside, my GND's aren't really grounds, but commons. The GND symbol is just really easy to grab in the schematic editor I'm using, and I figure we all understand functionally what is going on. But I didn't really want to talk about how this circuit is designed so much as I did the ramifications of the circuit.
Linear regulators are simply inefficient. In this setup using a 9V battery, 4V have to be dropped across the regulator in order to produce the 5V output (I am assuming an ideal battery for the sake of discussion). At the same time, the rest of the circuit for the project is going to need what current it needs. Knowing that Power = Volts x Amps, we can see that about 44% of the power delivered by the battery is dissipated across the regulator as heat for any given current draw. So 56% efficiency. Ouch. My initial design for the whole project only drew about 70mA, which I thought was pretty decent. But a quick search of the internet revealed that a typical 9V battery has around 565mAh (milli-amp hours) of life. My device would drain the battery in only 8 hours!
This is where switching regulators become really nice. Some are drop in replacements, and others require a few external components, but all are still simple enough to work with. And with efficiencies north of 90%, they are a very attractive option. Lets consider our 9V battery in terms of mWh instead of mAh. That means we have 5085mWh of life in the battery. Multiply by the 56% efficiency of the linear regulator and we have only 2848mWh remaining to power the circuit. The circuit itself draws 350mW. Again doing the math we see 8 hours of life. Let's consider a 91% efficiency switch regulator now. Only 9% of the power is lost regulating, leaving us with 4627mWh. Now the device can last 13 hours! Not great but still better.
At the end of the day what I really did was make the whole circuit more efficient. This was done through a combination of selecting better resistor values, and writing code to turn things on and off as needed to save on idle power draw. After several iterations of changes the current draw was reduced to <10mA. Now I could operate the device for at least 56 hours (again, ideally). I could take it a step further and add a switching regulator to eclipse 90 hours, but guess what? I don't have one of those lying around.....