Microprocessor output strength:
Most microprocessors have outputs that are not very strong. Many can sink (drive to ground) less than 20ma. That's about what you need to drive an LED but not a motor.
Protecting the Microprocessor:
You always want to protect the micro's inputs and output pins. The easyist way is to simply add a gate between the micro and the power device driver. Any logic gate that is compatible with your micro can be used. In most cases this gate can be zapped and spare the micro - keeping your little rug-runner from becoming brain dead. Gates are cheap, micros are expensive.
Another way is to use the following circuit which clamps out-of-range voltages:
+5v | -------- _|_ 1N4000-4 | /_\ Diode | I/O pin 220 ohm | Micro |<----------\/\/\/\---o-------- to circuit | _|_ | /_\ 1N4000-4 -------- | Diode GNDOpto-isolation can be used to protect circuitry but it's ususlly not necessary unless you're driving plasma cutter and other high-frequency/high-voltage devices. If you're still interested, look up data on the 4N25.
For loads < 200ma:
Many loads are small but just too big for a micro to drive. This includes small lights and small motors. Make sure to measure the current draw of your device before deciding on a drive scheme. Make sure to stall motors to see what their maximum current draw is.
My favorite parts for driving small loads < 200ma is the popular 2N2222 transistor. These can often be had for less than 10 cents each. They come in small plastic cases (TO92). When looking at the front of the package (flat side) with the legs going down, the pinout is E B C (Emitter, Base, Collector). It doesn't take a EE degree to use these great parts - here's an example. Notice that the transistor drives the load to ground.
+5 | / \ R3 ------- / 2N2222 ------ | \ _____| |___/\/\/\___ +5v | | R1 B |/ C | lamp | R2 Micro |<----/\/\/\---------| ------ | |\ E ------- | GNDR1 is not needed if the output of the micro is simply a week pullup-open collector/drain like the those found on the 8051. Otherwise, R1 is needed to limit the current through the base of the transistor. Try 1k ohm for R1 if your micro has a strong output.
R2 may be needed to limit the current through the load. Use Ohm's law to calculate.
R3 may be needed to pull up the input if the micro outputs nothing and has no internal pull-up's such as PO of the 8051. A 1k to 10k works good here.
Again, you can put a gate between the micro and transistor to add an extra layer of protection.
For loads > 200ma:
Ok, now for the big stuff you've been waiting for. MOSFETs are powerfull transistors that are great for doing this sort of thing. It's not a problem to drive loads > 10 amps using a MOSFET. Parts like the IRF540 are good for this job. Normally you can get them for $1-$3 depending on specs. Standard MOSFETs need 10volts at their Gate pin to turn them on completely. This can be done with a 7406 level converter or a 2N2222 transistor. This level conversion usually inverts the signal so make provisions in your software. There are logic-level MOSFETs now that turn on with only 5v. Here's an example:
+12v | \ / 10k \ ------- / IRF540 D -------- | |\ | |----| vacuum |----- +12v Micro |---| \O--o--------| -------- | |/ G |-- S ------- 7406 | GNDLooking at the MOSFET with the legs pointing down the pinout is G D S (Gate, Drain, Source).
These circuits are dealing with low voltages (<12v). For higher voltages, make sure to check the transistor specs.
Driving inductive loads:
Inductive loads have coils in them. Examples include motors, relays, and solenoid valves. When you turn an inductive load ON then OFF, it creates a voltage spike. Normally this spike will distroy a transistor. Sometimes a MOSFET will handle this condition but it's best to protect the transistor with a simple diode across the load. Use 1N4000 diodes for 1 amp loads and 1N5400 diodes for 3 amp loads. Simply put the diode across the load with the cathode at the + power supply and the anode at the other side that connects to the transistor.
Software and variable duty cycles:
In software you can simply turn ON or OFF the device by sending a 1 or 0 to the port pin. Remember that your drive circuitry will determine which will turn the device ON. Another not-so-obvious possiblity is that quickly turning ON and OFF the device can result in variable speed for motors and variable intensity for lamps. You can do this by setting up a timer or simply do a software timing loop. Normally a frequency of 100 to 1000hz will do the job just fine. Experiment, experiment, experiment.
Power up condition:
One often overlooked issue is that of power up conditions. When your micro is turned on, the output pins assume a default condition. Often this condition is HIGH (5v). If this is driven directly into the 2N2222 circuit shown above, then the device with turn on as soon as power is applied. Even if the micro's program quickly turns off the pin by sending a LOW (0v), you could still get a quick jolt on a motor or other device which could be bad. Use a gate such as a 74HC04 to invert the signal if needed.
That's a rap.