Chapter 5. Basic Electronics

In “Experiment: Controlling an LED”, you were launched right into controlling a motor using a transistor, without any real explanation as to how this worked. If you already know a little about electronics, you might well be familiar with transistors and be able to skip much of this chapter. If, on the other hand, you are new to electronics, you will find this explanation helpful.

Current, Voltage, and Resistance

In this section, you will need to refer to the schematic diagram for “Experiment: Controlling a Motor” that you built on the breadboard. This is shown in Figure 5-1.

The schematic view is just a different and more abstract way of representing what you made on the breadboard. Instead of diagramming how components look in real life, they are represented in a way that indicates what they do.

Current

The zig-zag line of resistor R1 gives a visual clue that this component will restrict the flow of current. In electronics, the word current refers to a flow of electrons through wires or components. You can think of it as electrons flowing from one place on your circuit to another. As an example, current is flowing out of a GPIO output on an Arduino or Raspberry Pi and into resistor R1. The current flows out of R1 and into the center connection (the base) of transistor Q1. 

Figure 5-1. The schematic diagram for controlling a motor

A small current flowing into the base of a transistor allows a much bigger current to flow through the two righthand connections of the transistor, the collector (top) and emitter (bottom). That is how the tiny current from the GPIO pin can control the much bigger current needed for something like a motor. It can be helpful to think of it as a digital switch that can be turned on and off with a small current.

The unit of measurement used for current is the ampere, or more commonly amp, which is further abbreviated to just A. A current of 1A is actually a relatively high current when dealing with an Arduino or Raspberry Pi, so the unit of milliamp (mA) is often used. 1mA is 1/1000 of an amp.

The reason we use the resistor R1 to restrict the flow of current is that the GPIO pins of a Raspberry Pi or Arduino cannot provide enough current to drive a motor directly. In fact, if you try to do that, there is a good chance you will either damage or destroy your Pi or Arduino. A Raspberry Pi can safely deliver about 16mA of current and an Arduino about 40mA. 

Voltage

In the same way that water always flows from higher places to lower places, electrical current always flows from parts of the circuit that are at a higher voltage to places that are at a lower voltage. So, in the case of the control GPIO pin in Figure 5-1, when the pin is at 0V, no current will flow out of it and through the resistor and then transistor, to ground, because the GPIO pin and ground are at the same voltage (0V). However, when the pin is high, at 3.3V on a Raspberry Pi or 5V on an Arduino, the current will flow through the resistor and transistor and then to ground.

An important point about schematics and voltage is that all points on a schematic that are connected by a line are the same voltage.

The unit of measurement for voltage is the volt, abbreviated to V. A Raspberry Pi’s GPIO pins used as an output are either 3.3V (high) or 0V (low) and those of an Arduino are 5V or 0V.

Ground

The line at the bottom of Figure 5-1 is labeled as ground. You will often see this on schematic diagrams labeled as GND. Ground represents zero volts in a schematic and is the base voltage against which other voltages in the circuit are measured. For example, the top positive terminal of the battery in Figure 5-1 will be referred to as being at 6V because it is 6V higher than ground.

When it comes to connecting different parts of a project together, the grounds from each part will all be connected together. In this case, if you were going to attach this motor control module to an Arduino or Raspberry Pi, then the ground of this motor controller would be connected to one of the ground pins (GND) of the Arduino or Raspberry Pi.

Resistance

Resistors have a value of resistance that is measured in ohms, which is abbreviated to the Greek letter omega (Ω). Resistors span quite a large range of values, so you will find resistor values in the kΩ range (thousands of Ω) and sometimes in the MΩ range (millions of Ω).

The resistor used in “Experiment: Controlling a Motor” is a 1kΩ resistor, and you can work out how much this 1kΩ resistor will limit the current using something called Ohm’s Law. The law states that the current flowing through a resistor will be the difference in voltage across the resistor (in volts) divided by the value of the resistor in ohms. In the case of a Raspberry Pi, the biggest voltage drop that there could possibly be between the GPIO pin and GND is when the GPIO pin is high at 3.3V. So the maximum current that could possibly flow is 3.3V / 1000Ω = 3.3mA.

The resistor is protecting the GPIO pin to limit the current that can be drawn from it, and if every digital output from a Raspberry Pi always starts with a 1kΩ resistor, we can rest assured that our Pi will be safe. Quite often, especially when using LEDs, you will be able to use a lower value of resistor because something else (such as the LED) will be using some of the voltage drop, reducing the current.

