I want to build an electric circuit which has to have a power of nine volts. Can I use a circuit like this, without breaking the Pi?
Or is there a better solution?
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It only takes a minute to sign up.Sign up to join this community
I would not recommend calling a transistor a relay. While they would do essentially the same thing in this particular case (switch an electrical signal) they're technically different with respect to how they are driven and what kind of signal they switch. I will assume to drive an LED here (joan's answer mentions that additional considerations might be necessary for different kinds of load).
A transistor is a semiconductor device used to amplify or switch electronic signals and electrical power.
A relay is an electrically operated switch.
For a bipolar transistor - that is driven by current not voltage - a series resistor between the GPIO pin of the Pi and the base of the transistor is recommended. This is typically around 10 kOhm (see here, R1). Dimensioning this resistor appropriately however requires the specifications of the particular transistor in question and the load it drives. Using the bipolar transistor as a switch (opposed to its use as an analogue amplifier) typically drives the transistor into the so called saturation region - “fully-ON” with the lowest possible resistance across the transistor (and thus the smallest voltage dropout and thermal dissipation). To do this the base resistor is calculated based on the base current required to turn the transistor fully-ON. This minimum base current is calculated from the collector current (the current that drives the load - your LED) and the DC current gain (a parameter obtained from the transistor's data sheet). Learn more.
It is also noteworthy that the limited current a GPIO pin can support (<16 mA) also limits the models of transistors that can be directly driven from the port. For BC547 or 2N3904 driving some LED that should pose no problem.
However, that unsuitable (yet somewhat viral) circuit shown in the question (the emitter follower) has a major drawback: the transistor on the high-side (near the positive supply voltage) is not a useful circuit with an NPN transistor. It will not work of the the voltage drop across the load (the LED) is higher than the voltage level of the Pi's GPIO pin (3V3) which could easily be the case for a white LED. Connect the emitter (the arrow) to ground and put the load (LED) between collector and +20V instead.
One additional thing: due to its non-linear characteristics (I will skip the details that are found somewhere else) the LED requires a series resistor to limit its operating current to a save value. See here.
This is how a simple circuit driving a LED from GPIO pin should look like. Note that selection of transistor and resistor are depending on the current through the LED. Assume GND of the Pi's power supply and the +20V supply are connected.