Raspberry Pi Beginners Tutorial 4: Using Transistors

Transistors can be used as a switch in a circuit, enabling you to control a high power component without damaging your Raspberry Pi's GPIO pins.

The GPIO pins are only capable of supplying a low amount of power. If you try to power a component that requires a lot of power, you can cause permanent damage to the Raspberry Pi.

To get around this issue, we can use a transistor in the following circuit:

Using Transistors with a Raspberry Pi wiring diagram schematic

Open IDLE on your Raspberry Pi (Menu > Programming > Python 2 (IDLE)) and open a new project (File > New File).  Then type the following:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while True:
    GPIO.output(18, True)
    time.sleep(1)
    GPIO.output(18, False)
    time.sleep(1)

Save your project as bright.py (File > Save As) in your Documents folder.

Now open Terminal (Menu > Accessories > Terminal) and type the following command:

python bright.py

The ultra bright LED will now blink. You can stop this program from running by pressing CTRL+Z.

In this circuit, the transistor acts like a switch. When a small current is applied to the Base pin (which is currently connected to GPIO pin 18), the transistor allows a larger current to flow between the collector and emitter (which are connected to Ground and the LED respectively).

Therefore, the ultra bright LED is drawing its power directly from the Raspberry Pi's more capable Ground and 3.3v Pins, but can still be controlled by a much smaller current from the GPIO pins.