Tag Archives: LED

All About That Base Are Belong To Us: Controlling an LED with an NPN Transistor

unforeseen problemFull disclosure: I have no idea what I’m doing. If you arrived here by googling CONTROL LED NPN TRANSISTER then you probably know only slightly less about electronics than I do.

Frankly I’m not real clear on exactly what voltage is. People keep telling me charge is like a swimming pool, and the voltage is how much water pressure you have, and the current is how much water flows out of the pool, and resistance is how big the pipe is that the water’s flowing out of, but honestly that makes no intuitive sense to me. No sense at all.

Apparently resistors have something to do with Buddhism because ohms? What. Evs.

I’m a huge fan of getting things working first and understanding them later, so let’s just barrel right ahead and be willing to break stuff for science. These components are relatively inexpensive, so even if you burn a few bucks you’ve gained some knowledge from the carnage.


Here’s what you’ll need:

parts

One. A power source. I’ve got an old 7.5V wall wart power adapter scavenged from what I’m guessing was a D-Link router at some point in the past. You can use any power source you like for this, just so long as you’re pushing somewhere around 5V. Four AA batteries will work great for this. Don’t get your power directly from the wall unless you’re seeking a new career as a fried ham.

The leads on this power supply are stranded wire, which doesn’t jam into breadboards very well. I’ve soldered them solid– not strictly necessary but helpful. If you do this yourself please don’t solder the leads while the power supply is plugged in. Is bad idea.

I’ve used blue painters’ tape to indicate the positive lead on this. Red’s the conventional color, but I ain’t got no red tape, son. Blue it is.

Two. An LED! You can buy these guys by the dozen for cheap, and some places will even sell you resistors in the package. I’m using a green LED. These tend to run somewhere around 3.3V (this matters, and you’ll see why in a moment.)

Any DIY project is improved by adding LED’s to it, so you really ought to have a few thousand of these at the ready. Making a Halloween costume? LED’s. Grade school diorama? LED’s. Bran muffins? LED’s, baby.

Three. Some resistors! Here’s why that 3.3V matters: remember that the power supply I’m using is 7.5 volts. Hook that puppy up to a dinky little 3.3V LED and kablooie– the LED will flash briefly and then die forever. The resistor’s going to limit the amount of juice flowing to the LED by turning excess electricity into heat and dissipating it into the air.

Trenchant insight alert: THIS IS WHY ELECTRONICS GET HOT WHEN YOU USE THEM.

I’m using a 12K and a 5.6K resistor for this project, which I picked at random from a pile of bent resistors in the bottom of my toolbox after a 100K resistor blocked too much current to light an LED.

Voltage calculations and predictions as to whether I will eventually burn up my components are left as an exercise for the reader. EE’s are free to leave helpful comments below.

Fo’. An NPN transistor! It’s tough to buy just one of these. Here’s a pack of one hundred, so you can screw up a bunch of times. We’re going to use one transistor as an electronic switch. What you do with the other 99 is up to you.

Five. A short length of hookup wire. Pretty much anything conductive will work for this– don’t worry about gauge as we’re not exactly building a space probe here. Use a paper clip if you’ve got nothing else.

Six. A solderless breadboard! (not pictured) You’ll probably want at least three of these in your house so that you can leave half-finished projects assembled while you attend to more pressing duties, but you only need one for this project.

The Basic LED Circuit

Let’s assume that you’ve never done this before. We’ll wire up a basic LED circuit to build confidence and then move on to adding the transistor.

Step 1: Connect the power to the breadboard. Plus to plus, minus to minus.

power hookup

Step 2: Connect the 12K resistor to the + strip and somewhere else on the breadboard. We’ll use a second resistor later when we want to avoid frying the transistor.

add resistor

Step 3: Connect the long lead of the LED to the free end of the 12K resistor.

add LED

Step 4: Connect the short lead of the LED back to ground with a piece of hookup wire.

LED circuit

And if all goes well, you should have glowing LED, with brightness dependent on your combination of resistor and power source.

So that’s LED’s 101. I still don’t know how this relates to swimming pools.

The Transistor Circuit

Let’s complicate things just a bit by adding the NPN transistor. Pro tip: It’s always helpful to remove all cats from the work area.

cat

Let’s not dwell on the difference between NPN and PNP transistors: there are plenty of other places on the internets to explain that, full of confusing diagrams and weird-ass equations. For morons at our level it’s enough to understand that when electricty touches the middle pin of an NPN transitor, current flows through the transistor. Otherwise, the transistor acts like a closed gate, and no electricity passes through it.

I’m using a 2N4401 transistor, but any NPN should work for this little tutorial.

Step 6: Take out the LED and hookup wire and insert the transistor so the curved side is facing away from you.

add transistor

Step 7: Connect the long lead of the LED to the rightmost leg of the transistor. Wire the transistor’s leg back to ground with your little piece of hookup wire. If you’re fortunate, nothing will happen.

wire to ground

The LED should still be dark, but if it’s glowing dimly then (I guess?) a little bit of current is passing through the transistor. You can use a more powerful resistor or lower your input voltage by swapping out your power supply.