Power

When a current passes through a resistor, there is a heating effect. The electrical energy is converted into heat energy and power is the amount of this energy that is converted per second. The unit of power is the watt (W), and the heat that a component generates is calculated by multiplying the voltage across it (in volts) by the current passing through it in amps.

Returning to our 1kΩ resistor from experiment 1, if it has 2.2V across it and a current flowing through it of 2.2mA, so it will produce about 4.8mW of power. That’s very little power. However, transistors will also generate heat of the voltage across them multiplied by the current flowing through them. If you used a fairly powerful motor, say 800mA for “Experiment: Controlling a Motor”, the voltage drop from the collector to the emitter of a fully on transistor will be about 1.2V. So the power converted to heat will be 800mA x 1.2V = 960mW. That will make the transistor get quite hot. If the transistor gets too hot, something inside will eventually melt and the transistor will fail. For this reason, just as the Raspberry Pi and Arduino output pins have a maximum current, so do transistors. Physically larger transistors will usually be rated for larger currents, which is one of the factors that you need to consider when selecting a transistor to control an actuator.

The MPSA14 Darlington transistor used in “Experiment: Controlling a Motor” has a maximum current of 1A.

Common Components

In the following subsections, we will look at some of the components used in this book and explain how to use them and also how to select them.

Resistors

Resistors are colorful little devices. If you are trying to work out their value, you can just measure their resistance with a multimeter, or you can read their value from the colored stripes. 

Each color has a number associated with it, as shown in Table 5-1.

Table 5-1. Resistor color codes

Black

0

Brown

1

Red

2

Orange

3

Yellow

4

Green

5

Blue

6

Violet

7

Gray

8

White

9

Gold

1/10

Silver

1/100

Gold and silver, as well as representing the fractions 1/10 and 1/100, are also used to indicate how accurate the resistor is, so gold is +–5% and silver is +–10%.

There will generally be three of these bands together starting at one end of the resistor, a gap, and then a single band at the other end of the resistor. The single band indicates the accuracy of the resistor value.

Figure 5-2 shows the arrangement of the colored bands. The resistor value uses just the three bands. The first band is the first digit, the second the second digit, and the third “multiplier” band is how many zeros to put after the first two digits.

Figure 5-2. Reading resistor color codes

So a 270Ω resistor has a first digit 2 (red), second digit 7 (violet), and a multiplier of 1 (brown). Similarly, at 1kΩ resistor will have bands of brown, black, and red (1, 0, 00).

Resistors also have a power rating. Through-hole resistors of the type used in this book are nearly all 1/4W. Other common power ratings are 1/2W, 1W, and 2W, with the resistors becoming increasingly large physically with the power rating.

Transistors

The range of transistors on offer from a component supplier is generally huge and bewildering, so in this book, I have simplified the choice to four transistors that will cover most of the bases, when it comes to switching things on and off.

The transistor in “Experiment: Controlling a Motor” is what allows the tiny current of a few milliamps to control the hundreds of milliamps required by the motor. Although transistors can be used in other roles, in this book we will be using them as switches. A small current flowing into the base and down to GND through the emitter of the transistor will switch a much larger current flowing from the collector to the emitter.

Figure 5-3 shows a selection of transistors of various types and power handling capabilities.

Figure 5-3. A selection of transistors

Transistors are supplied in a fairly small number of standard package types. So when identifying a transistor, you cannot go by appearance; you need to read the name that is printed on it.

The most common packages are TO-92 on the left and TO-220 in the center. Occasionally, for very high-power applications, you may use a transistor with a larger case style, such as the TO-247 package on the right.

The TO-220 and TO-247 packages are both designed to be bolted to heat sinks. It is not necessary to fix these transistors to a heat sink if you are using them at much lower currents than their specified maximum current.

Bipolar transistors

Transistors are made using different technologies that have pros and cons that make them suitable for some situations and unsuitable for others.

