© Charles Bell 2016

Charles Bell, Windows 10 for the Internet of Things, 10.1007/978-1-4842-2108-2_8

8. Electronics for Beginners

Charles Bell

(1)Warsaw, Virginia, USA

If you’re new to the IoT or have never worked with electronics, you may be wondering how you’re going to get your ideas for an IoT solution realized. The projects in this book walk you through how to connect the various components used and thus you can complete them without a lot of additional information or specialized skills.

However, if something goes wrong or you want to create projects on your own, you may need a bit more information than “plug this end in here.” More specifically, you need to know enough about how the components work in order to successfully complete your project—whether that is completing the examples in this book or examples found elsewhere on the Internet.

Rather than attempt to present a comprehensive tutorial on electronics, which would take several volumes, this chapter presents an overview of electronics for those who want to work with the types of electronic components commonly found in IoT projects. I include an overview of some of the basics, descriptions of common components, and a look at sensors. If you are new to electronics, this chapter gives you the extra boost you need to understand the components used in the projects in this book.

However, if you have experience with electronics either at the hobbyist or enthusiast level or have experience or formal training in electronics, you may want to skim this chapter or read the sections with topics that interest you.

Let’s begin with a look at the basics of electronics. Once again, this is in no way a tutorial that covers all there is to know, but it gets you to the point where the projects make sense in how they connect and use components.

The Basics

This section presents a short overview of some of the most common tools and techniques you need to use when working with electronics. As you will see, you only need the most basic of tools and the skills or techniques are not difficult to learn. However, before you get into those, let’s discuss the most fundamental concept you must understand when working with electronics—power!

Powering Your Electronics

Electricity1 is briefly defined as the flow of electric charge and when used, provides power for your electronics—a common lightbulb, a ceiling fan, a high-definition television, a tablet. Whether you are powering your electronics with batteries or a power supply, you are initiating a circuit where electrons flow in specific patterns. There are two forms (or kinds) of power that you will use. Your home is powered by alternating current and your electronics are powered by direct current.

The term alternating current (AC) is used to describe the flow of charged particles that changes direction periodically at a specific rate (or cycle) reversing the voltage along with the current. Thus, AC systems are designed to work with a specific range of cycles as well as voltage. Typically, AC systems use higher voltages than direct current systems.

The term direct current (DC) is used to describe the flow of charged particles that do not change direction and thus always flow in a specific “direction.” Most electronics systems are powered with DC voltages and are typically at lower voltages than AC systems; for example, IoT projects typically run on lower direct current (DC) voltages in the range 3.3V–24V.

Tip

For more information about AC and DC current and the differences, see https://learn.sparkfun.com/tutorials/alternating-current-ac-vs-direct-current-dc .

Since DC flows in a single direction, components that operate on DC have a positive side and a negative side, where current flows from positive to negative. The orientation of these sides—one to positive and one to negative—is called polarity. Some components, such as resistors, can operate in either “direction” but you should always be sure to connect your components according to its polarity. Most components are clearly marked but those that are not have a well-known arrangement. (For example, the positive pole (side) of an LED is the longer of the two legs; it is called an anode. The negative and shorter leg is called the cathode).

Despite the lower voltages, you mustn’t think that they are completely harmless or safe. Incorrectly wiring electronics (reversing polarity) or shorting (connecting positive and negative together) can damage your electronics and in some cases cause overheating, which, in extreme cases, cause electronics to catch fire.

Caution

Don’t be tempted to think working with 3.3 or 5.5 volts is “safe.” Even a small amount of voltage improperly connected can lead to potentially devastating results. Don’t assume low DC voltage is harmless.

I had a lesson in just how real this scenario can be a couple of years ago. I was changing the batteries in my smoke detectors. I took the old batteries out and placed them in my pocket. I had forgotten I had a small penknife in the same pocket. One of the 9V batteries shorted on the knife and within about ten minutes, the battery heated to an alarming temperature. It wasn’t enough to burn but had I left something like that unattended, it could have been bad.

That’s a scary thought, isn’t it? Consider it an admonishment as well as a warning; you should never relax your safe handling practices even for lower voltage projects.

Finally, DC components are often rated for a specific voltage range. Recall from the discussion on the various low-cost computing boards and GPIO headers, some boards operate at 5V, whereas others operate at 3.3V (or less). Fortunately, there are several ways you can adapt components that work at different voltages—by using other components!

Note

I have deliberately kept the discussion on power simple. There is far more to electrical current—even DC—than what I’ve described here. As long as you understand these basics, you’ll be able to work with the projects in this book and more.

Now let’s take a look at some of the tools you need to work on your IoT projects.

Tools

The vast majority of tools you need to construct your IoT projects are common hand tools (screwdrivers, small wrenches, pliers, etc.). For larger projects or for creating enclosures you may need additional tools, such as power tools, but I concentrate only on those tools needed for building the projects. The following is a list of tools that I recommend to get you started.

  • Breadboard

  • Breadboard wires (also called jumpers)

  • Electrostatic discharge (ESD) safe tweezers

  • Helping hands or printed circuit board (PCB) holder

  • Multimeter

  • Needle-nose pliers

  • Screw drivers: assorted sizes (micro, small)

  • Solder

  • Soldering iron

  • Solder remover (solder sucker)

  • Tool case, roll, or box for storage

  • Wire strippers

However, you cannot go wrong if you prefer to buy a complete electronics toolset, such as those from SparkFun ( www.sparkfun.com/categories/47 ) or Adafruit ( www.adafruit.com/categories/83 ). You can often find electronics kits at major brand electronics stores and home improvement centers. If you are fortunate enough to live near a Fry’s Electronics, you can find just about any electronics tool made. Most electronics kits have all the hand tools that you need. Some even come with a multimeter, but more often you have to buy it separately.

Most of the tools in the list do not need any explanation except to say you should purchase the best tools that your budget permits. The following paragraphs describe some of the tools that are used for special tasks, such as stripping wires, soldering, and measuring voltage and current.

