Sunday, 2 August 2009

Using a Transistor as a Digital Switch

A basic requirement for digital electronics is to switch on a higher current device using the low current capable IO pins of a microcontroller.

A microcontroller is typically capable of outputting only a limited current of 90-200 milliamps (mA) across all of its IO pins. Even if you only have to light up a couple of bright LEDs that will not be enough and drawing more current than allowed could damage the microcontroller. To limit the current drawn whilst still supplying enough power to the controlled devices you need to use a device that can act as a digital switch. A transistor can fill this role, drawing only a few milliamps from the microcontroller whilst the controlled devices draw as much as they require.

There are a several types of transistors which can allow you to switch on or off anything from LEDs to motors. The decision for which transistor to use depends on how fast the switch needs to be flipped, and how much power will flow through it.

TO-92 Packaged BC547 TransistorBipolar Junction Transistor (BJT)
A BJT transistor such as the NPN BC547 shown on the right can function as a digital switch. When a small amount of power is supplied to its base (middle leg), power is allowed to flow between its collector (left leg) and emitter (right leg). When the base is grounded no power can flow between collector and emitter.

There are two types of BJT transistor, NPN and PNP. NPN is far more widely used and in the majority of cases NPN is a better choice. The circuit symbols are shown below:
Transistors and Digital IO - PNP vs NPN

Here is a simple circuit demonstrating an NPN transistor being used to switch on an LED:
Transistors and Digital IO 

The circuit uses the 3.3V supply from the microcontroller’s IO pin, limited to 3.3mA by the 1K resistor, to allow the 5V supply, limited to 50mA by the 100 ohm resistor, to flow through the LED.

I built the circuit using the Digi Connect ME microcontroller and a breadboard:
Digi Development Board

The gator clip wires shown on the left connect up the Digi development board’s 5V supply to the breadboard’s left hand power rail. The white wire connects the first IO pin of the microcontroller (GPIO1) to the breadboard’s 1K resistor at the base of the BC547 transistor:

Breadboard Showing Transistor Circuit

If you have to switch higher current loads, say to drive a motor or a pump, different types of transistors may be applicable. The BC547 has a maximum current throughput of 100mA, so it’s only suitable for small loads such as the driving this LED. In particular Field Effect Transistors (FET) may be a better choice for many higher current robotics applications.

The Microsoft .NET Micro Framework (.NET MF) C# code used to test out the circuit is here:
http://purerobotdigiconnect.codeplex.com/SourceControl/changeset/view/29696#691604

2 comments:

Anonymous said...

This is a good tutorial. I have linked in my blog: www.snackhack.wordpress.com

Do you think you could do a follow up on how to bring a circuit to ground using a transistor switch?...'cause that's what I'm up to right at this moment.

PCG

Luke Thompson said...

Sure, I'll put it on my list of posts to work on.

Thanks for linking to the post - I took a look at your blog and your project; good luck with it.