The bipolar transistor is where most people start with transistors. These have not changed much since the early days of transistors. They have the advantage that they are very cheap and easy to use for smallish load currents. Their downside is that although a small current passing through the base to emitter of the transistor will result in a bigger current flowing through from the collector to the emitter, the collector current is limited to a multiple of the base current and that multiple (called gain or hFE) is typically  between 50 and 200.  So, if a Raspberry Pi is only supplying 2mA to the base, the  current flowing through the collector may only be 100mA. This may be a lot less than you were expecting, as the transistor may have a much higher current-carrying capability (say, 500mA) but never get to that limit because the base current is not enough. This is not normally a problem when using an Arduino, as it can supply more current to the base (up to 40mA) by using a lower value resistor in place of the 1kΩ resistor in Figure 5-1. If you selected a resistor value of 150Ω, for example, the base current would increase to I = V / R = (5 – 0.5) / 150 = 30mA. A base current of 30mA would even in the worst case of a transistor with a gain of 50 still result in a collector current of 1.5A.

The voltage calculation in the preceding equation is (5 – 0.5) because the voltage between the base and emitter of a bipolar transistor will be around 0.5V when the transistor is turned on.

In this book, we use just one model of bipolar resistor, the very common 2N3904. Although higher current bipolar transistors are available, there are better transistor technologies to use as the current starts to get higher.

Darlington transistors

When you need a bit more gain, if perhaps you are driving a small motor from a Raspberry Pi that is only capable of supplying a few milliamps into the base, then a Darlington transistor is a good alternative to a regular bipolar transistor and will typically have a gain of at least 10,000. 

Darlington transistors are actually made of two bipolar transistors within one transistor package (Figure 5-4). This two-stage arrangement is what gives the Darlington its high gain.

Because there are now two base-emitter junctions, each will drop at least 0.5V when the transistor is on, and so there will be a voltage drop of around 1V rather than the 0.5V of a normal bipolar transistor. In fact, this drop is also applied to the collector voltage and increases with the load current. This means that when controlling a current of 1A, an MPSA14 Darlington transistor may only actually provide 9V when powering a 12V load. Sometimes this matters, but other times it does not.

The transistor used in “Experiment: Controlling a Motor” (an MPSA14) is a Darlington transistor. The voltage drop across R1 when using a Raspberry Pi will not actually be 3.3V, but rather 3.3V–1V or 2.2V. So, the current that the Raspberry Pi needs to supply will be 2.2V / 1kΩ = 2.2mA.

As well as the low-power MPSA14, which is good for controlling loads up to 0.5A, I also recommend the higher-power TIP120 Darlington transistor as a standard transistor to keep in your component box.

Figure 5-4. A Darlington transistor

MOSFETs

Bipolar transistors are essentially current-driven devices—the small base current is amplified into a bigger collector current. There is another type of transistor called the metal-oxide-semiconductor field-effect transistor (MOSFET) that requires very little current to switch, but instead will turn on as long as the voltage at its “gate” connection is greater than a certain threshold.

Figure 5-5 shows the schematic diagram for the transistor; you can see that the symbol implies that the gate is not directly connected to the rest of the transistor.

Figure 5-5. The symbol for a MOSFET

Note that instead of a base, collector, and emitter, a MOSFET has a gate, drain, and source. Intuitively, you might expect the source and collector to be equivalents, but actually the drain is equivalent to the collector of a bipolar transistor. 

Switching using a MOSFET actually makes a lot of sense if you are using an Arduino or Raspberry Pi, because almost no current is needed; you just need to make sure that the voltage at the gate is greater than the gate threshold voltage for the transistor. The gate threshold voltage is the voltage at which the MOSFET turns on, allowing a current to flow from drain to source. Figure 5-6 shows how to connect up a MOSFET to control a load. This is really just the same as for a bipolar transistor.

Figure 5-6. Using a MOSFET

The schematic diagram shown in Figure 5-6 uses two symbols we have not seen before. Connected to the source of the transistor (S) is a row of three parallel lines of reducing length. This is the symbol for ground, and using it in schematics reduces the number of connecting lines that you need to draw on the diagram.

The other symbol is at the top of the diagram and is just a horizontal line marking 6V. This indicates that there will be 6V at that point in the circuit, saving us the trouble of drawing a battery.

Transistor Pinouts Vary

Although this book tries to stick to transistors with compatible pinouts to each other, this is not universal. Not all transistors in a certain package have the same pinout; check on the datasheet before you use a new transistor.

Pinouts for a lot of the devices used in this book are in Appendix A.

Looking at Figure 5-6, you may be wondering why you still need R1, as the gate does not draw any current. The reason it’s a good idea to still include the resistor is that when you first raise the gate voltage, there will be a very quick inrush of current for a fraction of a second. The resistor makes sure that this current does not exceed the capabilities of the GPIO pin. 

