Learn

Wiring an NPN Transistor to a Raspberry Pi 4B (GPIO Switching)

Use an NPN transistor (2N2222 or BC547) to let a Raspberry Pi 4B GPIO pin switch a separate power circuit — wiring, Python code, and what to check if the LED won't turn on.

Try this directly in the free Raspberry Pi 4B simulator — no hardware or signup required. New to the GPIO header? Start with the interactive pinout guide.

What you’ll need

Step by step

  1. Drag Raspberry Pi 4B and Breadboard onto the Canvas.
  2. Drag the NPN Transistor onto the breadboard. Hover over each of its 3 legs to confirm the labels — you should see Base, Collector, Emitter clearly named (don't guess the layout from memory; trust the on-screen labels, since 2N2222 and BC547 don't share the same physical pin order).
  3. Drag two Resistors onto the breadboard, and set their values: one to 220Ω, one to 1kΩ (click each to select it, adjust the value in the settings panel).
  4. Drag an LED onto the breadboard.
  5. Wire the base circuit (the small “control” signal): Connect a GPIO pin (e.g. pin 37) → through the 1kΩ resistor → to the transistor's Base.
  6. Wire the load circuit (the thing being switched): Connect the Pi's 5V pin → through the 220Ω resistor → to the LED's anode (+). Connect the LED's cathode (−) → to the transistor's Collector. Connect the transistor's Emitter → to a GND pin.
  7. Go to the Code tab and write a simple test script:
import RPi.GPIO as GPIO
import asyncio
basePin = 37
GPIO.setmode(GPIO.BOARD)
GPIO.setup(basePin, GPIO.OUT)
GPIO.output(basePin, 1) # base HIGH -> transistor conducts -> LED should turn ON
await asyncio.sleep(2)
GPIO.output(basePin, 0) # base LOW -> transistor stops conducting -> LED should turn OFF
await asyncio.sleep(2)
GPIO.cleanup()
  1. Click Start. You should see the LED turn ON for 2 seconds, then OFF for 2 seconds — controlled entirely by the small signal on the Base pin, even though the LED's actual power is coming from the separate 5V line, not the GPIO pin itself. That's the real thing a transistor does: a small control signal gating a separate, larger circuit.

What “working correctly” looks like

If something’s wrong

More component tutorials are in Learn.