Multimeter

A multimeter is one of those tools that you need when building IoT solutions. You also need it to do almost any electrical repair on your circuits. There are many different multimeters available with prices ranging from inexpensive, basic units to complex, feature-rich, incredibly expensive units. For most IoT projects, including most IoT kits, a basic unit is all that you need. However, if you plan to build more than one IoT solution or want to assemble your own electronics, you may want to invest a bit more in a more sophisticated multimeter. Figure 8-1 shows a basic digital multimeter (costing about $10) on the left and a professional multimeter from BK Precision on the right.

A416714_1_En_8_Fig1_HTML.jpg
Figure 8-1. Digital multimeters

Notice that the better meter has more granular settings and more features. Again, you probably won’t need more than the basic unit. You need to measure voltage, current, and resistance at a minimum. Whichever meter you buy, make sure that it has modes for measuring AC and DC voltage, continuity testing (with an audible alert), and checking resistance. I explain how to use a multimeter in a later section.

Tip

Most multimeters including the inexpensive ones come with a small instruction booklet that shows you how to measure voltage, resistance, and other functions of the unit.

Soldering Iron

A soldering iron is not required for any of the projects in this book because you use a breadboard to lay out and connect the components. However, if you plan to build a simple IoT solution where you need to solder wires, or maybe a few connectors, a basic soldering iron from an electronics store such as Radio Shack is all you need. On the other hand, if you plan to assemble your own electronics, you may want to consider getting a good, professional soldering iron, such as a Hakko. The professional models include features that allow you to set the temperature of the wand, have a wider array of tips available, and tend to last a lot longer. Figure 8-2 shows a well-used entry-level Radio Shack. Figure 8-3 shows a professional model Hakko soldering iron.

A416714_1_En_8_Fig2_HTML.jpg
Figure 8-2. Entry-level soldering iron
A416714_1_En_8_Fig3_HTML.jpg
Figure 8-3. Professional soldering iron
Tip

For best results, choose a solder with a low lead content in the 37%–40% range. If you use a professional soldering iron, adjust the temperature to match the melting point of the solder (listed on the label).

Wire Strippers

There are several types of wire strippers. In fact, there are probably a dozen or more designs out there. But there really are two kinds: ones that only grip and cut the insulation as you pull it off the wire, and those that grip, cut, and remove the insulation. The first type is more common and, with some practice, does just fine for most small jobs (like repairing a broken wire); but the second type makes a larger job—such as wiring electronics from bare wire (no prefab connectors)—much faster. As you can imagine, the first type is considerably cheaper. Figure 8-4 shows both types of wire strippers. Either is a good choice.

A416714_1_En_8_Fig4_HTML.jpg
Figure 8-4. Wire strippers

Helping Hands

There is one other tool that you may want to get, especially if you need to do any soldering; it’s called helping handsor a third-hand tool. Most have a pair of alligator clips to hold wires, printed circuit boards, or components while you solder. Figure 8-5 shows an excellent example from Adafruit ( www.adafruit.com/products/291 ).

A416714_1_En_8_Fig5_HTML.jpg
Figure 8-5. Helping hands tool (courtesy of adafruit.com)

Now let’s take a look at some of the skills you are likely to need when working with advanced IoT projects.

Using a Multimeter

The electrical skills needed for IoT projects can vary from plugging in wires on a breadboard—as you saw with the projects so far—to needing to solder components together or to printed circuit boards (PCBs) . Regardless of whether you need to solder the electronics, you need to be able to use a basic multimeter to measure resistance and check voltage and current.

A multimeter is a very useful and essential tool for any electronics hobbyist and downright required for any enthusiast of worth. A typical multimeter has a digital display2 (typically an LCD or similar numeric display), a dial, and two or more posts or ports for plugging in test leads with probe ends. Most multimeters have ports for lower current (that you will use most) and ports for higher current. Test leads use red for positive and black for negative (ground). The ground port is where you plug in the black test lead and is often marked either with a dash or COM for common. Which of the other ports you use depends on what you are testing.

One thing to note on the dial is that there are many settings (with some values repeated) or those that look similar. For example, you see a set of values (sometimes called a scale) for ohms; one or two sets of values for amperage; and one or two sets of values for volts. The DC voltage is indicated by a V with a solid and dashed line over it; whereas the AC voltage is indicated by a V with a wavy line over it. Amperage ranges are marked in the same manner. Figure 8-6 shows a close-up of a multimeter dial labeled with the sets of values that I mentioned.

A416714_1_En_8_Fig6_HTML.jpg
Figure 8-6. Multimeter dial (typical)
Tip

When not in use, be sure to turn your multimeter dial to off or one of the voltage ranges if it has a separate off button.

There is a lot you can do with a multimeter. You can check voltage, measure resistance, and even check continuity. Most basic multimeters do all of these functions; however, some multimeters have a great many more features, such as testing capacitors, and the ability to test AC and DC.

Let’s see how you can use a multimeter to perform the most common tasks you need for IoT projects: testing continuity, measuring voltage in a DC circuit, measuring resistance, and measuring current.

Testing Continuity

You test for continuity to determine if there is a path for the charged particles to flow. That is, your wires and components are connected properly; for example, you may want to check to ensure that a wire has been spliced correctly.

To test for continuity, turn your multimeter dial to the position marked with an audible symbol, bell, or triangle with an arrow through it. Plug the black test lead into the COM port and the red test lead in the port marked with Hz VΩ or similar. Now you can touch the probe end of the test leads together to hear an audible tone or beep. Some multimeters don’t have an audible tone but instead may display “1” or similar to indicate continuity. Check your manual for how your multimeter indicates continuity. Figure 8-7 shows how to set a multimeter to check for continuity including which ports to plug in the test leads.

A416714_1_En_8_Fig7_HTML.jpg
Figure 8-7. Settings for checking continuity

In Figure 8-7, I simply touched the probes together to demonstrate how to check for continuity. I like to do this just to ensure that my multimeter is turned on and in the correct setting.3

