Nothing is quite so humbling as someone coming in and showing you that you are making your problem way more complicated than it really is. Lee pointed out that I was overthinking it: that a simple 2N2222 would work just fine, and indeed, I went back over it, and realized he was right. I entered in the schematic shown below into LTSpice....
And it looks like it will work fine. I had thought that the 2N2222 would have to dissipate too much power, but I made mistakes based upon two problems: I had placed the current limiting resistor on the emitter side of the circuit, which meant that the base of the transistor was improperly biased, and I had originally used a 12v supply. In the configuration above, less than 50mw gets dissipated by 2n2222, and just about 1/2w by each of the current limiting resistor and the LED. Cool.
And thanks Lee, for taking the time out of your day to lend advice to such an introductory topic.
This posting begins with a caveat: while I'm pretty experienced as a programmer and software engineer, I'm actually a bit of newbie when it comes to electronics. I'm entirely self-taught, and because of my inherent laziness, I find it hard to learn anything before I actually need to know it. This means that I often get in a bit over my head, and only then start to figure things out and find out where my preconceptions and misunderstandings have lead me astray.
This happened with a project I was working on last night.
The basic idea is that I wanted to use PWM to control a high power (1W) Cree LED. I had the program running nicely on my little ATtiny13, and if I hooked an ordinary (20ma) LED to the pin through a current limiting resistor, all was well. But I knew that the Arduino couldn't supply the current necessary to light the Cree, so I needed a switch.
And so, I'm off into territory I hadn't been in before.
In my junk box, I have a bunch of IRF510s. I think to myself, they can handle high power, I could use them to switch the LED. So, I hooked it up. I put the LED in series with a 100 ohm resistor for safety, and wired that through the source/drain of the MOSFET to a 9v supply. I sent the voltage from the PWM on the microcontroller to the gate, and added a 10K resistor from the gate to the ground. I powered it up, and... well... it worked!
I then decided to drop the current limiting resistor's value. The easiest way on the breadboard was to parallel another 100 ohm resistor in place. Sure enough, when I switched in that resistor, the LED got visibly brighter. Good deal, think I.
But then I moved on. I had been powering the ATtiny13 from the 5v supply on my USB port. I didn't really want to do that in the final configuration. I didn't have a spare 5v regulated supply, but I know that the ATtiny can operate down to 2.8v. I had a pair of fresh AA batteries, so I plugged those in, and sure enough, the processor booted and ran just fine.
But then I noticed that the overall brightness of the LED display seemed pretty weak. And when I paralleled in the extra resistance to drop the current limit, the LEDs got no brighter.
It only took me a few minutes to figure out what the deal was: the voltage driving the gate of the MOSFET wasn't strong enough to open the MOSFET entirely. The MOSFET itself must be limiting the current.
That sent me back to the web to look at datasheets (which mostly confused me) but one thing became clear: the IRF510 wasn't meant to be driven from logic levels. The important figure would seem to be specified as the Drain to Source Resistance (rDS(ON)). For a gate to source voltage of 10V, and a drain current of 3.4A, the resistance is typically about 0.4 ohms. But if the gate to source voltage is only 5v, what happens? What if the gate to source voltage is only 3v?
I stared at the Fairchild datasheet some more. Figure 6 was telling: it showed the graph of the saturation characteristics of the IRF510 at a variety of gate/source voltage levels, ranging from 4V to 10V. For each VGS value, the graph shows the relationship between the VDS and ID, the current through the drain. For a VGS of 5V, the curve is remarkably flat right around 1 amp. But if you look at the curve around 4V, it's flat right at zero. In other words, the MOSFET doesn't switch any current at all. And who knows what happens below that.
When I am running my circuit at 3V, I'm barely tickling the output. In fact, the specifications say that it shouldn't work at all at 3V. My hypothesis is that it's just a fluke: my transistor is actually just conductive down at that voltage level. (The appropriate specification would appear to be VGS(TH), the gate to source threshold value, which is specified to be between 2.0 and 4.0 V)
By the time I got all this figured out, I started tinkering with LTSpice. I learned a bit, but it really would be helpful if I had a model for the LED that I am using. But I did discover that by mucking around with the LED supply voltage and the gate/source voltage, I could emulate some of the features I saw in my actual circuit. It does seem possible to limit the LED current to 200ma or so with a 5V VGS, but it won't reach anything close to that level of current with only a 3V VGS.
Thus, what seems like a very simple electronics project turns into a lesson. I can proceed along a number of possible avenues to make it work:
- I could try to make it work with a 5V logic level, still using the IRF510. I suspect that since I don't need a full ampere of current through the drain, that is at least possible. I plan on using a 6V lantern battery for the LED supply. I could inject 5v onto the gate, and empirically test the current through the LED by just measuring it with my multimeter, and adjusting the current limiting resistor until I get the current I desire.
- I could ship to using a common Darlington (maybe a TIP120 or similar, which you can even get at Radio Shack). That will let me run the processor from just two batteries, but a lot more power will go into driving the Darlington than is consumed by the entire rest of the chip. I could of course use a Darlington at 5V as well just fine.
- Did I mention overall power? It would be great if this thing could be powered by a 6v lantern battery, but if I want to drive the chip at 5V, any of the common 7805s I have in the box won't work (drop out voltage would be too high). Maybe just a Zener diode supply?
I have to remind myself: projects like this are like musical scales to a budding pianist.