Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT / Arduino

Practical Electronics For Makers (Part 5 of N)

5.00/5 (9 votes)
9 Apr 2019CPOL14 min read 11.8K  
Using transistors as switches

Introduction

This is the ongoing series covering basic electronic principles in an effort to work our way toward building usable devices using common components (and eventually various Arduino boards).

Background

You can read the previous chapters here on CodeProject:

Here's a list of items that you may need for this chapter:

Automation is Our Goal

The entire reason we are learning all of these things about electricity and electronics is so we can get to a point where we automate something.

Don’t Think Programming, Think Automation

Many people who work with computers and electronic devices think about programming a computer or device. That often leads to a tech-centric way of thinking because we become so enamored with the computer language and the technical details of programming. Suddenly, the focus shifts from the final product (the solution, the product, the thing the user wants) to the technical details of building the product.

For that reason, I like to think slightly differently and think about automating computers and devices.

With just that one simple shift in thinking, I believe you will stay more focused on your final goal. After all, everything we do is to automate some task.

Microsoft Office: Automation

Even the Microsoft Office applications are about automation.

MS Word is a way to automate the typing of documents that used to have to be done manually on a typewriter. You may wonder how a word processor is automation at all. Well, shift your thinking just a bit and you’ll see. Once you type a document into Word, you can print it numerous times without ever having to type it again. Of course on a typewriter, you had to type it each time.

Additionally, there are things you can do in Word that allow you to generate form letters with specific details such as addresses for each receiver. In the past, each of those letters would have to be typed individually.

Microsoft Excel is just the automation of the old time accounting ledger. In the past, if you had a number wrong at the top, you’d have to go back through and calculate all your resulting numbers again. Of course, now if you change the value in the cell at the top of the spreadsheet, all of your dependent values in the rest of the sheet are automatically updated. That’s automation.

Video Games are Automation

Even video games are automation. Video games automate NPCs (Non-Player Characters) so even if you are alone, you have an opponent to play against. In the past, you might have to roll dice and physically move the opponents on a board. Now, all of that is done for you by the computer. And, of course, the computer redraws the characters automatically, animating the characters to convince you that they are moving on the screen. In fact they are being drawn, erased and then drawn in another place on the screen. That’s automation.

IoT is Automation too

That’s what most of IoT is about too. For example, if we build a simple device that collects room temperatures on a time interval and writes them to an SD card, we are automating work that we could do with a thermometer and paper and pen. We could sit in a room and check the thermometer every five minutes and write it down in our notebook.

Automating Switches

The most basic and possibly most often automated thing in electronics is the switch. The ability to turn something on and off automatically or in response to some other event is one of the most common things you’ll need to do.

Imagine if you could press a button in an app on your phone and the light switch on your wall would flip on/off. That’s not directly possible because the phone app can’t manipulate physical (mechanical) things. However, as we’ll see, we can replace the physical switch with another type of device and actually do something similar.

I noticed that my garage door was wired up to a momentary switch and I added an electronic relay (magnet switch) into the system that I could control via bluetooth from my phone and made it so I can open my garage door with my phone.

Amazing things become possible once you learn a few more of the foundational things of what you can do.

We Need an Electronic Switch

But, what we need is a thing that acts like an electronic switch. Something that can be turned on or off with the presence or absence of electricity. That thing that I’m alluding to is one of the greatest inventions of all time and the thing that makes computers and automation possible. It is the transistor.

A Summary of What a Transistor Does

This isn’t exactly an explanation of how a transistor works. To really understand how a transistor works, you need to understand physics and chemistry and atomic structure and more. This is an explanation of how the transistor works as a component. It’s really all you need to know to build your circuits. But, of course, you can always go further if you choose.

Bipolar Junction Transistors (BJT)

The transistors we are going to talk about here are Bipolar Junction Transistors (BJT). That simply means there are two poles which are created by the two types of material that are used to build the transistor. The two types of material are N-type (negative type) and P-type (positive type). Yes, there's way more physics and science behind it, but that's all you really need to know for our purposes.

There are two types of BJT transistors:

  1. NPN
  2. PNP