Another excellent use for the continuity test is when diagnosing or discovering how cables are wired. For example, you can use the continuity test to discover which connector is connected on each end of the cable (sometimes called wire sorting or ringing out, from the old telephone days).

Measuring Voltage

Our IoT projects use DC. To measure voltage in the circuit, you use the DC range on the multimeter. The DC range has several stops. This is a scale selection. Choose the scale that closely matches the voltage range you want to test. For example, for our IoT projects, you often measure 3.3V–12V, so you choose 20 on the dial. Next, plug the black test lead into the COM port and the red test lead into the port labeled Hz VΩ.

Now you need something to measure! Take any battery you have in the house and touch the black probe to the negative side and the red probe to the positive side. You should see a value appear on the display that is close to the range for the battery; for example, if you used a 1.5V battery, you should see close to 1.5V. It may not be exactly 1.5V–1.6V if the battery is depleted. So now you know how to test batteries for freshness! Figure 8-8 shows how to measure voltage of a battery.

A416714_1_En_8_Fig8_HTML.jpg
Figure 8-8. Measuring voltage of a battery

The readout displays 1.50, which is the correct voltage for this AA battery. If I had reversed the probes—the red one on negative and the black on positive, the display would have read -1.50. This is OK because it shows the current is flowing in the opposite direction of how the probes are oriented.

Note

If you use the wrong probe when measuring voltage in a DC circuit, most multimeters display the voltage as a negative number. Try that with your battery. It won’t hurt the multimeter (or the battery)!

You can use this technique to measure voltage in your projects. Just be careful to place the probes on the appropriate positions and try not to cross or short by touching more than one component at a time with a single probe tip.

Measuring Current

Current is measured as amperage (actually milliamps (mA)). Thus, you use the range marked with an A with a straight and dashed line (not the wavy one, that’s AC). You measure current in series. That is, you must place the multimeter in the circuit. This can be a little tricky because you must interrupt the flow of current and put the meter inline.

Let’s set up an experiment to measure current. Get your breadboard, an LED, a resistor, and two jumper wires you used in the blink project. Wire everything up the same way except don’t complete the circuit for the GPIO4 pin. Instead, you use the multimeter to complete the circuit by touching one probe to the positive 5V pin on the GPIO and the other probe on the resistor. Figure 8-9 shows how to set up the circuit with the multimeter inline.

A416714_1_En_8_Fig9_HTML.jpg
Figure 8-9. Measuring current

Before powering on your Raspberry Pi, plug the black test lead into the COM port and the other test leads into the port labeled mA. Some multimeters use the same port for measuring voltage as well as current. Turn the dial on the multimeter to the 200mA setting. Then power on the Raspberry Pi and touch the leads to the places indicated. Be careful to touch only the 5V pin on the header. If you want to err on the side of caution, use the remaining jumper wire and connect it to the 5V pin, and then touch the probe to the other end of the jumper wire. Once the Raspberry Pi is powered on, you should see a value on the multimeter. Figure 8-10 shows how to use a multimeter to measure current in a circuit.

A416714_1_En_8_Fig10_HTML.jpg
Figure 8-10. Measuring current

In Figure 8-10, I am using a breadboard with a breadboard power supply instead of the Raspberry Pi. Whereas the value you see on the multimeter may differ, the demonstration accomplishes the same goal. In this case, I touch the red probe to the positive pole on the power supply and the black probe on the resistor in the same manner as I described.

