Electronics Cookbook Simon Monk Electronics Cookbook by Simon Monk Copyright © 2016 Simon Monk. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( http://safaribooksonline.com ). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected] . Editors: Susan Conant and Jeff Bleiel Proofreader: FILL IN PROOFREADER Production Editor: FILL IN PRODUCTION EDI‐ Indexer: FILL IN INDEXER TOR Interior Designer: David Futato Copyeditor: FILL IN COPYEDITOR Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest December 2016: First Edition Revision History for the First Edition 2016-07-29: First Early Release 2016-10-04: Second Early Release 2016-11-17: Third Early Release See http://oreilly.com/catalog/errata.csp?isbn=9781491953341 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Electronics Cookbook, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author(s) have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibil‐ ity for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-95334-1 [FILL IN] Table of Contents 1. Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.0 Introduction 9 1.1 Understanding Current 9 1.2 Understanding Voltage 11 1.3 Ohm’s Law 12 1.4 Kirchhoff’s Current Law 15 1.5 Kirchhoff’s Voltage Law 16 1.6 Understanding Power 17 1.7 Alternating Current 18 2. Resistors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.0 Introduction 21 2.1 Reading Resistor Packages 21 2.2 Finding Standard Resistor Values 24 2.3 Potentiometers (Variable Resistors) 25 2.4 Resistors in Series 27 2.5 Resistors in Parallel 28 2.6 Voltage Divider 29 2.7 Power Rating 31 2.8 Photoresistors 33 2.9 Thermistor 35 2.10 Wires and Conductors 37 3. Capacitors and Inductors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.0 Introduction 41 3.1 Capacitor Basics 41 3.2 Types of Capacitor 46 3.3 Reading Capacitor Packages 48 iii 3.4 Capacitors in Parallel 49 3.5 Capacitors in Series 50 3.6 Supercapacitors 51 3.7 Energy Stored in a Capacitor 52 3.8 Inductor Basics 53 3.9 Transformers 54 4. Diodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.0 Introduction 59 4.1 Diode Basics 59 4.2 Types of Diode 61 4.3 Zener Diodes 63 4.4 LEDs (Light Emitting Diodes) 65 4.5 Photodiodes 66 5. Transistors and Integrated Circuits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.0 Introduction 69 5.1 Bipolar Junction Transistors 70 5.2 Darlington Transistors 74 5.3 MOSFETs 75 5.4 IGBTs 78 5.5 Choosing the Right Transistor 79 5.6 TRIACs 82 5.7 Phototransistors 83 5.8 Opto-couplers and Opto-isolators 84 5.9 IC Basics 86 6. Switches and Relays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 6.0 Introduction 91 6.1 Switch Basics 91 6.2 Types of Switch 93 6.3 Reed Switches 96 6.4 Relays 97 7. Power Supplies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 7.0 Introduction 99 7.1 AC to AC 100 7.2 Ungregulated AC to DC Power Supplies 102 7.3 Full-wave Recification 104 7.4 AC to Regulated DC 106 7.5 Variable Regulated AC to DC 108 7.6 Voltage Regulation from a Battery Source 110 iv | Table of Contents 7.7 A Constant-Current Power Supply 111 7.8 Efficient DC Voltage Regulation (Switchers) 112 7.9 DC to DC Step-Up (Boost Converters) 113 7.10 Mains Inverters 114 7.11 Mains Switched Mode Power Supply (SMPS) 117 7.12 Voltage Multiplier 118 7.13 High Voltage Supply 450V 120 7.14 Even Higher Voltage Supply (> 1kV) 122 7.15 Very Very High Voltage Supply (Solid-State Tesla Coil) 123 7.16 Fuses 127 7.17 Polarity Protection 129 8. Batteries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 8.0 Introduction 133 8.1 Estimating Battery Life 133 8.2 Selecting a Non-Rechargeable Battery 135 8.3 Selecting a Rechargeable Battery 137 8.4 Trickle Charging 138 8.5 Automatic Battery Backup 139 8.6 Charging LiPo Batteries 141 8.7 Joule Thief 143 9. Solar Power. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 9.0 Introduction 145 9.1 Solar Power Basics 145 9.2 Choosing a Solar Panel 148 9.3 Measuring the Actual Output Power of a Solar Panel 150 9.4 Powering an Arduino with Solar 153 9.5 Powering a Raspberry Pi with Solar 153 10. Arduino and Raspberry Pi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 10.0 Introduction 159 10.1 Arduino 159 10.2 Downloading and Using the Book’s Arduino Sketches 163 10.3 Raspberry Pi 164 10.4 Downloading and Running this Book’s Python Programs 166 10.5 Running a Program on Raspberry Pi on Startup 167 10.6 Other Boards 168 10.7 GPIO (General Purpose IO) Pins 170 10.8 Arduino Digital Ouputs 174 10.9 Raspberry Pi Digital Outputs 175 10.10 Arduino Digital Inputs 176 Table of Contents | v 10.11 Raspberry Pi Digital Inputs 181 10.12 Arduino Analog Inputs 182 10.13 Arduino Analog Output 184 10.14 Raspberry Pi PWM Output 188 10.15 Raspberry Pi I2C (IC to IC Interface) 189 10.16 Raspberry Pi SPI (Serial Programming Interface) 192 10.17 Level Conversion 193 11. Switching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 11.0 Introduction 197 11.1 The BJT as a Switch 197 11.2 High-side Switching using a BJT 199 11.3 The MOSFET as a Switch 201 11.4 High-side Switching with a MOSFET 204 11.5 BJT verses MOSFET 205 11.6 Switching with Arduino 206 11.7 Switching with a Raspberry Pi 211 11.8 Push-Pull Driving 212 11.9 Controlling a Relay from a GPIO Pin 214 11.10 Controlling a Solid State Relay from a GPIO Pin 216 12. Sensors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 12.0 Introduction 219 12.1 Using a Switch 219 12.2 Position Sensing with a Quadrature Encoder 225 12.3 Resistive Sensors (Analog Input) 229 12.4 Adding Analog Inputs to Raspberry Pi 231 12.5 Resistive Sensors (Step Response) 232 12.6 Measuring Light Intensity with a Photoresistor 234 12.7 Measuring Temperature with a Thermistor (Voltage Divider) 235 12.8 Measuring Temperature with a Thermistor (Step Response) 238 12.9 Measuring Rotation using a Potentiometer 239 12.10 Measuring Temperature with a TMP36 241 12.11 Using a DS18B20 Digital Temperature Sensor 243 12.12 Measuring Humidity 247 12.13 Measuring Distance 249 13. Motors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 13.0 Introduction 255 13.1 Switching DC Motors On and Off 255 13.2 Mewasuring the Speed of a DC Motor 256 13.3 Controlling the Direction of a DC Motor 259 vi | Table of Contents 13.4 Servomotors 263 13.5 Bipolar Stepper Motors 269 13.6 Unipolar Stepper Motors 274 14. LEDs and Displays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 14.0 Introduction 279 14.1 Standard LEDs 279 14.2 Driving High-Power LEDs 283 14.3 Powering Lots of LEDs 285 14.4 Switching Lots of LEDs at the Same Time 287 14.5 Multiplexing 7-Segment Displays 288 14.6 Charlieplexing 291 14.7 RGB LEDs 296 14.8 Addressable LED Strips 302 14.9 I2C 7-Segment LED Displays 305 14.10 OLED I2C Displays 309 14.11 Alphanumeric LCD Displays 311 15. Digital ICs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 15.0 Introduction 317 15.1 Decoupling Capacitors 317 15.2 Logic Families 319 15.3 Serial to Parallel Shift Registers 320 15.4 Flip Flops 324 15.5 Frequency Dividers 326 15.6 Decimal Counters 327 16. Analog. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 16.0 Introduction 331 16.1 Low-Pass Filtering (RC) 331 16.2 A Transistor Oscillator 335 16.3 Ring Oscillator 331 16.4 Emitter Follower Buffer 338 16.5 Oscillator Using an NE555 IC (50% Duty Cycle) 339 16.6 Oscillator Using an NE555 IC (Variable Duty Cycle) 341 16.7 One Shot Timer Using an NE555 IC 344 16.8 PWM Motor Speed Control 345 16.9 PWM of an Analog Signal Using a NE555 Timer IC 348 16.10 Voltage Controlled Oscillator (VCO) 350 17. Operational Amplifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 17.0 Introduction 353 Table of Contents | vii 17.1 Selecting an Op-Amp 353 17.2 Powering an Op-Amp (Split Supply) 355 17.3 Powering an Op-amp (Single Supply) 356 17.4 Inverting Amplifier 357 17.5 Non-Inverting Amplifier 359 17.6 Unity Gain Buffer 361 17.7 Active Low-Pass Filter 363 17.8 Active High Pass Filter 366 17.9 Band-Pass Filter 368 17.10 Comparitors 371 18. Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 18.0 Introduction 373 18.1 Decibels 373 18.2 Playing Sounds on Arduino 375 18.3 Playing Sound with a Raspberry Pi 377 18.4 Electret Microphone Pre-amp 378 18.5 Linear Power Amplifier (1W) 383 18.6 Digital Power Amplifier (10W) 385 viii | Table of Contents