Several people wrote in about my earlier post concerning relays, noting that it’s a bit risky to control a relay from the Arduino as you can easily fry the IC chip. A safer method is to use a transistor, which basically accomplishes the same thing (and more cheaply):it lets a small current control a much larger one. This lets you use the Arduino to turn large electric devices on and off when they are running off a large separate power supply.
There are two type of transistor: NPN (negative-positive-negative) and PNP. The difference is whether you want to turn the transistor on with a signal that is a high voltage (5 V) or low (ground). NPN transistors are turned on with a high voltage, and that’s what we will use.
Transistors have three pins, with a plastic top with one side flattened. If you are looking at the flat side, the pins from left to right are called the collector, base and emitter. The middle pin, the base, is where you connect the control signal from the Arduino. The collector pin on the left is wired to the device, and the device must be connected to its own positive power supply terminal. The emitter pin on the right is connected to ground for the Arduino, AND to the ground of the outside power supply. The Arduino and the device being controlled must have a common ground.
To have the Arduino run a motor, first put the transistor in a breadboard. Connect the ground on the Arduino to the ground channel on the breadboard, and the ground channel to the emitter pin on the transistor. Wire pin 9 on the Arduino to the base pin of he transistor. Wire the collector to one of the motor terminals, then the other motor terminal to the positive terminal of a battery pack. Then wire the negative terminal of the battery pack back to the ground channel on the breadboard.
This code has the Arduino pulse the motor on and off at 1 second intervals.
int transistorPin = 9;