Transistor controlled by Arduino
This example shows how to hook up a transistor so that it can be controlled by an Arduino. The Arduino can either use the transistor as a low-power switch to turn on higher power loads, or as an analog amplifier for converting small amplitude signals to larger amplitude signals. There are examples of code for both uses.
Note: These examples use the Arduino 5V pin as the power supply and LEDs as the "load" for simplicity. The power coming out of the Arduino 5V pin (max 40mA) is generally too small to drive much more than a few LEDs or a very small motor. For larger loads, such as bigger motors or relays, or stronger lights or sound, a stronger power supply, such as from batteries or a power adaptor, would be used instead.
Transistor as switch
The following code uses the transistor as a switch by sending current directly to the base of the transistor.
Transistor as amplifier
The following version of the code uses the transistor as an amplifier by pulsing the base of the transistor at varying degrees in order to simulate an analog signal.
Note: unlike the example of using a transistor as an amplifier based on an analog source signal, the transistor in this example is not truly behaving in an analog fashion. That is because the Arduino is not capable of producing truly analog signals.
The transistor in this example is actually being used as an on/off switch, but the switch is being pulsed on and off very quickly at a varying frequency in order to simulate an analog signal.