This Instructable will detail a basic transistor and what it can be used for. I am making this because I have had a box of assorted transistors for a while and never really knew how to use them in circuits. After reading some tutorials online I combined some ideas together and figured out how to use transistors in basic circuits.
I will demonstrate the use of transistors by controlling PC fans from a computer's LPT (parallel) port. Then I will implement a simple form of PWM (pulse-width modulation) to control the fan's speed. This demonstrates the ability of transistors to use low-voltage, low-current signal lines (such as a parallel port or microcontroller IO line) to control higher voltage, higher current devices like motors (in this case, PC fans).
Step 1: How a Transistor Works
Note that the load and the signaling devices must share a common ground. In the case of a fan (running from an external power supply) and a computer's LPT port, you can connect the ground terminal of the power supply to the ground pin on the LPT port to achieve this. For a microcontroller you can use the same ground for the chip and the load.
For my examples I soldered three NPN transistors to a small PCB and wired all of the emitters together to form a common ground. I did this to make connecting things easier. In the following examples I am only using one of the transistors. The other two are not affecting anything and can be disregarded entirely. The transistors are "MPS A06" labeled. According to the datasheet they can control up to 300mA and have a voltage limit at 80V. On my PCB, the brown wires are common ground, blue is collector (load), and white is base (signal).
Step 2: Programming for the Parallel Port
The input and output functions are in a header called sys/io.h (some sites say asm/io.h, this is outdated and doesn't work in 9.04). The function ioperm() gives you access to the LPT port and the function outb() outputs binary to the port. The base address for the port is 0x378 and we are going to control all the outputs together, so writing a 255 to the port turns on all 8 output pins, writing a 0 turns them all off. Other combinations involve converting the number to binary which isn't going to be described in this Instructable.
Here is a sample program that will turn on the signal lines for 10 seconds and then turn them off. Compile it with g++ in the form "g++ lptfan.cpp -o lptfan.o" replacing lptfan with whatever you save it as.
//lpt controlled fan
Step 3: Connecting the Transistor, Fan, and Computer
For this step, I'm using a PC fan (12V), a 9V transformer, and my transistor board to connect the fan to the PC. I have the base (white wire) connected to pin 2 on the LPT port, the collector (blue wire) connected to the fan's negative wire, the fan's positive wire connected to the power supply's positive wire, the brown wire to one of the LPT port's ground pins, and the other brown wire to the power supply's negative wire.
Step 4: Turn on the Power!
With everything connected, run the program you wrote in step 2. You'll have to use sudo and enter your password because port access is restricted. Start it by typing "sudo ./lptfan.o" (again typing whatever you called your program instead of lptfan) and hitting enter.
The fan should start spinning at full speed, run for 10 seconds, and then turn off.
Step 5: Write a Program for PWM
As this is a quick program I threw together, there is no good way to exit it, just do CTRL+C to kill it.
#define base 0x378
using namespace std;
void runFan(int powerLevel)
//Turn the fan to 100% for 1 second to get it spinning
//Loop to generate PWM at a given percentage
int powerLevel = 0;
cout << "Power level %: ";
cin >> powerLevel;