Those designations are related to the types of material they are created from.

Basically, it is either a P-type of material between two outer layers of N-type of material.

BJTs have three pins or legs. Here are two block diagrams of how the pins are connected to the materials in each type (NPN, PNP).

Image 1

Or it is a N-type of material between two outer layers of P-type of material.

Image 2

This is important only in passing so you can understand that the different type of material affects the way electrons flow in the transistor.

BJT Transistors have 3 Pins

A transistor has three pins (legs). Each of those pins has a name:

  1. Base
  2. Collector
  3. Emitter

Here’s a picture of an NPN transistor. You can see that the front has a flat side (back side is curved) and contains some printing to indicate the transistor model.

Image 3

When you examine the datasheet, you will be able to see which pin is which in relation to the orientation of the flat face of the transistor. You'll need this information when you go to wire up your circuit.

It'll look something like the following:

Image 4

Here’s the symbol that represents the NPN transistor in a schematic.

Image 5

Here’s a picture of an PNP transistor. You can't tell which type of transistor you have (NPN or PNP) simply by looking at it, because they look identical.

Image 6

Here’s the symbol that represents the PNP in a schematic.

Image 7

Both of these types of transistors also come in another package which looks like a tin can. You may see these in some circuits.

Image 8

Schematic Details

When you first see the schematics, it may feel odd that the PNP shows the arrow pointing in on the emitter. It feels like the emitter would always be pointing out. But, again, this is related to electron flow so this is just something to memorize so you can determine by looking at a schematic if you need an NPN or PNP type for your circuit.

Not Pointing In = NPN

Also, a way to remember which schematic is which is to simply remember the one for NPN by memorizing a silly little phrase: NPN means Not Pointing iN. It's an easy way to remember that the schematic for the NPN has its arrow pointing out of the center of the transistor. Alternately, if it's PNP, then it always points in towards the center.

Focus on NPN

For now though, we’ll just focus on the NPN type in an effort not to confuse how the two different types are used.

And, finally, here is how a transistor acts like a switch in a circuit.

Explanation of How a Transistor Acts as a Switch

When a voltage is present on the base (pin), then current will flow between the collector and emitter (On). Of course, this means that when a voltage is absent from the base, then no current flows between the collector and emitter (Off). That is an electronic switch.

Let’s see this in action by building a very simple circuit.

BJT - Two Circuit Thinking

When we think about a circuit which uses a bipolar junction transistor (BJT), it is helpful to think of it as if it is two circuits.

Two Parts to the Circuit

We can think of it as the controlling circuit or input circuit which is a circuit that is made up of a voltage source, the base and the emitter. That part of the circuit will look something like the following:

Image 9

Here, we have a 700 millivolt (0.7V) source where the positive side of the battery is connected to the base and the emitter is connected to the negative (or ground) side of the battery. I’ve chosen 700mV because it is the maximum amount of voltage we can apply to the base pin (according to datasheet). In this case, I’ve also added a pushbutton switch (normally open) so that we can apply the 700 mV to the base when we press the button.

Of course, in real life, we don’t have easy access to a 700 mV power source (although we might with a test power supply). In most cases, as we have learned, we have 1.5V batteries that we have to conform for our use. However, if we apply more than 700mV to this circuit, we may damage the transistor.

For our circuit, we are going to power the two parts of the circuit from one battery so we are going to use a 5V power supply (4AA x 1.3V NiMH Nickel Metal Hydride = 5.2V).

However, since we cannot apply the full 5V to the base pin of the transistor, we will implement a voltage divider (two resistors in parallel) to drop the voltage that is applied to the base pin.

Our controlling circuit will look like the following:

Image 10

You can see that we still have our complete circuit from positive battery terminal through base pin and out the emitter into ground. But we have also added two resistors. R1 (1000 Ohms) seems normal as it is in series with the rest of the circuit. R2 (10,000 Ohms) may look odd though because it is connected directly from the positive side to ground. The intersection where the two resistors are connected creates our voltage divider. Most of the 5V is dropped over the larger resistor.

Calculating Voltage Drop Values