The catch with MOSFETs is that the threshold voltage is sometimes too high to be switched with the 3.3V or 5V of a Raspberry Pi or Arduino. MOSFETs that have a low enough gate threshold to be used directly from a GPIO port are called logic-level MOSFETs.  This book standardizes on two MOSFETs: for lower power, use the 2N7000; and for higher power applications, use the FQP30N06L. Both have a gate threshold voltage guaranteed to be below 3V, making them suitable for use with both an Arduino and Raspberry Pi.

In general, MOSFETs run a lot cooler when switching loads than bipolar devices. One of the main properties to look for when buying a MOSFET is its on resistance. A MOSFET with a very low on resistance will switch high currents without even getting warm. As you might expect, the cost of a MOSFET increases the lower its on resistance.

In this book, you will find MOSFETs being used quite a lot. Mostly I stick to the FQP30N06L (up to 30A), although for this kind of current you will need a big heat sink.

In fact, the emitter, base, and collector pins of the TIP120 Darlington and the source, gate, and drain of an FQP30N06L MOSFET are in the same positions, so you could just unplug a TIP120 from a breadboard and plug in an FQP30N06L using the same configuration and the circuit should still work.

PNP and P-channel transistors

Transistors of all the types just described actually come in two flavors. So far we have only considered one flavor: negative positive negative (NPN) or N-channel in the case of MOSFETs. These are the most common type and usually that is all you will ever need.

The other flavor of transistors are PNP or P-channel devices. Where N-type devices are used to switch the load to ground, P-type devices switch it to the positive supply. P-channel MOSFETs are used in H-bridge motor drivers in Chapter 8.

Transistor selection guide

Selecting the right transistor for a job can be tricky. Table 5-2 standardizes the choice into just five devices.

When used with a Raspberry Pi, it is assumed that a 1kΩ resistor is between the GPIO pin and the base or the gate of the transistor. For an Arduino, that resistor is assumed to be 150Ω. The current figures are derived by testing on actual devices, and the maximum voltage figures are taken from the devices’ datasheets.

Table 5-2. A useful set of transistors
Transistor Type Package

Max. current

(Pi 3.3V)

Max. current

(Arduino 5V)

Max. volts
2N3904 Bipolar TO-92 100mA 200mA 40V
2N7000 MOSFET TO-92 200mA 200mA 60V
MPSA14 Darlington TO-92 1A 1A 30V
TIP120 Darlington TO-220 5A 5A 60V
FQP30N06L MOSFET TO-220 30A 30A 60V

When shopping for an FQP30N06L, make sure the MOSFET is the “L” (for logic) version, with “L” on the end of the part name; otherwise, the gate threshold voltage may be too high.

The MPSA14 is actually a pretty universally useful device for currents up to 1A, although at that current there is a voltage drop of nearly 3V and the device gets up to a temperature of 120°C! At 500mA, the voltage drop is a more reasonable 1.8V and the temperature 60°C.

To summarize, if you only need to switch around 100mA then a 2N3904 will work just fine. If you need up to 1A, use an MPSA14. Above that, the FQP30N06L is probably the best choice, unless price is a consideration, because the TIP120 is considerably cheaper.

Diodes

The diode in experiment 1 (see Figure 5-1) is included to protect the Raspberry Pi or Arduino and the transistor.

Motors create voltage spikes and all sorts of electrical noise that can wreak havoc with delicate electronics like the Raspberry Pi or Arduino. A diode is used to make sure that these electrical spikes caused by the motor cannot momentarily reverse the flow of current, something that can easily destroy the transistor. It does this because the diode only allows current to pass in one direction. It is a kind of one-way valve. Current can only flow in the direction indicated by its arrow-like shape.

It is quite common to attach a diode across the terminals of a motor in this way. The current will usually flow through the motor in the opposite direction to the direction allowed by the diode, but should there be a negative spike in voltage, the diode will come into play, conduct and effectively short out the brief flow of current, nullifying it.

LEDs

You will learn much more about LEDs in Chapter 6. The term LED is an acronym for Light Emitting Diode.

As you might guess, an LED works like a plain diode, except that when current is flowing through it, it emits light. The symbol for an LED is the same as for a normal diode, but with arrows on it to indicate the emission of light (Figure 5-7).

Figure 5-7. The symbol for an LED

