# A Simple Introduction to Transistors and PWM (Pulse-Width Modulation)

The silicon transistor was invented in 1954 and has been considered one of the greatest inventions in the history of technology. Its invention practically spawned the field of electronics and contributed to all of our modern computers, iPods, phones, etc. If you've ever asked what a transistor is or does, you probably were told that it is like a switch. However, it is a bit more complicated than that.

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).
Remove these ads by Signing Up

## Step 1: How a Transistor Works

In this instructable I will be focusing on NPN transistors. A transistor has three terminals: Base, Emitter, and Collector. The base terminal is connected to the signal voltage, the collector is connected to the load, and the emitter is connected to negative (ground). Whenever the base voltage is zero, the collector voltage is also zero. Applying a small current to the base causes a proportionately larger current to flow through the collector. By doing this, a tiny amount of current from a signal can be used to drive larger currents to power heavier loads such as relays and motors.

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).
usbg3rd3 years ago
What is the code if i use c++ aur visual basic 2008? Can any one help me
ARJOON3 years ago
how can i use it to produce a square wave current. that will then be amplified by mosfets. the frequency must be variable
luckyvictor4 years ago
how to set up the experiment for the fan speed testing please?
CalcProgrammer1 (author)  luckyvictor4 years ago
Look at the diagram in Step 3, it shows how to wire the fan, power supply, transistor, and parallel port.  On the software side you need to produce a pulse-width modulation signal (turning on and off really fast) with a specific duty cycle (duty cycle is the ratio of time on to time off, a 10% duty cycle has the switch on for 1/10th of the full cycle time, so 0.1ms if you are using a 1 millisecond cycle time).

In code, you can use a loop from 1 to 100 that sleeps a constant amount of time each iteration and turns off the pin when the counter hits the desired duty cycle percentage.  Put that loop in an infinite loop that first turns the pin on before starting to count.

C++ example:

while(1)
{
//turn lpt on
for(int i = 0; i < 100; i++)
{
usleep(1);
if(i == duty_cycle)
{
//turn lpt off
}
}
}
profpat4 years ago
this is a cool experiment, i will be doing this! thanks
CalcProgrammer1 (author) 5 years ago
Awesome, going to order an Arduino this weekend. This method works for any general signal-level output (microcontroller, parallel port, arduino, etc). I ported my PWM generator to regular AVR code over the weekend and tested it on a board I borrowed, seems to work better on the AVR since it has a steady clock and output. You can also use PWM output to control hobby and robotics servos. Their signal pins can be driven via parallel port or microcontroller as long as you connect the ground from the motor to your parallel port's ground or microcontroller's ground. I have some continuous rotation motors from a Vex robotics kit that I want to use with an Arduino or standalone AVR (the Vex controller isn't open source and is based on PIC's, I like AVR's more).