Or just plow on ahead and don’t worry about incinerating your components. That’s what I’d do.

Step 8: This is where the magic happens! We’re going to apply current to the transistor’s middle leg, which will permit current to flow through the transistor and light up our LED.

transistor circuit

Just to be on the safe side I put my 5.6K resistor in between the source voltage and the transistor’s middle leg. Of course you could read the datasheet and know for sure how much voltage that middle leg can handle (spoiler: it’s 6V read Andy’s comments below) but reading datasheets is for suckers who didn’t buy a 100-pack of transistors.

This Circuit is Stupid

Yeah, I know. But it’s a proof of concept, right? Instead of keeping an LED lit (lame) one might be using this transistor with an Arduino digital pin wired to the middle leg. One could toggle massively interesting circuts by writing HIGH or LOW to the circuit from the Arduino.

Or, OR! In theeeeeeeeeeory, one could use this basic circuit to resolve conflicts among I2C devices with identical hardware addresses by interrupting the SCA signal, if one had accidentally purchased A TSSOP multiplexer and SOIC breakout boards and had a week to kill while the proper components were being shipped to his lab and needed to feel like he was making forward progress on some front for the love of Pete, because none of us are getting any younger, you know, and Time is the enemy.

I’m not saying that happened. But it could have. In theory.

#staytuned, my friends. Lao Zheng out.

Hack-O-Lantern

Halloween fast approaches, and I still haven’t managed to make that electroluminsencent Riddler costume I’ve been dreaming about for the last two years. But this year I did manage to come up with a nifty Hack-O-Lantern that uses an Arduino and a pair of diffusers that I printed on my MakerBot Replicator. Here’s what the animation looks like, including my new favorite function, derp().

My apologies for the soul-deadening ambient light in the video. The Hack-O-Lantern looks a lot cooler in person, although if I had more time I’d try to boost the voltage to the LED’s and brighten them up a bit. Right now they’re running off straight off the Arduino, and I didn’t want to burn out any pins by driving too much juice. Maybe next year.

You don’t need to use an Arduino to use these diffusers: if you’d rather just stick a couple of LED’s in there with a watch battery taped to the leads, that will work just fine. The LED’s in the top photo are running in series off 4X 1.2V NIMH 2500 mAh rechargable C cells, and they look great.

The diffuser has a slight lip on the back that you can use to score your pumpkin’s flesh before cutting.

Nightmare fuel, anyone? Here’s all 14 LEDs soldered to hookup wire, fed through the pumpkin’s eye holes.

Once I connected the LEDs to pins 0-13 on an old Arduino Duemilanove I had kicking around (SCORE for finding a set of headers I’d forgotten I ordered six months ago), I put the whole contraption in a plastic bag so the pumpkin guts couldn’t short the hardware.

Working inside that cavity gives you a lot more respect for brain surgeons.

If you’ve carved a pumpkin recently, you’ve probably got some seeds kicking around. Here’s what I’ve been doing with them lately:

Zheng3 Szechuan Pumpkin Seeds

approximately 1.5 cups of pumpkin seeds, washed.
1 tablespoon doubianjiang
1 teaspoon light soy sauce
1 teaspoon hot chili oil
2 tablespoons peanut oil
1 tablespoon Szechuan peppercorns

Mix everything except the peppercorns in a bowl and toss to coat. Set aside for an hour to marinate.

Toast the peppercorns in a wok over medium heat until fragrant. Crush with a mortar and pestle.

Spread the pumpkin seeds and marinade evenly on a flat baking tray. Bake at 350° for about 20 minutes. Sprinkle with the crushed peppercorns and serve.

If you can’t find doubianjiang and you’re not willing to wait for a shipment from Amazon you can probably substitute some garlic powder mixed with Sriacha rooster sauce.

Approximating the flavor and mouthfeel of Szechuan peppercorns is more difficult. Try this:

Dip a jalapeño pepper in powdered laundry detergent and suck on it for 30 seconds. Then put your lips across the terminals of a 9V battery.

It tastes better than it sounds, believe me.

You can download the STL’s and Arduino code here.

Socket to me.

I can’t think of many designs that aren’t improved by making them glow. LED’s are the tinker’s equivalent of Photoshop Layer Effects.

The LED socket is my attempt to play industrial designer; I wanted to make an object that was intuitive, elegant, and easy to use. So I started small.

It’s designed to hold a 5mm LED and battery without trimming the leads.

The recessed cradle for the LED is straight on one side so the user is guaranteed to get the orientation of the cathode correct. The grooves on the sides should just fit 5mm LED leads if they’re bent with two 90° angles around the bottom of the socket.

The bottom of the socket is also grooved so that the socket can stand on its base like a candle.

Instructions: Feed the leads through the holes at the top of the socket. Insert the battery through the hole at the bottom of the socket. If the bulb doesn’t light, flip the battery around.

Once the bulb is lit, bend the LED leads around the bottom of the socket and press them into the side grooves to keep them out of the way.

This uses a DL1025 or equivalent battery.