There is one other tricky thing about measuring current. If you attempt to measure current that is greater than the maximum for the port (for example, the meter in the photo has a maximum of 20mA on the one port. If I exceeded that by say 5A, I would likely blow a fuse in the multimeter. This is not desirable but at least there is a fuse that you can replace should you make a mistake and choose the wrong port.

Measuring Resistance

Resistance is measured in ohms (Ω) . A resistor is the most common component that you use to introduce resistance in a circuit. You can test the resistance of the charge through the resistor with your multimeter. To test resistance, choose the ohm scale that is closest to the rating of the resistor. For example, I tested a resistor that I believed about 200 ohms, but since I was not sure, I chose the 2K setting.

Next, plug the black test lead into the COM port and the red test lead into the port labeled Hz VΩ. Now, touch a probe to one side of a resistor and the other probe to the other side. It doesn’t matter which side you choose—a resistor works in both directions. Notice the readout. The meter reads one of three things: 0.00, 1, or the actual resistor value.

In this case, the meter reads 0.219, meaning this resistor has a value of 220Ω. Recall, I used the 2K scale, which means a resistor of 1K would read 1.0. Since the value is a decimal, I can move the decimal point to the left to get a whole number.

If the multimeter displays another value, such as 0 or 1, it indicates the scale is wrong and you should try a higher scale. This isn’t a problem. It just means you need to choose a larger scale. On the other hand, if the display shows 0 or a really small number, you need to choose a lower scale. I like to go one tick of the knob either way when I am testing resistance in an unknown component or circuit.

Figure 8-11 shows an example of measuring resistance for a resistor. The display reads, 219. I am testing a resistor rated at 220 ohms. The reason it is 219 instead of 220 is because the resistor I am using is rated at 220 +/- 5%. Thus, the acceptable range for this resistor is 209 ohms to 231 ohms.

A416714_1_En_8_Fig11_HTML.jpg
Figure 8-11. Measuring resistance of a resistor

Now you know how to test a resistor to discover its rating. As you will see, those rings around the body of the resistor are the primary way you know its rating but you can always test it if you’re unsure, someone has painted over it (hey, it happens), or you’re too lazy to look it up.

Electronic Components

Aside from learning how to use a multimeter and possibly learning to solder, you also need to know something about the electronic components available to build your projects. In this section, I describe some common components—listed in alphabetical order by name—that you encounter when building IoT solutions. I also cover breakout boards and logic circuits, which are small circuits built with a set of components that provide a feature or solve a problem. For example, you can get breakout boards for USB host connections, Ethernet modules, logic shifters, real time clocks, and more.

Button

A button (sometimes called a momentary button) is a mechanism that makes a connection when pressed. More specifically, a button connects two or more poles together while it is pressed. A common (and perhaps over used) example of a button is a home doorbell. When pressed, it completes a circuit that triggers a chime, bell, tone, or music to play. Some older doorbells continue to sound while the button is pressed.

In IoT projects, you use buttons to trigger events, start and stop actions, and similar operations. A button is a simple form of a switch but unlike a switch, you must continue to press the button to make the electrical connections. Most buttons have at least two legs (or pins) that are connected when the button is pressed. Some have more than two legs connected in pairs and some of those can permit multiple connections. Figure 8-12 shows a number of buttons.

A416714_1_En_8_Fig12_HTML.jpg
Figure 8-12. Momentary buttons

There is a special variant of a momentary button called a latching momentary button. This version uses a notch or detent to keep the poles connected until it is pushed again. If you’ve seen a button on a stereo or in your car that remains depressed until pressed again, it is likely a latching momentary button.

There are all kinds of buttons from those that can be used with breadboards (the spacing of the pins allow it to be plugged into a breadboard), can be mounted in a panel, or those made for soldering to printed circuit boards.

Capacitor

A capacitor is designed to store charges. As current flows through the capacitor, it accumulates charge and can discharge after the current is disconnected. In this way, it is like a battery but unlike a battery, a capacitor charges and discharges very fast. You use capacitors for all manner of current storage from blocking current, reducing noise in power supplies, in audio circuits, and more. Figure 8-13 shows a number of capacitors.

A416714_1_En_8_Fig13_HTML.jpg
Figure 8-13. Capacitors

There are several types of capacitors but you most often encounter capacitors when building power supplies for IoT projects. Most capacitors have two legs (pins) that are polarized. That is, one is positive and the other negative. Be sure to connect the capacitor with the correct polarity in your circuit.

Diode

A diode is designed to allow current to flow in only one direction. Most are marked with an arrow pointing to a line, which indicates the direction of flow. A diode is often used as rectifiers in AC-to-DC converters (devices that convert AC to DC voltage), used in conjunction with other components to suppress voltage spikes, or protect components from reversed voltage. Often used to protect against current flowing into a device.

Most diodes are shaped like a small cylinder, are usually black with silver writing, and have two legs. They look a little like resistors. You use a special variant called a Zener diode in power supplies to help regulate voltages. Figure 8-14 shows a number of Zener diodes.

A416714_1_En_8_Fig14_HTML.jpg
Figure 8-14. Diodes

Fuse

A fuse is designed to protect a device (actually the entire circuit) from current greater than what the components can safely operate. Fuses are placed inline on the positive pole. When too much current flows through the fuse, the internal parts trigger a break in the flow of current.

Some fuses use a special wire inside that melts or breaks (thereby rendering it useless but protecting your equipment) while other fuses use a mechanism that operates like a switch (many of these are resettable). When this happens, you say the fuse has “blown” or “tripped.” Fuses are rated at a certain current in amperage, indicating the maximum amps that the fuse permits to flow without tripping.

Fuses come in many shapes and varieties. They work with either AC or DC voltage. The fuses that you use are of the disposable variety. Figure 8-15 shows an example of two fuses: an automotive-style blade fuse on the left and a glass cartridge fuse on the right.

A416714_1_En_8_Fig15_HTML.jpg
Figure 8-15. Fuses

If you are familiar with your home’s electrical panel that contains the circuit breakers, they are resettable fuses. So the next time one of them goes "click" and the lights go out, you can say, "Hey, a fuse has tripped!" Better still, now you know why—you have exceeded the maximum rating of the circuit breaker.

Which is probably fine in situations where you accidentally left that infrared heater on when you dropped the toast and started the microwave (it happens), but if you are tripping breakers frequently without any load, you should call an electrician to have the circuit checked.

Light Emitting Diode (LED)

Recall from Chapter 3 that an LED is a special diode that produces light when powered.

As you learned in Chapter 3, an LED has two legs: the longer leg is positive and the shorter is negative. LEDs also have a flat edge that also indicates the negative leg. They come in a variety of sizes ranging from as small as 3mm to 10mm. Figure 8-16 shows an example of some smaller LEDs.

A416714_1_En_8_Fig16_HTML.jpg
Figure 8-16. Light emitting diodes

Recall you also need to use a resistor with an LED. You need this to help reduce the flow of the circuit to lower the current flowing through the LED. LEDs can be used with lower current (they burn a bit dimmer than normal) but should not be used with higher current.

To determine what size resistor you need, you need to know several things about the LED. This data is available from the manufacturer who provides the data in the form of a data sheet or in the case of commercially packaged products, lists the data on the package. The data you need includes the maximum voltage, the supply voltage (how many volts are coming to the LED), and the current rating of the LED.

For example, if I have an LED like the one you used in the last chapter, in this case a 5mm red LED, you find on Adafruit’s web site ( www.adafruit.com/products/297 ) that the LED operates at 1.8V–2.2V and 20mA of current. Let’s say you want to use this with a 5V supply voltage. You can then take these values and plug them into this formula:4

R = (Vcc-Vf)/I

Using more descriptive names for the variable, you get the following.

Resistor = (Volts_supply - Volts_forward) / Desired_current

Plugging in the data, you get this result. Note that you have mA so you must use the correct decimal value (divide by 1000). In this case, 0.020 and you pick a voltage in the middle.

Resistor = (5 - 2.0) / 0.020
         = 3.0 / 0.020
         = 150

Thus, you need a resistor of 150 ohms. Cool. Sometimes the formula produces a value that does not match any existing resistors. In that case, choose one closest to the value but a bit higher. Remember, you want to limit current and thus err on the side of more restrictive than less restrictive. For example, if you found you need a resistor of 95 ohms, you can use one rated at 100 ohms, which is safer than using one rated at 90 ohms.

Tip

Always err on the side of the more restrictive resistor when the formula produces a value for which there is no resistor available.

Also, if you use LEDs in serial or parallel, the formula is a little different. See https://learn.adafruit.com/all-about-leds for more information about using LEDs in your projects and calculating the size of resistors to use with LEDs.

Relay

A relay is an interesting component that helps you control higher voltages with lower voltage circuits. For example, suppose you wanted to control a device that is powered by 12V from your Raspberry Pi, which only produces a maximum of 5V. A relay can be used with a 5V circuit to turn on (or relay) power from that higher source. In this example, you would use the Raspberry Pi’s output to trigger the relay to switch on the 12V power. Thus, relays are a form of switch. Figure 8-17 shows a typical relay and how the pins are arranged.

A416714_1_En_8_Fig17_HTML.jpg
Figure 8-17. Relay

Relays can take a lot of different forms and typically have slightly different wiring options, such as where the supply voltage is attached and where the trigger voltage attaches as well as whether the initial state is open (no flow) or close (flow) and thus the behavior of how it controls voltage. Some relays come mounted on a PCB with clearly marked terminals that show where to change the switching feature and where everything plugs in. If you want to use relays in your projects, always check the datasheet to make sure that you are wiring it correctly, based on its configuration.

You can also use relays to allow your DC circuit to turn AC appliances on and off like PowerSwitch Tail from Adafruit ( www.adafruit.com/products/268 ).

Resistor

A resistor is one of the standard building blocks of electronics. Its job is to impede current and impose a reduction in voltage (which is converted to heat). Its effect, known as resistance, is measured in ohms. A resistor can be used to reduce voltage to other components, limiting frequency response, or protect sensitive components from over voltage. Figure 8-18 shows a number of resistors.

A416714_1_En_8_Fig18_HTML.jpg
Figure 8-18. Resistors

When a resistor is used to pull up voltage (by attaching one end to positive voltage) or pull down voltage (by attaching one end to ground) (resistors are bidirectional), it eliminates the possibility of the voltage floating in an indeterminate state. Thus a pull-up resistor ensures that the stable state is positive voltage, and a pull-down resistor ensures that the stable state is zero voltage (ground).

Switch

A switch is designed to control the flow of current between two or more pins. Switches come in all manner of shapes, sizes, and packaging. Some are designed as a simple on/off while others can be used to change current from one set of pins to another. Like buttons, switches come in a variety of mounting options from PCB (also called a through hole) to panel mount for mounting in enclosures. Figure 8-19 shows a variety of switches.

A416714_1_En_8_Fig19_HTML.jpg
Figure 8-19. Various switches

Switches that have only one pole (leg or side) are called single-pole switches. Switches that can divert current from one set of poles to another set are called two-pole switches. Switches where there is only one secondary connection per pole are called single-throw switches. Switches that disconnect from one set of poles and connect to another while maintaining a common input are called double-throw switches. These are often combined together and form the switch type (or kind) as follows .

  • SPST: Single pole, single throw

  • DPST: Double pole, single throw

  • SPDT: Single pole, double throw

  • DPDT: Double pole, double throw

  • 3PDT: Three pole, double throw

There may be other variants that you could encounter. I like to keep it simple: if I have just an on/off situation, I want a single throw switch. How many poles depends on how many wires or circuits I want to turn on or off at the same time. I use these for double throw switches when I have an “A” condition and a “B” condition in which I want A on when B is off, and vice-versa. I sometimes use multiple throw switches when I want A, B, and off situations. I use the center position (throw) as off. You can be very creative with switches!

Transistor

A transistor (a bipolar transistor) is designed to switch current on/off in a cycle or amplify fluctuations in current. Interestingly, transistors used to amplify current replaced vacuum tubes. If you are an audiophile, you likely know a great deal about vacuum tubes. When a resistor operates in switching mode, it behaves similar to a relay but its “off” position still allows a small amount of current to flow. Transistors are used in audio equipment, signal processing, and switching power supplies. Figure 8-20 shows two varieties of transistors.

A416714_1_En_8_Fig20_HTML.jpg
Figure 8-20. Transistors

Transistors come in all manner of varieties, packaging, and ratings that make it suitable for one solution or another.

Voltage Regulator

A voltage regulator (linear voltage regulator) is designed to keep the flow of current constant. Voltage regulators often appear in electronics when you need to condition or lower current from a source. For example, you want to supply 5V to a circuit but only have a 9V power supply. Voltage regulators accomplish this (roughly) by taking current in and dissipating the excess current through a heat sink. Thus, voltage regulators have three legs: positive current in, negative, and positive current out. They are typically shaped like those shown in figure 8-21 but other varieties exist.

A416714_1_En_8_Fig21_HTML.jpg
Figure 8-21. Voltage regulators

The small hole in the plate that extends out of the voltage regulator is where the heat sink is mounted. Voltage regulators are often numbered to match their rating; for example, a LM7805 produces 5V, whereas a LM7833 produces 3.3V.

An example of using a voltage regulator to supply power to a 3.3V circuit on a breadboard is shown in Figure 8-22. This circuit was designed with capacitors to help smooth or condition the power. Notice that the capacitors are rated by uF, which means microfarad.

A416714_1_En_8_Fig22_HTML.jpg
Figure 8-22. Power supply circuit on a breadboard with voltage regulator

Breakout Boards and Circuits

Breakout boards are your modular building blocks for IoT solutions. They typically combine several components together to form a function, such as measuring temperature, enabling reading GPS data, communicating via cellular services, and more. Figure 8-23 shows two breakout boards. On the left is an Adafruit AC/DC converter ( www.adafruit.com/products/1083 ) and on the right is an Adafruit barometric pressure sensor breakout board ( www.adafruit.com/products/391 ).

A416714_1_En_8_Fig23_HTML.jpg
Figure 8-23. Breakout boards

Whenever you design a circuit or IoT solution, you should consider using breakout boards as much as possible because they simplify the use of the components. Take the Barometric Pressure Sensor for example, Adafruit has designed this board so that all you need to do to use it is attach power and connect it to your IoT device on its I2C bus. An I2C bus is a fast digital protocol that uses two wires (plus power and ground) to read data from circuits (or devices).

Thus, there is no need to worry about how to connect the sensor to other components to use it— just connect it like any I2C device and start reading data! You use several breakout boards in the projects later in this book.

Using a Breadboard to Build Circuits

If you have been following along with the projects thus far in the book, you have already encountered a breadboard to make a very simple circuit. Recall from Chapter 3 that a breadboard is a tool you use to plug components into to form circuits. Technically, you’re using a solderless breadboard. A solder breadboard has the same layout only it has only through-hole solder points on a PCB.

A breadboard allows you to create prototypes for your circuits or simply temporary circuits without having to spend the time (and cost) to make the printed circuit board. Prototyping is the process of experimenting with a circuit by building and testing your ideas. In fact, once you’ve got your circuit to work correctly, you can use the breadboard layout to help you design a PCB. Figure 8-24 shows a number of breadboards.

A416714_1_En_8_Fig24_HTML.jpg
Figure 8-24. Assorted breadboards

Most breadboards (there are several varieties) have a center groove (called a ravine) or a printed line down the center of the board. This signifies the terminal strips that run perpendicular to the channel are not connected. That is, the terminal strip on one side is not connected to the other side. This allows you to plug integrated circuits (IC) or chip that are packaged as two rows of pins. Thus, you can plug the IC into the breadboard with one set of pins on each side of the breadboard. You see this in the following example.

Most breadboards also have one or more sets of power rails that are connected together parallel to the ravine. If there are two sets, the sets are not connected together. The power rails may have a colored reference line but this is only for reference; you can make either one positive with the other negative. Finally, some breadboards number the terminal strip rows. These are for reference only and have no other meaning. However, they can be handy for making notes in your engineering notebook. Figure 8-25 shows the nomenclature of a breadboard and how the terminal strips and power rails are connected together.

A416714_1_En_8_Fig25_HTML.jpg
Figure 8-25. Breadboard layout
Note

The sets of power rails are not connected together. If you want to have power on both sides of the breadboard, you must use jumpers to connect them.

It is sometimes desirable to test a circuit out separately from code. For example, if you want to make sure that all of your devices are connected together properly, you can use a breadboard power supply to power the circuit. This way, if something goes horribly wrong, you don’t risk damaging your IoT device. Most breadboard power supplies are built on a small PCB with a barrel jack for a wall wart power supply, two sets of pins to plug into the power rails on the breadboard, an off switch (very handy), and some can generate different voltages. Figure 8-26 shows one of my favorite breadboard power supplies from SparkFun ( www.sparkfun.com/products/13157 ).

A416714_1_En_8_Fig26_HTML.jpg
Figure 8-26. Breadboard power supply

Should your circuits require more room than what is available on a single breadboard, you can use multiple breadboards by simply jumping the power rails and continuing the circuit. To facilitate this, some breadboards can be connected together using small nubs and slots on the side. Finally, most breadboards also come with an adhesive backing that you can use to mount on a plate or inside an enclosure or similar workspace. If you decide to use the adhesive backing, be forewarned that they cannot be unstuck easily—they stay put quite nicely.

Now that you know more about how breadboards work, let’s discuss the component your IoT solutions employ to collect data: sensors.

What Are Sensors ?

A sensor is a device that measures phenomena of the physical world. These phenomena can be things you see, like light, gases, water vapor, and so on. They can also be things you feel, like temperature, electricity,5 water, wind, and so on. Humans have senses that act like sensors, allowing you to experience the world around you. However, there are some things your body can’t see or feel, such as radiation, radio waves, voltage, and amperage. Upon measuring these phenomena, it’s the sensors’ job to convey a measurement in the form of either a voltage representation or a number.

There are many forms of sensors. They’re typically low-cost devices designed for a single purpose and with a limited capability for processing. Most simple sensors are discrete components; even those that have more sophisticated parts can be treated as separate components. Sensors are either analog or digital and are typically designed to measure only one thing. But an increasing number of sensor modules are designed to measure a set of related phenomena, such as the USB weather board from SparkFun Electronics ( www.sparkfun.com/products/10586 ).

The following sections examine how sensors measure data, how to store that data, and examples of some common sensors.

How Sensors Measure

Sensors are electronic devices that generate a voltage based on the unique properties of their chemical and mechanical construction. They don’t actually manipulate the phenomena they’re designed to measure. Rather, sensors sample some physical variable and turn it into a proportional electric signal (voltage, current, digital, and so on).

For example, a humidity sensor measures the concentration of water (moisture) in the air. Humidity sensors react to these phenomena and generate a voltage that the microcontroller or similar device can then read and use to calculate a value on a scale. A basic, low-cost humidity sensor is the DHT-22 available from most electronic stores.

The DHT-22 is designed to measure temperature as well as humidity. It generates a digital signal on the output (data pin). Although simple to use, it’s a bit slow and should be used to track data at a reasonably slow rate (no more frequently than about once every 3 or 4 seconds).

When this sensor generates data, that data is transmitted as a series of high (interpreted as a 1) and low (interpreted as a 0) voltages that the microcontroller can read and use to form a value. In this case, the microcontroller reads a value that is 40 bits in length (40 pulses of high or low voltage)—that is, 5 bytes—from the sensor and places it in a program variable. The first two bytes are the value for humidity, the second two are for temperature, and the fifth byte is the checksum value to ensure an accurate read. Fortunately, all of this hard work is done for you in the form of a special library designed for the DHT-22 and similar sensors.

The DHT-22 produces a digital value. Not all sensors do this; some generate a voltage range instead. These are called analog sensors. Let’s take a moment to understand the differences. This becomes essential information as you plan and build your sensor nodes.

Analog Sensors

Analog sensors are devices that generate a voltage range, typically between 0 and 5 volts. An analog-to-digital circuit is needed to convert the voltage to a number. But it isn’t that simple (is it ever?). Analog sensors work like resistors and, when connected to GPIO pins, often require another resistor to “pull up” or “pull down” the voltage to avoid spurious changes in voltage known as floating. This is because voltage flowing through resistors is continuous in both time and amplitude.

Thus, even when the sensor isn’t generating a value or measurement, there is still a flow of voltage through the sensor that can cause spurious readings. Your projects require a clear distinction between OFF (zero voltage) or ON (positive voltage). Pull-up and pull-down resistors ensure that you have one of these two states. It’s the responsibility of the A/D converter to take the voltage read from the sensor and converts it to a value that can be interpreted as data.

When sampled (when a value is read from a sensor), the voltage read must be interpreted as a value in the range specified for the given sensor. Remember that a value of, say, 2 volts from one analog sensor may not mean the same thing as 2 volts from another analog sensor . Each sensor’s data sheet shows you how to interpret these values .

As you can see, working with analog sensors is a lot more complicated than using the DHT-22 digital sensor. With a little practice, you find that most analog sensors aren’t difficult to use once you understand how to attach them to a microcontroller and how to interpret their voltage on the scale in which the sensor is calibrated to work.

Digital Sensors

Digital sensors like the DHT-22 are designed to produce a string of bits using serial transmission (one bit at a time). However, some digital sensors produce data via parallel transmission (one or more bytes6 at a time). As described previously, the bits are represented as voltage, where high voltage (say, 5 volts) or ON is 1 and low voltage (0 or even -5 volts) or OFF is 0. These sequences of ON and OFF values are called discrete valuesbecause the sensor is producing one or the other in pulses—it’s either ON or OFF.

Digital sensors can be sampled more frequently than analog signals because they generate the data more quickly and because no additional circuitry is needed to read the values (such as A/D converters and logic or software to convert the values to a scale). As a result, digital sensors are generally more accurate and reliable than analog sensors. But the accuracy of a digital sensor is directly proportional to the number of bits it uses for sampling data.

The most common form of digital sensor is the pushbutton or switch. What, a button is a sensor? Why, yes, it’s a sensor. Consider for a moment the sensor attached to a window in a home security system. It’s a simple switch that is closed when the window is closed and open when the window is open. When the switch is wired into a circuit, the flow of current is constant and unbroken (measuring positive volts using a pull-up resistor) when the window is closed and the switch is closed, but the current is broken (measuring zero volts) when the window and switch is open. This is the most basic of ON and OFF sensors.

Most digital sensors are actually small circuits of several components designed to generate digital data. Unlike analog sensors, reading their data is easy because the values can be used directly without conversion (except to other scales or units of measure). Some may suggest this is more difficult than using analog sensors, but that depends on your point of view. An electronics enthusiast would see working with analog sensors as easier, whereas a programmer would think digital sensors are simpler to use.

Now let’s take a look at some of the sensors available and the types of phenomena they measure.

Examples of Sensors

An IoT solution that observes something may use at least one sensor and a means to read and interpret the data. You may be thinking of all manner of useful things you can measure in your home or office, or even in your yard or surroundings. You may want to measure the temperature changes in your new sun room, detect when the mail carrier has tossed the latest circular in your mailbox, or perhaps keep a log of how many times your dog uses his doggy door. I hope that by now you can see these are just the tip of the iceberg when it comes to imagining what you can measure.

What types of sensors are available? The following list describes some of the more popular sensors and what they measure. This is just a sampling of what is available. Perusing the catalogs of online electronics vendors like Mouser Electronics ( www.mouser.com ), SparkFun Electronics ( www.sparkfun.com ) and Adafruit Industries ( www.adafruit.com ) reveal many more examples.

  • Accelerometers: These sensors measure motion or movement of the sensor or whatever it’s attached to. They’re designed to sense motion (velocity, inclination, vibration, and so on) on several axes. Some include gyroscopic features. Most are digital sensors. A Wii Nunchuck (or WiiChuck) contains a sophisticated accelerometer for tracking movement. Aha: now you know the secret of those funny little thingamabobs that came with your Wii.

  • Audio sensors: Perhaps this is obvious, but microphones are used to measure sound. Most are analog, but some of the better security and surveillance sensors have digital variants for higher compression of transmitted data.

  • Barcode readers: These sensors are designed to read barcodes. Most often, barcode readers generate digital data representing the numeric equivalent of a barcode. Such sensors are often used in inventory-tracking systems to track equipment through a plant or during transport. They’re plentiful, and many are economically priced, enabling you to incorporate them into your own projects.

  • RFID sensors: Radio frequency identification uses a passive device (sometimes called an RFID tag) to communicate data using radio frequencies through electromagnetic induction. For example, an RFID tag can be a credit-card-sized plastic card, a label, or something similar that contains a special antenna, typically in the form of a coil, thin wire, or foil layer that is tuned to a specific frequency. When the tag is placed in close proximity to the reader, the reader emits a radio signal; the tag can use the electromagnet energy to transmit a nonvolatile message embedded in the antenna, in the form of radio signals, which is then converted to an alphanumeric string.7

  • Biometric sensors: A sensor that reads fingerprints, irises, or palm prints contains a special sensor designed to recognize patterns. Given the uniqueness inherit in patterns, such as fingerprints and palm prints, they make excellent components for a secure access system. Most biometric sensors produce a block of digital data that represents the fingerprint or palm print.

  • Capacitive sensors: A special application of capacitive sensors, pulse sensors are designed to measure your pulse rate and typically use a fingertip for the sensing site. Special devices known as pulse oximeters (called pulse-ox by some medical professionals) measure pulse rate with a capacitive sensor and determine the oxygen content of blood with a light sensor. If you own modern electronic devices, you may have encountered touch-sensitive buttons that use special capacitive sensors to detect touch and pressure.

  • Coin sensors: This is one of the most unusual types of sensors.8 These devices are like the coin slots on a typical vending machine. Like their commercial equivalent, they can be calibrated to sense when a certain size of coin is inserted. Although not as sophisticated as commercial units that can distinguish fake coins from real ones, coin sensors can be used to add a new dimension to your projects. Imagine a coin-operated Wi-Fi station. Now, that should keep the kids from spending too much time on the Internet!

  • Current sensors: These are designed to measure voltage and amperage. Some are designed to measure change, whereas others measure load.

  • Flex/Force sensors: Resistance sensors measure flexes in a piece of material or the force or impact of pressure on the sensor. Flex sensors may be useful for measuring torsional effects or as a means to measure finger movements (like in a Nintendo Power Glove). Flex-sensor resistance increases when the sensor is flexed.

  • Gas sensors: There are a great many types of gas sensors. Some measure potentially harmful gases, such as LPG and methane, and other gases, such as hydrogen, oxygen, and so on. Other gas sensors are combined with light sensors to sense smoke or pollutants in the air. The next time you hear that telltale and often annoying low-battery warning beep9 from your smoke detector, think about what that device contains. Why, it’s a sensor node!

  • Light sensors: Sensors that measure the intensity or lack of light are special types of resistors: light-dependent resistors (LDRs) , sometimes called photo resistors or photocells. Thus, they’re analog by nature. If you own a Mac laptop, chances are you’ve seen a photo resistor in action when your illuminated keyboard turns itself on in low light. Special forms of light sensors can detect other light spectrums, such as infrared (as in older TV remotes).

  • Liquid-flow sensors: These sensors resemble valves and are placed in-line in plumbing systems. They measure the flow of liquid as it passes through. Basic flow sensors use a spinning wheel and a magnet to generate a Hall effect (rapid ON/OFF sequences whose frequency equates to how much water has passed).

  • Liquid-level sensors: A special resistive solid-state device can be used to measure the relative height of a body of water. One example generates low resistance when the water level is high and higher resistance when the level is low.

  • Location sensors: Modern smartphones have GPS sensors for sensing location, and of course GPS devices use the GPS technology to help you navigate. Fortunately, GPS sensors are available in low-cost forms, enabling you to add location sensing to your project. GPS sensors generate digital data in the form of longitude and latitude, but some can also sense altitude.

  • Magnetic-stripe readers: These sensors read data from magnetic stripes (like that on a credit card) and return the digital form of the alphanumeric data (the actual strings).

  • Magnetometers: These sensors measure orientation via the strength of magnetic fields. A compass is a sensor for finding magnetic north. Some magnetometers offer multiple axes to allow even finer detection of magnetic fields.

  • Proximity sensors: Often thought of as distance sensors, proximity sensors use infrared or sound waves to detect distance or the range to/from an object. Made popular by low-cost robotics kits, the Parallax Ultrasonic Sensor uses sound waves to measure distance by sensing the amount of time between pulse sent and pulse received (the echo). For approximate distance measuring,10 it’s a simple math problem to convert the time to distance. How cool is that?

  • Radiation sensors: Among the more serious sensors are those that detect radiation. This can also be electromagnetic radiation (there are sensors for that too), but a Geiger counter uses radiation sensors to detect harmful ionizing. In fact, it’s possible to build your very own Geiger counter using a sensor and an Arduino (and a few electronic components).

  • Speed sensors: Similar to flow sensors, simple speed sensors like those found on many bicycles use a magnet and a reed switch to generate a Hall effect. The frequency combined with the circumference of the wheel can be used to calculate speed and, over time, distance traveled. The speed sensor on the wheel and fork provides the data for the monitor on your handlebars.

  • Switches and pushbuttons: These are the most basic of digital sensors used to detect if something is set (ON) or reset (OFF).

  • Tilt switches: These sensors can detect when a device is tilted one way or another. Although very simple, they can be useful for low-cost motion-detection sensors. They are digital and are essentially switches.

  • Touch sensors: The touch-sensitive membranes formed into keypads, keyboards, pointing devices, and the like are an interesting form of sensor. You can use touch-sensitive devices like these for collecting data from humans.

  • Video sensors: As mentioned previously, it’s possible to obtain very small video sensors that use cameras and circuitry to capture images and transmit them as digital data.

  • Weather sensors: Sensors for temperature, barometric pressure, rain fall, humidity, wind speed, and so on are all classified as weather sensors. Most generate digital data and can be combined to create comprehensive environmental solutions. Yes, it’s possible to build your own weather station from about a dozen inexpensive sensors, an Arduino (or a Raspberry Pi), and a bit of programming to interpret and combine the data.

Summary

Learning how to work with electronics as a hobby or as a means to create an IoT solution does not require a lifetime of study or a change of vocation. Indeed, learning how to work with electronics is all part of the fun of experimenting with the IoT!

As you’ve seen in this chapter, knowing about the types of components available, the types of sensors, and a bit of key knowledge of how to use a multimeter goes a long ways toward becoming proficient with electronics. You also learned about one of the key components of an IoT solution—sensors. You discovered two ways they communicate and a bit of what types of sensors are available.

In the next chapter, you take a look at a couple of special kits for experimenting with IoT solutions. As you will see, the kits provide a set of electronic components as well as several tools and accessories, such as a breadboard and a power supply.

Footnotes

2 Older multimeters have an analog gauge. You can still find them if you want a bit of old school feel.

3 Yes, a bit of OCD there. Check, double check, check again.

4 A variant of Ohm’s law ( https://en.wikipedia.org/wiki/Ohm's_law ).

5 Shocking, isn’t it?

6 This depends on the width of the parallel buffer. An 8-bit buffer can communicate 1 byte at a time; a 16-bit buffer can communicate 2 bytes at a time, and so on.

9 I for one can never tell which detector is beeping, so I replace the batteries in all of them.

10 Accuracy may depend on environmental variables, such as elevation, temperature, and so on.

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

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