To calculate the Voltage of R1, we use the following formula:

VOUT= VIN * (R1 / (R1 + R2))

In our case, calculating for R1:

VOUT = 5 * (1,000 / (10,000 + 1,000)
	VOUT = 5 * (1,000 / 11,000)
	0.45V (450mV) = 5 * 0.090

Of course, we know that the total voltage drop over the entire circuit has to be the total applied (5V) so we can subtract 0.45V and get the amount of voltage that is dropped over the 10K resistor: 4.55V.

However, we can also check our math by calculating the voltage drop over the 10K resistor using our original formula.

VOUT = VIN * (R2 / (R1 + R2))
VOUT = 5 * (10,000 / 11,000)
4.5V = 5 * 0.900

It includes a bit of rounding error, but we can see that 4.5V + 0.45 V = 4.95V.

Controlling Circuit

This is our controlling circuit. That means when the button is pressed, a voltage will be applied to the base pin and that will turn on the output circuit.

Output Circuit

Our output circuit will illuminate an LED.

Here’s the complete output circuit without the controlling circuit added yet.

You can see that this is a simple circuit to light up an LED.

Image 11

We calculate the amount of current we want through the LED just as we did earlier.

5V - 1.7V (LED drop) = 3.3V
3.3V / 30mA = 110 Ohms

We round down and grab a 100 Ohm resistor and place it in the circuit.

You can see that we build the circuit as a complete loop using the collector through the emitter.

That simply means we connect our 100 Ohm resistor to the collector pin of the transistor on the positive side of the power source. We connect a wire back from the emitter to ground to give us a complete circuit through the transistor.

LED Does Not Illuminate

If we were to wire up that previous diagram as shown the LED would not light up. That’s because the transistor acts as a switch and since there is no voltage on the base pin, the transistor does not allow current to flow from the collector to emitter and the circuit is off.

Add In Our Controlling Circuit

Now we need to add in our controlling circuit so we can see the entire thing work.

Image 12

Now, when we press the pushbutton (SW1), a voltage will be applied to the base pin which will allow the current to flow between the collector and emitter pins and the LED will light up.

Where’s the Automation?

If you’re wondering how this is any different from our earlier circuits which used manual switches, you are correct to do so. We are still pushing a button. However, the pushbutton in this circuit was only used so we could do a simple first experiment with our new transistor component.

Replace Power Supply and Pushbutton With Microcontroller

Now, imagine if we replace the 5V power supply and the switch with a microcontroller pin. In other words, imagine if we allow a microcontroller pin to supply the 5V when some event occurs.

We might build a circuit like the following (keep in mind this is just a mock-up):

Image 13

In this new circuit, the Arduino will monitor the temperature (reading it from the temperature component connected to P5).

If the temperature reaches 78 degrees or higher, it will turn on pin 10 (arbitrarily chosen) of the Arduino. That will set the pin to high (which is 5V on the Arduino). This, in turn, will put a voltage on the base pin of the transistor and activate the secondary circuit which will turn on the LED, just as it does when we press the button in our previous circuit.

Automation Success

This new circuit is completely automated. It is now controlled by the Arduino reading the room temperature from the temperature component. This is the basics of how all automation circuits work so now you understand the secrets of how to do just about everything.

You can now trigger (automate) just about any activity with this simple method.

Crude Example to Get You Thinking

There are a number of details such as how to write code for the Arduino, how to insure the LED stays lit for some minimum amount of time if the temperature fluctuates and various other things. Of course, this is a bit contrived because if you already have the Arduino available, then you don’t necessarily need the output circuit and the extra transistor to do that work either. Again, this is a crude example to get you thinking about how transistors act like a switch and how we can activate them with automation.

Next, Using Transistors to Create Flashing LEDs

Now that you understand how a transistor acts like a switch and can be automated, let’s build a circuit which will flash two LEDs on and off with nothing more than a few simple components (2 transistors, 2 capacitors, 4 resistors and 2 LEDs).

We’ll also learn a little bit about how capacitors work in a DC circuit.

We'll do that next time.

History

  • 9th April, 2019: First publication

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)