LEDs are available in a wide range of colors and sizes. They can be driven directly from an Arduino or Raspberry Pi GPIO pin, but you must use a resistor to limit the current, as you would when driving a transistor. You will discover how to do this in Chapter 6.

Capacitors

Capacitors can be thought of as places where you can temporarily store electricity—akin to very low capacity batteries that can keep a small reserve of charge. They will be used in this book in various roles, from helping to suppress electrical interference to keeping a small reserve of extra electrical energy so that when there are sudden peaks in demand they can be drawn from a capacitor.

Figure 5-8 shows the symbols for a capacitor, which, if it has a high value of capacitance, will usually be polarized. Low-value capacitors do not have a positive and negative side.

Figure 5-8. The symbols for a capacitor: unpolarized (A) and polarized (B)

You may also come across slightly different symbols for capacitors that are recognizable but use a hollow box as the positive end of a polarized capacitor and a solid box as the negative end. This book uses the US convention for capacitor symbols shown in Figure 5-8.

Integrated Circuits

Integrated circuits (ICs), or chips are they are often known, are made up of many transistors built onto a single slice of silicon and encapsulated into a single package. The Raspberry Pi and Arduino are each made of many ICs and some other components attached to a  printed circuit board (PCB).

There are special-purpose ICs designed for pretty much any kind of electronic use that you could want. Of particular relevance for this book are ICs that help you to control things. These kind of ICs will often combine high-current transistors and control logic into a single package.

The Ins and Outs of Connections

Having covered the basics of electronics, we should also look at how those electronics interface with a Raspberry Pi or Arduino. You will find much more specific and detailed explanations of this, especially the programming side of things in Chapters 2 and 3.

Digital Outputs

As you saw in Chapter 4, digital outputs are used to turn things on and off. If you are using a Raspberry Pi, a digital output will either be at 0V low or 3.3V high. For an Arduino, the high voltage is 5V rather than 3.3V, but the principle is the same; they cannot be at any voltage in between high and low.

Another difference between the Arduino and the Raspberry Pi is that the Arduino can supply more current (40mA rather than 16mA). 

This book is littered with examples that use digital outputs—it is the main mechanism used to control things.

Digital Inputs

Digital inputs are often connected to switches or digital outputs from other devices. A digital input will have a threshold voltage, usually in the middle of the high/low voltage range, so for an Arduino that threshold will be around 2.5V and for a Raspberry Pi it will be around 1.65V. When the program running on the Arduino or Raspberry Pi reads a digital input, if it is above the threshold, it is considered to be high; otherwise, the input is low. 

Just like digital outputs, there are no half-measures with digital inputs—they are either high or low. 

You can find information on using digital inputs on the Arduino in “Digital Inputs” in Chapter 2 and on the Raspberry Pi in “Digital Inputs” in Chapter 3.

Analog Inputs

Analog inputs allow you to measure a voltage on an analog pin that lies between the low and high voltages. The Raspberry Pi does not have any analog inputs, but the Arduino has six, labeled A0 to A5.

In an Arduino, the voltage between 0 and 5V is mapped to a number between 0 and 1023. So 0V gives a reading of 0, and 5V a reading of 1023. Something in the middle (2.5V) will give a reading of around 511.

You can find out more about analog inputs on the Arduino in “Analog Inputs” in Chapter 2.

Analog Outputs

Although you might imagine analog outputs would allow you to set an output pin to any voltage between low and high, they are a bit more complicated than that. They use a technique called pulse-width modulation (PWM) to control the average power arriving at a normal digital output. 

PWM is used to control the speed of motors and the brightness of LEDs. “Controlling Speed (PWM)” explains how PWM works, and can be used to control the speed of a motor.

Serial Communication

The interface techniques just described are the basic, low-level techniques. Some devices that you will want to control from an Arduino or Raspberry Pi will use serial interfaces that pass binary data one bit at a time from one device’s digital output to another device’s digital input.

For example, in Chapter 14, you will find displays that require their data in serial form.

There are various standards of serial interface that all do a very similar job but in slightly different ways. These include what is known as simply serial or TTL serial, I2C (pronounced i squared c), and  serial peripheral interface (SPI).  

Summary

In this chapter you have learned all about the fundamental electronic concepts of current voltage and resistance, and looked at some electronic components that are used in this book. But that’s enough theory for now! In the next chapter, you will learn how to use various types of LED with your Arduino and Raspberry Pi.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset