Basics of Electronics
-
Here is the information that will be useful to you.
Understanding these quantities will be aided by the electrical connection diagram from the "Experiment: Motor Control" section that we assembled on the breadboard.
Such a schematic representation is just another, more abstract, way to show what we have on the breadboard. In the diagram, components are depicted not as they appear in reality but to illustrate how they work.
Current
The jagged line representing resistor R1 indicates that this component restricts current. In electronics, current refers to the flow of electrons through wires or circuit elements. You can think of electrons moving from one part of your circuit to another. For example, current may flow from the GPIO output of an Arduino or Raspberry Pi to resistor R1. After passing through resistor R1, it reaches the central connection (base) of transistor Q1.
The small current going through the transistor's base allows us to control a much stronger current flowing through two transistor connections shown on the right: the collector (top) and the emitter (bottom). It's through this effect that a weak current from a GPIO pin enables control of a strong current, such as the one powering a motor. This construction can be conveniently compared to a digital switch that you can turn on and off using a weak current.
The unit of measurement for current is called an ampere (abbreviated as A). Since a current of 1 A is too powerful to work with directly on an Arduino or Raspberry Pi, the unit of milliampere (mA) is often used for convenience. In this case, 1 mA = 1/1000 A.
Resistor R1, limiting the current strength, is included in our circuit because the GPIO pins of Arduino or Raspberry Pi are not powerful enough to control a motor directly. If you were to attempt this, you would likely damage or completely disable your Raspberry Pi or Arduino. As mentioned earlier, Raspberry Pi can handle a current of no more than 16 mA, while Arduino can handle around 40 mA.
Voltage
Water always flows from top to bottom. Similarly, in an electrical circuit, current always flows from components at higher voltage to components at lower voltage. If the GPIO pin, from which current should flow to our circuit along the Control line, is at 0 V, no current will flow from it to the resistor, transistor, and subsequently to ground because the voltage at both the GPIO pin and the ground is the same - 0 V.
However, when the GPIO pin is in a high state (high), meaning it is supplied with 3.3 V (Raspberry Pi) or 5 V (Arduino), current will flow from it to the resistor and further through the transistor to the ground.
It's important to note that at all points connected by a line on the diagram, the voltage is the same.
Voltage is measured in volts (abbreviated as V). As mentioned earlier, the GPIO pins of Raspberry Pi operate at 3.3 V (high) or 0 V (low), and on Arduino, it's 5 V and 0 V, respectively.
Ground
The line labeled "Ground" at the bottom of the diagram represents the ground connection. In such schematics, it is often denoted by the abbreviation GND (from the English word "ground"). The voltage at the ground is always 0 V and serves as the baseline from which all other voltage readings on the circuit are measured. For example, the top positive terminal of the battery is labeled as 6 V because its voltage is 6 V higher than the voltage at the Ground connection.
When connecting different components of a project together, all the grounds of these components must be interconnected. In our case, when we plan to connect the motor control module to an Arduino or Raspberry Pi board, its ground line will be connected to one of the ground pins (GND) of that board.
Resistance
Each resistor has its nominal resistance, which is measured in ohms (Ω). On diagrams, ohms are sometimes represented by the Greek letter omega (Ω). The range of resistance values for resistors can vary greatly; some have resistances in the kilohms (thousands of ohms), while others have resistances in the megohms (millions of ohms).
The resistor included in the circuit has a value of 1 kΩ. To determine how much the current can be limited by such a resistor, we can use Ohm's law. This law states that the current passing through a resistor is equal to the voltage difference across the resistor (in volts) divided by the resistance of the resistor (in ohms). In the case of Raspberry Pi, the maximum voltage difference between a GPIO pin and ground occurs when the GPIO pin is in a high state, which means its voltage value is 3.3 V. Therefore, the maximum possible current in this case is:
3.3 V / 1000 Ω = 3.3 mA.
CURRENT LIMITATIONS FOR RASPBERRY PI GPIO PINS
Until now, there is no consensus on the maximum current that a Raspberry Pi GPIO pin can handle. Typically, I rely on the value officially stated by the Raspberry Pi manufacturer (i.e., 3 mA per GPIO pin). The 3 mA value emerged because the first model of the Raspberry Pi had only 14 GPIO pins, and its three-volt voltage regulator allowed for a total current of 50 mA to be supplied to GPIO pins, resulting in 3 mA per GPIO pin when all such pins on the board were engaged.
These figures are no longer relevant for the new models of Raspberry Pi (A+, B+, and Pi 2), which have 24 GPIO pins available for use and are equipped with a three-volt voltage regulator theoretically capable of providing up to 1 A. However, even if you are working with one of these new Raspberry Pi models, do not attempt to supply 1 A / 24 = 41 mA per GPIO pin, as the Broadcom single-chip system on a chip (SoC) used in Raspberry Pi also limits the maximum current for one GPIO pin to 16 mA.
Theoretically, on Raspberry Pi A+, B+, or Pi 2, you can safely supply up to 16 mA to any number of GPIO pins. However, there are other factors that determine how much current you can supply to the Pi without risking damage, such as modulation frequency and the total current across all GPIO pins that the Broadcom chip can withstand.
So, it's advisable to adhere to the following recommendations:
The resistor R1 shown in the diagram protects the GPIO pin by limiting the current that can be drawn from this pin. If every digital output from a Raspberry Pi is always protected by a 1 kΩ resistor, there is no need to worry about our Pi. Additionally, quite often, especially when working with LEDs, you may be able to use a resistor with a lower value since some components (like LEDs) will experience a partial voltage drop, which will reduce the current.
Power
When current flows through a resistor, it heats up. Electrical energy is converted into thermal energy, and this transformation is measured in power - the amount of energy converted per second. The unit of power is the watt (W), and to calculate the power dissipated by a component as it heats up due to the current passing through it, you need to multiply the voltage across it (in volts) by the current passing through it (in amperes).
Let's go back to the resistor shown in the diagram, which has a resistance of 1 kΩ. If it is subjected to a voltage of 2.2 V and a current of 2.2 mA flows through it, it will dissipate approximately 4.8 mW of power. This is a very small amount of power. However, transistors also generate heat, determined by multiplying the current passing through them by the voltage. If you were working with a fairly powerful motor consuming, say, 800 mA of current, then the voltage drop across the transistor as a whole would be about 1.2 V. In this case, the power converted into heat would be:
800 mA x 1.2 V = 960 mW.
In this case, the transistor would heat up significantly. If a transistor overheats, something inside it could melt, causing it to fail. Therefore, the limitation of the maximum allowable current exists not only for Arduino or Raspberry Pi but also for the transistor. Relatively large transistors are typically designed to handle a stronger current than smaller ones, and this factor must be taken into account when selecting a transistor for controlling the actuator.
The composite transistor (Darlington pair) MPSA14 used in our experiment on motor control (see "Experiment: Motor Control" section) can handle a current of up to 1 A.
Such a schematic representation is just another, more abstract, way to show what we have on the breadboard. In the diagram, components are depicted not as they appear in reality but to illustrate how they work.
Current
The jagged line representing resistor R1 indicates that this component restricts current. In electronics, current refers to the flow of electrons through wires or circuit elements. You can think of electrons moving from one part of your circuit to another. For example, current may flow from the GPIO output of an Arduino or Raspberry Pi to resistor R1. After passing through resistor R1, it reaches the central connection (base) of transistor Q1.
The small current going through the transistor's base allows us to control a much stronger current flowing through two transistor connections shown on the right: the collector (top) and the emitter (bottom). It's through this effect that a weak current from a GPIO pin enables control of a strong current, such as the one powering a motor. This construction can be conveniently compared to a digital switch that you can turn on and off using a weak current.
The unit of measurement for current is called an ampere (abbreviated as A). Since a current of 1 A is too powerful to work with directly on an Arduino or Raspberry Pi, the unit of milliampere (mA) is often used for convenience. In this case, 1 mA = 1/1000 A.
Resistor R1, limiting the current strength, is included in our circuit because the GPIO pins of Arduino or Raspberry Pi are not powerful enough to control a motor directly. If you were to attempt this, you would likely damage or completely disable your Raspberry Pi or Arduino. As mentioned earlier, Raspberry Pi can handle a current of no more than 16 mA, while Arduino can handle around 40 mA.
Voltage
Water always flows from top to bottom. Similarly, in an electrical circuit, current always flows from components at higher voltage to components at lower voltage. If the GPIO pin, from which current should flow to our circuit along the Control line, is at 0 V, no current will flow from it to the resistor, transistor, and subsequently to ground because the voltage at both the GPIO pin and the ground is the same - 0 V.
However, when the GPIO pin is in a high state (high), meaning it is supplied with 3.3 V (Raspberry Pi) or 5 V (Arduino), current will flow from it to the resistor and further through the transistor to the ground.
It's important to note that at all points connected by a line on the diagram, the voltage is the same.
Voltage is measured in volts (abbreviated as V). As mentioned earlier, the GPIO pins of Raspberry Pi operate at 3.3 V (high) or 0 V (low), and on Arduino, it's 5 V and 0 V, respectively.
Ground
The line labeled "Ground" at the bottom of the diagram represents the ground connection. In such schematics, it is often denoted by the abbreviation GND (from the English word "ground"). The voltage at the ground is always 0 V and serves as the baseline from which all other voltage readings on the circuit are measured. For example, the top positive terminal of the battery is labeled as 6 V because its voltage is 6 V higher than the voltage at the Ground connection.
When connecting different components of a project together, all the grounds of these components must be interconnected. In our case, when we plan to connect the motor control module to an Arduino or Raspberry Pi board, its ground line will be connected to one of the ground pins (GND) of that board.
Resistance
Each resistor has its nominal resistance, which is measured in ohms (Ω). On diagrams, ohms are sometimes represented by the Greek letter omega (Ω). The range of resistance values for resistors can vary greatly; some have resistances in the kilohms (thousands of ohms), while others have resistances in the megohms (millions of ohms).
The resistor included in the circuit has a value of 1 kΩ. To determine how much the current can be limited by such a resistor, we can use Ohm's law. This law states that the current passing through a resistor is equal to the voltage difference across the resistor (in volts) divided by the resistance of the resistor (in ohms). In the case of Raspberry Pi, the maximum voltage difference between a GPIO pin and ground occurs when the GPIO pin is in a high state, which means its voltage value is 3.3 V. Therefore, the maximum possible current in this case is:
3.3 V / 1000 Ω = 3.3 mA.
CURRENT LIMITATIONS FOR RASPBERRY PI GPIO PINS
Until now, there is no consensus on the maximum current that a Raspberry Pi GPIO pin can handle. Typically, I rely on the value officially stated by the Raspberry Pi manufacturer (i.e., 3 mA per GPIO pin). The 3 mA value emerged because the first model of the Raspberry Pi had only 14 GPIO pins, and its three-volt voltage regulator allowed for a total current of 50 mA to be supplied to GPIO pins, resulting in 3 mA per GPIO pin when all such pins on the board were engaged.
These figures are no longer relevant for the new models of Raspberry Pi (A+, B+, and Pi 2), which have 24 GPIO pins available for use and are equipped with a three-volt voltage regulator theoretically capable of providing up to 1 A. However, even if you are working with one of these new Raspberry Pi models, do not attempt to supply 1 A / 24 = 41 mA per GPIO pin, as the Broadcom single-chip system on a chip (SoC) used in Raspberry Pi also limits the maximum current for one GPIO pin to 16 mA.
Theoretically, on Raspberry Pi A+, B+, or Pi 2, you can safely supply up to 16 mA to any number of GPIO pins. However, there are other factors that determine how much current you can supply to the Pi without risking damage, such as modulation frequency and the total current across all GPIO pins that the Broadcom chip can withstand.
So, it's advisable to adhere to the following recommendations:
- On a basic Raspberry Pi, you can use 16 mA for each GPIO pin with a total current of 40 mA across all GPIO pins;
- On Raspberry Pi A+, B+, or Pi 2, you can safely supply no more than 16 mA per GPIO pin and no more than 100 mA for all engaged GPIO pins.
The resistor R1 shown in the diagram protects the GPIO pin by limiting the current that can be drawn from this pin. If every digital output from a Raspberry Pi is always protected by a 1 kΩ resistor, there is no need to worry about our Pi. Additionally, quite often, especially when working with LEDs, you may be able to use a resistor with a lower value since some components (like LEDs) will experience a partial voltage drop, which will reduce the current.
Power
When current flows through a resistor, it heats up. Electrical energy is converted into thermal energy, and this transformation is measured in power - the amount of energy converted per second. The unit of power is the watt (W), and to calculate the power dissipated by a component as it heats up due to the current passing through it, you need to multiply the voltage across it (in volts) by the current passing through it (in amperes).
Let's go back to the resistor shown in the diagram, which has a resistance of 1 kΩ. If it is subjected to a voltage of 2.2 V and a current of 2.2 mA flows through it, it will dissipate approximately 4.8 mW of power. This is a very small amount of power. However, transistors also generate heat, determined by multiplying the current passing through them by the voltage. If you were working with a fairly powerful motor consuming, say, 800 mA of current, then the voltage drop across the transistor as a whole would be about 1.2 V. In this case, the power converted into heat would be:
800 mA x 1.2 V = 960 mW.
In this case, the transistor would heat up significantly. If a transistor overheats, something inside it could melt, causing it to fail. Therefore, the limitation of the maximum allowable current exists not only for Arduino or Raspberry Pi but also for the transistor. Relatively large transistors are typically designed to handle a stronger current than smaller ones, and this factor must be taken into account when selecting a transistor for controlling the actuator.
The composite transistor (Darlington pair) MPSA14 used in our experiment on motor control (see "Experiment: Motor Control" section) can handle a current of up to 1 A.
Resistors
Resistors may appear as small components with colorful bands on them. To determine the resistance value of a resistor, you can measure it with a multimeter or decipher it based on the color bands. Each color corresponds to a number:
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
NOTE
The gold and silver colors not only indicate fractions, 1/10 and 1/100, respectively, but also represent the precision of the resistor. Gold means ±5%, while silver means ±10%.
Typically, resistors have three such color bands at one end, followed by a gap and another band at the other end, which denotes the accuracy of the resistor's value.
The arrangement of the colored bands is shown in the diagram. The value of the resistor is determined by the first three bands: the first corresponds to the first digit, the second to the second digit, and the third is the "multiplier," indicating how many zeros to add after the first two digits.
Based on what was mentioned, the resistor depicted in the diagram has a resistance of 270 ohms: the first digit is 2 (red band), the second is 7 (violet band), and the multiplier is 1 (brown band), adding two zeros after the digits 27. Similarly, a 1 kohm resistor will have brown, black, and red bands (1, 0, 00).
Resistors also vary in nominal power ratings. Almost all common resistors of the type used in the circuits of this book (for through-hole mounting) have a power rating of 0.25 watts. Other common nominal power values include 0.5 watts, 1 watt, and 2 watts, with higher-rated resistors being physically larger.
Transistors
Typically, any electronics component supplier offers an immense variety of transistors. To simplify the search, I have selected only four transistors for use in this book. These transistors form the basis for creating nearly all electronic circuits capable of controlling various devices connected to Arduino and Raspberry Pi.
The transistor mentioned in the "Experiment: Motor Control" section is precisely the component that allows weak current, such as a few milliamperes, to control hundreds of milliamperes supplied to a motor. While transistors can be used for other purposes, in this book, they will serve as switches. A small current flows into the transistor's base and then to the ground (GND) through the transistor's emitter. This current will switch a much stronger current from the collector to the emitter. The image below shows an assortment of transistors of different types that can handle various power levels.
There are only a few different transistor package styles. It is impossible to determine their properties solely by their appearance; you need to read what is written on them.
The most common package variants are TO-92 (left in the image) and TO-220 (center in the image). Occasionally, for handling very high currents, transistors with larger packages, like TO-247 shown on the right in the image, may be used. Models TO-220 and TO-247 are intended for mounting on heat sinks. However, if you are using these transistors at currents significantly lower than their maximum ratings, it is not necessary to mount them on heat sinks.
Bipolar Transistors
Transistors are manufactured using various technologies, each with its own advantages and disadvantages. Therefore, a transistor that suits one situation may not be suitable for another.
When starting to work with transistors, you will most likely encounter bipolar transistors. They have remained largely unchanged since the early days of the transistor industry. The advantage of such transistors is their affordability and ease of use at low load currents. However, they also have a drawback. Although a small current flowing from the base to the emitter of the transistor can allow a stronger current to flow from the collector to the emitter, the collector current is limited by the base current multiplier (called the current gain or hFE), which usually ranges from 50 to 200. Therefore, if the Raspberry Pi provides a base current of 2 mA, the collector current cannot exceed 100 mA. This current is much lower than what you might expect, as the transistor can handle a higher current (e.g., 500 mA). Still, it will never reach that limit because the base current is simply not sufficient. When working with Arduino, this problem usually does not arise because Arduino allows a higher base current (up to 40 mA) if you use a weaker resistor instead of a 1 kohm resistor. For example, if you use a 150 ohm resistor, the base current increases to:
1-V/R=(5-0.5)/150=30 mA.
Even in the worst-case scenario, if the transistor has a current gain of 50, a base current of 30 mA will result in a collector current of 1.5 A.
It should be noted: in the example just discussed, the voltage is calculated as (5 - 0.5) because the voltage between the base and emitter of the bipolar transistor, when the transistor is on, is about 0.5 V.
In this book, we will work with only one model of bipolar transistor, the widely used 2N3904. Although there are bipolar transistors that can handle higher currents, it is better to use more advanced transistors when dealing with higher current loads.
Composite Transistors
When you need greater amplification, for instance, if you're controlling a small motor with a Raspberry Pi board that can only provide a few milliamperes to the base, it's convenient to use a composite transistor (often referred to as a Darlington pair) instead of a regular bipolar transistor. Composite transistors typically have a gain factor of no less than 10,000.
A composite transistor contains two bipolar transistors in a single package (as shown in the diagram below), and it's this two-component structure that gives composite transistors their high gain factor.
Because a composite transistor has two components, each with its own base and emitter, when the transistor is on, each pair produces a voltage drop of at least 0.5 V. This results in a total voltage drop of 1 V, rather than the 0.5 V you'd have with a typical bipolar transistor. In reality, this voltage drop also affects the collector voltage and increases with higher load currents. Therefore, when we control a current of 1 A, the composite transistor MPSA14 is practically capable of providing us only 9 V, even though the load voltage is 12 V. In some cases, this matters; in others, it doesn't.
The transistor in the "Experiment: Motor Control" section is a composite transistor MPSA14. When using a Raspberry Pi, the voltage drop across resistor R1 will not be 3.3 V but 3.3 V + 1 V = 2.2 V. Accordingly, Raspberry Pi will need to provide a current of 2.2 V / 1 kohm = 2.2 mA.
In addition to the low-power transistor MPSA14, which is convenient for controlling loads up to 0.5 V, I also recommend working with a more powerful composite transistor, the TIP120, which is a standard component and should definitely be in your parts box.
MOSFET Transistors
In principle, a bipolar transistor is a current-controlled device: a small base current is amplified and turned into a large collector current. However, there is another type of transistor called a field-effect transistor, or MOSFET transistor (short for "metal-oxide-semiconductor field-effect transistor"), which requires a very small current to switch but remains in the on state as long as the voltage on its input gate exceeds a certain threshold value.
The diagram below schematically represents such a transistor. As you might guess, the input component of this transistor (the gate) is not electrically connected to the other transistor components.
Note: Unlike a bipolar transistor, whose components are called the base, collector, and emitter, the components of a MOSFET transistor are called the gate (G), drain (D), and source (S). You might think that the source is equivalent to the collector, but in reality, the collector of a bipolar transistor is equivalent to the drain of a field-effect transistor.
When working with Arduino or Raspberry Pi, it is highly advisable to use MOSFET transistors to switch devices in the circuit, as this can be done with minimal current. You just need to ensure that the voltage at the transistor's gate is above its threshold value. The gate threshold voltage is the point at which the MOSFET transistor turns on and allows current to flow from the drain to the source. The diagram shows how to connect a MOSFET transistor for controlling a load. As you can see, the connection is essentially the same as with a bipolar transistor.
In the circuit shown in the diagram, two symbols are used that we have not encountered before. From the source of the transistor (S), there is a line with three parallel lines that gradually shorten. This is a ground symbol - using it in the circuit eliminates the need for additional connecting lines.
The second symbol is located at the top of the circuit and is just a horizontal line marked with 6 V. It signifies that the voltage in this part of the circuit will be 6 V, eliminating the need to draw a battery.
Transistor Pinout May Vary
While we try to use transistors with compatible pinouts (lead arrangements), this rule is not universal. Not all transistors in similar packages have the same pinout, so always check the datasheet before using a new transistor.
After reviewing the diagram above, you might wonder why we still need resistor R1, considering that no current is drawn by the gate. The reason is that having this resistor is still prudent because increasing the voltage at the gate for even a fraction of a second can result in a sudden surge of current. The resistor ensures that such a surge won't damage the GPIO contact.
The complexity of working with MOSFET transistors lies in the fact that sometimes their gate threshold voltage is too high to be switched by a 3.3 V signal from a Raspberry Pi or a 5 V signal from an Arduino. MOSFET transistors with gate threshold voltages low enough to be switched directly from the GPIO contacts of our boards are known as logic-level MOSFETs. For this book, two standard MOSFET transistors are chosen: the 2N7000 for low-power applications and the FQP30N06L for relatively high-power applications. Both have a guaranteed gate threshold voltage of no more than 3 V, making them suitable for use with both Arduino and Raspberry Pi.
In general, MOSFET transistors generate less heat when switching loads compared to bipolar transistors. One of the main parameters to consider when purchasing a MOSFET component is the on resistance, which determines how easily it conducts when turned on. MOSFET transistors with very low on-resistance can handle larger currents without heating up. As you might guess, the lower the on-resistance of a MOSFET transistor, the more expensive it tends to be.
We will work quite a bit with MOSFET transistors. Primarily, I will talk about the FQP30N06L (up to 30 A), but for
such currents, you'll need substantial heat dissipation.
By the way, the collector, base, and emitter of the composite transistor TIP120 and the source, gate, and drain of the MOSFET transistor FQP30N06L are arranged identically. Therefore, you can simply remove the TIP120 from the circuit board and replace it with the FQP30N06L without changing the configuration - the circuit should still work.
PNP Transistors and P-Channel Transistors
Each of the transistor types described earlier actually exists in two varieties. So far, we've discussed only one of them: negative-positive-negative (NPN) transistors or N-channel transistors (in the case of MOSFETs). These transistors are the most common and are usually sufficient for most applications.
The other type of transistor is positive-negative-positive (PNP) or P-channel devices. While N-type devices are used to switch loads to ground, P-type devices switch loads to the positive power supply. P-channel MOSFETs are used in H-bridge motor control circuits, which we'll discuss later.
How to Choose a Transistor?
Selecting the right transistor can sometimes be challenging. With the help of a table, the choice can be narrowed down to just five transistors.
When working with Raspberry Pi, it's assumed that there's a 1 kohm resistor between the GPIO pin and the transistor's base or gate. In the case of Arduino, a 150 ohm resistor for this purpose is assumed. These values are obtained from testing real devices, and the maximum voltage values are taken from product specifications.
Table: Useful Transistor Selection
When purchasing the FQP30N061 transistor, make sure it's the L (logic-level) version, as the model name should end in L; otherwise, the gate threshold voltage might be too high.
The MPSA14 transistor is almost universal for currents up to 1 A. However, at such currents, the voltage drop is nearly 3 V, and the transistor heats up to 120°C! At 500 mA, the voltage drop is less severe at 1.8 V, and the transistor temperature is 60°C.
So, if you need to switch currents of about 100 mA, the 2N3904 transistor is sufficient. For 1 A, use the MPSA14 transistor. For higher currents, the best option is probably the FQP30N06L transistor, unless the price is a concern, as the TIP120 transistor is significantly cheaper.
Diodes
The diode D1, included in the circuit shown in Fig. 5.1, is used to protect Raspberry Pi or Arduino boards and the transistor.
Electric motors generate voltage spikes and various electrical noise, which can wreak havoc on delicate electronics like Raspberry Pi or Arduino. The diode ensures that current surges caused by the motor won't lead to an instantaneous change in current direction, which could burn out the transistor. The diode only allows current to flow in one direction—it's like a one-way valve for electricity. Current can pass through the diode only in the direction indicated by the diode, which looks like an arrow.
Due to these diode properties, they are often placed on the motor terminals. Normally, current flows through the motor in the opposite direction to what the diode allows. However, if there is a negative voltage spike, the diode kicks in, conducts the current, and nullifies it, effectively suppressing a short circuit.
Light Emitting Diodes (LEDs)
We will discuss light-emitting diodes (LEDs) in much more detail later.
As you might have guessed, an LED functions like a regular diode, but when current flows through it, it emits light. The schematic symbol for an LED is the same as that of a regular diode, but it includes arrows on the right to indicate light emission (as shown below).
LEDs come in various colors and sizes. You can control them directly from Arduino or Raspberry Pi GPIO pins, but, just like when working with transistors, it's necessary to use a resistor to limit the current. We'll cover how to do this later.
Capacitors
A capacitor can be thought of as a temporary store of electricity, somewhat like very small batteries that hold a small reserve of charge. Capacitors will be used in various capacities in the book's projects—we will use them to suppress electrical noise and store small reserves of electrical energy that can be tapped into when needed.
The symbols for capacitors are shown in the diagram. If a capacitor has a very high capacitance value, it is typically polarized. Small-capacity capacitors do not have a positive or negative pole.
Sometimes, slightly different but still recognizable symbols for capacitors are used: in these, the positive pole of a polarized capacitor is indicated by an empty frame, and the negative pole is filled. In this book, capacitors are represented using the symbolism commonly used in the United States, as shown in the diagram.
Integrated Circuits
Integrated circuits (ICs), often simply called chips or microchips, consist of many transistors, diodes, and other electronic components placed on a silicon crystal and interconnected into complex circuits. Both Raspberry Pi and Arduino boards have numerous such microchips and other electronic elements on their printed circuit boards.
There are specialized integrated microchips designed to work in various types of electronic devices. However, in this book, we are particularly interested in microchips that allow us to control electronic devices. In these microchips, high-power transistors and control logic circuits are often combined in the same package.
Resistors may appear as small components with colorful bands on them. To determine the resistance value of a resistor, you can measure it with a multimeter or decipher it based on the color bands. Each color corresponds to a number:
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
NOTE
The gold and silver colors not only indicate fractions, 1/10 and 1/100, respectively, but also represent the precision of the resistor. Gold means ±5%, while silver means ±10%.
Typically, resistors have three such color bands at one end, followed by a gap and another band at the other end, which denotes the accuracy of the resistor's value.
The arrangement of the colored bands is shown in the diagram. The value of the resistor is determined by the first three bands: the first corresponds to the first digit, the second to the second digit, and the third is the "multiplier," indicating how many zeros to add after the first two digits.
Based on what was mentioned, the resistor depicted in the diagram has a resistance of 270 ohms: the first digit is 2 (red band), the second is 7 (violet band), and the multiplier is 1 (brown band), adding two zeros after the digits 27. Similarly, a 1 kohm resistor will have brown, black, and red bands (1, 0, 00).
Resistors also vary in nominal power ratings. Almost all common resistors of the type used in the circuits of this book (for through-hole mounting) have a power rating of 0.25 watts. Other common nominal power values include 0.5 watts, 1 watt, and 2 watts, with higher-rated resistors being physically larger.
Transistors
Typically, any electronics component supplier offers an immense variety of transistors. To simplify the search, I have selected only four transistors for use in this book. These transistors form the basis for creating nearly all electronic circuits capable of controlling various devices connected to Arduino and Raspberry Pi.
The transistor mentioned in the "Experiment: Motor Control" section is precisely the component that allows weak current, such as a few milliamperes, to control hundreds of milliamperes supplied to a motor. While transistors can be used for other purposes, in this book, they will serve as switches. A small current flows into the transistor's base and then to the ground (GND) through the transistor's emitter. This current will switch a much stronger current from the collector to the emitter. The image below shows an assortment of transistors of different types that can handle various power levels.
There are only a few different transistor package styles. It is impossible to determine their properties solely by their appearance; you need to read what is written on them.
The most common package variants are TO-92 (left in the image) and TO-220 (center in the image). Occasionally, for handling very high currents, transistors with larger packages, like TO-247 shown on the right in the image, may be used. Models TO-220 and TO-247 are intended for mounting on heat sinks. However, if you are using these transistors at currents significantly lower than their maximum ratings, it is not necessary to mount them on heat sinks.
Bipolar Transistors
Transistors are manufactured using various technologies, each with its own advantages and disadvantages. Therefore, a transistor that suits one situation may not be suitable for another.
When starting to work with transistors, you will most likely encounter bipolar transistors. They have remained largely unchanged since the early days of the transistor industry. The advantage of such transistors is their affordability and ease of use at low load currents. However, they also have a drawback. Although a small current flowing from the base to the emitter of the transistor can allow a stronger current to flow from the collector to the emitter, the collector current is limited by the base current multiplier (called the current gain or hFE), which usually ranges from 50 to 200. Therefore, if the Raspberry Pi provides a base current of 2 mA, the collector current cannot exceed 100 mA. This current is much lower than what you might expect, as the transistor can handle a higher current (e.g., 500 mA). Still, it will never reach that limit because the base current is simply not sufficient. When working with Arduino, this problem usually does not arise because Arduino allows a higher base current (up to 40 mA) if you use a weaker resistor instead of a 1 kohm resistor. For example, if you use a 150 ohm resistor, the base current increases to:
1-V/R=(5-0.5)/150=30 mA.
Even in the worst-case scenario, if the transistor has a current gain of 50, a base current of 30 mA will result in a collector current of 1.5 A.
It should be noted: in the example just discussed, the voltage is calculated as (5 - 0.5) because the voltage between the base and emitter of the bipolar transistor, when the transistor is on, is about 0.5 V.
In this book, we will work with only one model of bipolar transistor, the widely used 2N3904. Although there are bipolar transistors that can handle higher currents, it is better to use more advanced transistors when dealing with higher current loads.
Composite Transistors
When you need greater amplification, for instance, if you're controlling a small motor with a Raspberry Pi board that can only provide a few milliamperes to the base, it's convenient to use a composite transistor (often referred to as a Darlington pair) instead of a regular bipolar transistor. Composite transistors typically have a gain factor of no less than 10,000.
A composite transistor contains two bipolar transistors in a single package (as shown in the diagram below), and it's this two-component structure that gives composite transistors their high gain factor.
Because a composite transistor has two components, each with its own base and emitter, when the transistor is on, each pair produces a voltage drop of at least 0.5 V. This results in a total voltage drop of 1 V, rather than the 0.5 V you'd have with a typical bipolar transistor. In reality, this voltage drop also affects the collector voltage and increases with higher load currents. Therefore, when we control a current of 1 A, the composite transistor MPSA14 is practically capable of providing us only 9 V, even though the load voltage is 12 V. In some cases, this matters; in others, it doesn't.
The transistor in the "Experiment: Motor Control" section is a composite transistor MPSA14. When using a Raspberry Pi, the voltage drop across resistor R1 will not be 3.3 V but 3.3 V + 1 V = 2.2 V. Accordingly, Raspberry Pi will need to provide a current of 2.2 V / 1 kohm = 2.2 mA.
In addition to the low-power transistor MPSA14, which is convenient for controlling loads up to 0.5 V, I also recommend working with a more powerful composite transistor, the TIP120, which is a standard component and should definitely be in your parts box.
MOSFET Transistors
In principle, a bipolar transistor is a current-controlled device: a small base current is amplified and turned into a large collector current. However, there is another type of transistor called a field-effect transistor, or MOSFET transistor (short for "metal-oxide-semiconductor field-effect transistor"), which requires a very small current to switch but remains in the on state as long as the voltage on its input gate exceeds a certain threshold value.
The diagram below schematically represents such a transistor. As you might guess, the input component of this transistor (the gate) is not electrically connected to the other transistor components.
Note: Unlike a bipolar transistor, whose components are called the base, collector, and emitter, the components of a MOSFET transistor are called the gate (G), drain (D), and source (S). You might think that the source is equivalent to the collector, but in reality, the collector of a bipolar transistor is equivalent to the drain of a field-effect transistor.
When working with Arduino or Raspberry Pi, it is highly advisable to use MOSFET transistors to switch devices in the circuit, as this can be done with minimal current. You just need to ensure that the voltage at the transistor's gate is above its threshold value. The gate threshold voltage is the point at which the MOSFET transistor turns on and allows current to flow from the drain to the source. The diagram shows how to connect a MOSFET transistor for controlling a load. As you can see, the connection is essentially the same as with a bipolar transistor.
In the circuit shown in the diagram, two symbols are used that we have not encountered before. From the source of the transistor (S), there is a line with three parallel lines that gradually shorten. This is a ground symbol - using it in the circuit eliminates the need for additional connecting lines.
The second symbol is located at the top of the circuit and is just a horizontal line marked with 6 V. It signifies that the voltage in this part of the circuit will be 6 V, eliminating the need to draw a battery.
Transistor Pinout May Vary
While we try to use transistors with compatible pinouts (lead arrangements), this rule is not universal. Not all transistors in similar packages have the same pinout, so always check the datasheet before using a new transistor.
After reviewing the diagram above, you might wonder why we still need resistor R1, considering that no current is drawn by the gate. The reason is that having this resistor is still prudent because increasing the voltage at the gate for even a fraction of a second can result in a sudden surge of current. The resistor ensures that such a surge won't damage the GPIO contact.
The complexity of working with MOSFET transistors lies in the fact that sometimes their gate threshold voltage is too high to be switched by a 3.3 V signal from a Raspberry Pi or a 5 V signal from an Arduino. MOSFET transistors with gate threshold voltages low enough to be switched directly from the GPIO contacts of our boards are known as logic-level MOSFETs. For this book, two standard MOSFET transistors are chosen: the 2N7000 for low-power applications and the FQP30N06L for relatively high-power applications. Both have a guaranteed gate threshold voltage of no more than 3 V, making them suitable for use with both Arduino and Raspberry Pi.
In general, MOSFET transistors generate less heat when switching loads compared to bipolar transistors. One of the main parameters to consider when purchasing a MOSFET component is the on resistance, which determines how easily it conducts when turned on. MOSFET transistors with very low on-resistance can handle larger currents without heating up. As you might guess, the lower the on-resistance of a MOSFET transistor, the more expensive it tends to be.
We will work quite a bit with MOSFET transistors. Primarily, I will talk about the FQP30N06L (up to 30 A), but for
such currents, you'll need substantial heat dissipation.
By the way, the collector, base, and emitter of the composite transistor TIP120 and the source, gate, and drain of the MOSFET transistor FQP30N06L are arranged identically. Therefore, you can simply remove the TIP120 from the circuit board and replace it with the FQP30N06L without changing the configuration - the circuit should still work.
PNP Transistors and P-Channel Transistors
Each of the transistor types described earlier actually exists in two varieties. So far, we've discussed only one of them: negative-positive-negative (NPN) transistors or N-channel transistors (in the case of MOSFETs). These transistors are the most common and are usually sufficient for most applications.
The other type of transistor is positive-negative-positive (PNP) or P-channel devices. While N-type devices are used to switch loads to ground, P-type devices switch loads to the positive power supply. P-channel MOSFETs are used in H-bridge motor control circuits, which we'll discuss later.
How to Choose a Transistor?
Selecting the right transistor can sometimes be challenging. With the help of a table, the choice can be narrowed down to just five transistors.
When working with Raspberry Pi, it's assumed that there's a 1 kohm resistor between the GPIO pin and the transistor's base or gate. In the case of Arduino, a 150 ohm resistor for this purpose is assumed. These values are obtained from testing real devices, and the maximum voltage values are taken from product specifications.
Table: Useful Transistor Selection
When purchasing the FQP30N061 transistor, make sure it's the L (logic-level) version, as the model name should end in L; otherwise, the gate threshold voltage might be too high.
The MPSA14 transistor is almost universal for currents up to 1 A. However, at such currents, the voltage drop is nearly 3 V, and the transistor heats up to 120°C! At 500 mA, the voltage drop is less severe at 1.8 V, and the transistor temperature is 60°C.
So, if you need to switch currents of about 100 mA, the 2N3904 transistor is sufficient. For 1 A, use the MPSA14 transistor. For higher currents, the best option is probably the FQP30N06L transistor, unless the price is a concern, as the TIP120 transistor is significantly cheaper.
Diodes
The diode D1, included in the circuit shown in Fig. 5.1, is used to protect Raspberry Pi or Arduino boards and the transistor.
Electric motors generate voltage spikes and various electrical noise, which can wreak havoc on delicate electronics like Raspberry Pi or Arduino. The diode ensures that current surges caused by the motor won't lead to an instantaneous change in current direction, which could burn out the transistor. The diode only allows current to flow in one direction—it's like a one-way valve for electricity. Current can pass through the diode only in the direction indicated by the diode, which looks like an arrow.
Due to these diode properties, they are often placed on the motor terminals. Normally, current flows through the motor in the opposite direction to what the diode allows. However, if there is a negative voltage spike, the diode kicks in, conducts the current, and nullifies it, effectively suppressing a short circuit.
Light Emitting Diodes (LEDs)
We will discuss light-emitting diodes (LEDs) in much more detail later.
As you might have guessed, an LED functions like a regular diode, but when current flows through it, it emits light. The schematic symbol for an LED is the same as that of a regular diode, but it includes arrows on the right to indicate light emission (as shown below).
LEDs come in various colors and sizes. You can control them directly from Arduino or Raspberry Pi GPIO pins, but, just like when working with transistors, it's necessary to use a resistor to limit the current. We'll cover how to do this later.
Capacitors
A capacitor can be thought of as a temporary store of electricity, somewhat like very small batteries that hold a small reserve of charge. Capacitors will be used in various capacities in the book's projects—we will use them to suppress electrical noise and store small reserves of electrical energy that can be tapped into when needed.
The symbols for capacitors are shown in the diagram. If a capacitor has a very high capacitance value, it is typically polarized. Small-capacity capacitors do not have a positive or negative pole.
Sometimes, slightly different but still recognizable symbols for capacitors are used: in these, the positive pole of a polarized capacitor is indicated by an empty frame, and the negative pole is filled. In this book, capacitors are represented using the symbolism commonly used in the United States, as shown in the diagram.
Integrated Circuits
Integrated circuits (ICs), often simply called chips or microchips, consist of many transistors, diodes, and other electronic components placed on a silicon crystal and interconnected into complex circuits. Both Raspberry Pi and Arduino boards have numerous such microchips and other electronic elements on their printed circuit boards.
There are specialized integrated microchips designed to work in various types of electronic devices. However, in this book, we are particularly interested in microchips that allow us to control electronic devices. In these microchips, high-power transistors and control logic circuits are often combined in the same package.
Digital Outputs
Having covered the basics of electronics, it's essential to briefly mention how various electronic devices can be connected to Raspberry Pi or Arduino. These topics have been explored in detail previously, especially everything related to programming. So, if you need any clarifications, you can refer back to those sections.
Digital Outputs
As shown earlier, digital outputs are used for turning devices on and off. If you're working with Raspberry Pi, the voltage on a digital output will be either 0V (low) or 3.3V (high). For Arduino, the high voltage is 5V, not 3.3V, but the principle is similar – there are only two voltage values on digital outputs: high and low.
Another difference between Arduino and Raspberry Pi is that Arduino can handle higher current (40mA, not 16mA).
Digital Inputs
Digital inputs are often connected to switches or digital outputs of other devices. The threshold voltage for digital input is usually the midpoint between high and low. For Arduino, this threshold is around 2.5V, while for Raspberry Pi, it's approximately 1.65V. When a program running on Arduino or Raspberry Pi reads the value on a digital input, it checks whether the voltage is above or below this threshold: in the first case, the input is considered high, and in the second case, it's low.
Like digital outputs, digital inputs cannot have any intermediate voltage values; they are either high or low. Previous sections have covered how to work with digital inputs on Arduino and Raspberry Pi.
Analog Inputs
Analog inputs allow you to measure the voltage between high and low values on their pins. Raspberry Pi doesn't have analog inputs, but Arduino has six of them, labeled from A0 to A5.
In Arduino, any voltage between 0V and 5V corresponds to a numeric value ranging from 0 to 1023. So, 0V corresponds to 0, and 5V corresponds to 1023. An average voltage of 2.5V would correspond to a value around 511.
Analog Outputs
Although it might seem that an analog output allows you to set any voltage value between low and high, the reality is a bit more complex. These pins use a technique called Pulse Width Modulation (PWM) to control the average power delivered to a regular digital output.
PWM is used for controlling the speed of electric motors and the brightness of LEDs.
Serial Interfaces
Earlier, we described the simplest low-level techniques for connecting electronic devices to Arduino or Raspberry Pi. However, some devices you might want to control with Arduino or Raspberry Pi use serial interfaces that transmit binary data bit by bit from one device's digital output to another device's digital input.
For instance, you will encounter displays that require data to be sent via a serial interface. There are various standards for serial interfaces, each solving similar problems but in slightly different ways. These standards include the serial interface (also known as TTL serial), the I^2C (pronounced "I-squared-C") interface, and the Serial Peripheral Interface (SPI).
Having covered the basics of electronics, it's essential to briefly mention how various electronic devices can be connected to Raspberry Pi or Arduino. These topics have been explored in detail previously, especially everything related to programming. So, if you need any clarifications, you can refer back to those sections.
Digital Outputs
As shown earlier, digital outputs are used for turning devices on and off. If you're working with Raspberry Pi, the voltage on a digital output will be either 0V (low) or 3.3V (high). For Arduino, the high voltage is 5V, not 3.3V, but the principle is similar – there are only two voltage values on digital outputs: high and low.
Another difference between Arduino and Raspberry Pi is that Arduino can handle higher current (40mA, not 16mA).
Digital Inputs
Digital inputs are often connected to switches or digital outputs of other devices. The threshold voltage for digital input is usually the midpoint between high and low. For Arduino, this threshold is around 2.5V, while for Raspberry Pi, it's approximately 1.65V. When a program running on Arduino or Raspberry Pi reads the value on a digital input, it checks whether the voltage is above or below this threshold: in the first case, the input is considered high, and in the second case, it's low.
Like digital outputs, digital inputs cannot have any intermediate voltage values; they are either high or low. Previous sections have covered how to work with digital inputs on Arduino and Raspberry Pi.
Analog Inputs
Analog inputs allow you to measure the voltage between high and low values on their pins. Raspberry Pi doesn't have analog inputs, but Arduino has six of them, labeled from A0 to A5.
In Arduino, any voltage between 0V and 5V corresponds to a numeric value ranging from 0 to 1023. So, 0V corresponds to 0, and 5V corresponds to 1023. An average voltage of 2.5V would correspond to a value around 511.
Analog Outputs
Although it might seem that an analog output allows you to set any voltage value between low and high, the reality is a bit more complex. These pins use a technique called Pulse Width Modulation (PWM) to control the average power delivered to a regular digital output.
PWM is used for controlling the speed of electric motors and the brightness of LEDs.
Serial Interfaces
Earlier, we described the simplest low-level techniques for connecting electronic devices to Arduino or Raspberry Pi. However, some devices you might want to control with Arduino or Raspberry Pi use serial interfaces that transmit binary data bit by bit from one device's digital output to another device's digital input.
For instance, you will encounter displays that require data to be sent via a serial interface. There are various standards for serial interfaces, each solving similar problems but in slightly different ways. These standards include the serial interface (also known as TTL serial), the I^2C (pronounced "I-squared-C") interface, and the Serial Peripheral Interface (SPI).