Fun With Transistors

Transistors. Arguably the most important invention — ever! So what can we do with these things?

Mostly I use transistors as switches that I can operate from GPIO pins on a microcontroller, so I’m going to show you that here. GPIO pins can typically safely handle only about 20mA of power. But many things that I want to control from my microcontroller need a lot more current than that. So I connect a transistor base to the GPIO pin, and then I use the other two connectors as a switch for the higher-current device. The transistor I am going to show you here can handle 800mA (i.e., 40 times as much current as the GPIO pin). If I need to switch something that uses even more power than that, I can use the transistor to power an electromechanical relay. The relay I will show you here takes only about 100mA when it is on (although the current spikes higher than that when first turned on). This relay can nominally switch up to 10A at up to 30V DC, or up to 250V AC — although when I am working with higher power values I always use components with specs much higher than I expect to be working with. Probably a good rule of thumb is to use only 1/4 of the rated specs (so I would suggest switching at most 2.5A on this 10A-rated relay). Please also see the disclaimer below.

Alright, let’s build this thing…

The GPIO pin shown at the bottom left is a typical General Purpose Input/Output pin, like those found on many single board computers (SBCs) or microcontroller boards (MCUs) like many Arduino or Raspberry Pi computers. As I noted above, these GPIO pins should not normally be tasked with providing more than about 20mA of current to whatever you connect. In this case, I am using a 750Ω resistor, which will limit the current from a 5V source to a little less than 7mA. From the GPIO pin through the resistor, current flows to the base (labelled “b” in the diagram) of the transistor.

This transistor (highlighted in green and labelled Q1 in this diagram) is the very common standard model 2N2222, which is produced by many different manufacturers. It is an NPN bipolar junction transistor and is considered a general purpose transistor. This variety of transistor can be used as a switch across its collector (labelled “c”) in the diagram and emitter (labelled “e” in the diagram). The emitter is always connected to ground, and when a small positive current is applied to the base (flowing to the emitter0, the transistor allows a proportionally much larger current to flow from the collector to the emitter. This transistor has a gain of approximately 100. That is, whatever power you applied to the base will enable 100 times that amount of power to flow from the collector to the emitter. So applying ~7mA at the base as is being done here will enable up to ~700mA to flow across the collector to the emitter.

In this circuit, the relay is connected to +5V and to the collector, so when the GPIO pin goes high, the relay will turn on. The relay needs only about 100mA in steady on state, but somewhat more amperage flows when it changes from off to on. In any case, the transistor will be biased enough to enable almost 700mA flow through it, and the transistor is rated to a maximum of 800mA. So the values are all looking good here.

There are a few other things to note about the circuit.

It is a good practice to always connect a diode (D1 in the diagram) in parallel to the windings of your relays. When a relay is turned off, the collapsing magnetic field actually generates a power spike as it envelops the coil. That power spike could feedback into your circuit damaging things if you are not very careful. The diode will help by providing an alternate (unidirectional) flow for that power to protect your circuit. I used another standard part here, the 1N4001 model. This is an optional but recommended component.

Also optional is the circuitry that is shown in light gray. That circuitry (just an LED and a resistor) just provides a visual indication when the relay portion of the circuit is active. You can omit this without any negative impact on the circuit. I always like to include an LED as a troubleshooting precaution. If there is no LED there is no external way to tell that the relay is powered (unless you get out your multimeter). Of course with these electromagnetic relays, you may hear the “click” when they turn on or off, but in steady state is hard to tell (although they do generate a little heat). So I prefer to have a visual indicator.

Note that if what you are switching needs only a few hundred milliamperes of low voltage DC current, you don’t need the relay. You can safely connect your load directly to the collector (optionally, you can keep the LED part of the circuit too).

Note also that if you are switching an AC load, you have the option to use a completely solid state relay. Solid state relays cost more than the electromagnetic varieties, but they don’t have moving parts to wear out. Presumably they could last forever! If you are not familiar with the difference between Alternating Current (AC) and Direct Current (DC), Wikipedia has a good write-up on that.

Here are some examples of this circuit that I have built to use in various projects. Two are without a relay, and they have pins to connect the LED remotely (typically the LED will be poking through the side of the enclosure, with wires running back to these pins). The larger one has a relay.

When I show some project here I will usually provide an individual parts list with links to places to buy each, but in this case the components only cost pennies each and I usually buy them in quantity. If you just want one (or a small number) for some specific project you are probably better off buying a pre-made component for this (or a kit with all the parts). The task being performed by this circuit is very commonly needed by people who build things with microcontrollers, so these components and kits are easy to find. Here are some links for you:

Pre-built components (ready to wire directly to your GPIO pins:

Amazon (pack of 5 single-relay boards)

Amazon (2-relay board, connects to 2 GPIO pins)

And, just for fun, eBay (16 relay board!)

And one more… This is a latching variant (requires power to switch state but needs no power to stay at a particular state (requires 2 pins though, see its documentation):

Adafruit latching single relay board

Some simple kits with everything you need to build your own:

SparkFun BeefCake 2.0 (single relay board kit)

Evil Mad Scientist (single relay board, slightly different circuit)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.