Electronics Cookbook Practical Electronic Recipes with Arduino and Raspberry Pi Simon Monk Electronics Cookbook by Simon Monk Copyright © 2017 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://oreilly.com/safari). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Editors: Susan Conant and Jeff Bleiel Production Editor: Colleen Lobner Copyeditor: Christina Edwards Proofreader: Kim Cofer Indexer: Judy McConville Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest April 2017: First Edition Revision History for the First Edition 2017-03-29: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491953402 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Electronics Cookbook, the cover image of an elephantnose fish, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility 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-95340-2 [LSI] Preface Traditional wisdom requires people using electronics to have at least an EE degree before they can do anything useful, but in this book the whole subject of electronics is given the highly respected O’Reilly Cookbook treatment and is broken down into recipes. These recipes make it possible for the reader to access the book at random, following the recipe that solves their problem and learning as much or as little about the theory as they are comfortable with. While it is impossible to cover in one volume everything in a complex and wide-ranging subject like electronics, I have tried to select recipes that seem to come up most frequently when I talk to other makers, hobbyists, and inventors. Who Should Read This Book If you are into electronics or want to get into electronics, then this is the book that will help you get more from your hobby. The book is full of built-and-tested recipes that you can trust to do just what you need them to do, no matter what your level of expertise. If you are new to electronics then this book will serve as a guide to get you started; if you are an experienced electronics maker, it will act as a useful reference. Why I Wrote This Book This book has been gestating for a while. I believe that the original concept came from no less a person than Tim O’Reilly himself. The idea was to fill the gap in the market between books like the Arduino Cookbook and the Raspberry Pi Cookbook and heavyweight electronics textbooks. In other words, to cover more of the fundamentals of electronics and topics peripheral to the use of microcontrollers that often get neglected, except in heavyweight electronic tomes. Topics such as how to construct various types of power supply, using the right transistor for switching, using analog and digital ICs, as well as how to construct projects and prototypes and use test equipment. A Word on Electronics Today Boards like the Arduino and Raspberry Pi have lured whole new generations of makers, hobbyists, and inventors into the world of electronics. Components and tools are now low cost and within the reach of more people than at any time in history. Hackspaces and Fab Labs have electronic workstations where you can use tools to realize your projects. The free availability of information including detailed designs means that you can learn from and adapt other people’s work for your own specific needs. Many people who start with electronics as a hobby progress to formal education in electronic engineering, or just jump straight to product design as an inventor and entrepreneur. After all, if you have access to a computer and a few tools and components, you can build a working prototype of your great invention and then find someone to manufacture it for you, all financed with the help of crowdfunding. The barrier of entry to the electronics business is at an all-time low. Navigating This Book As a “cookbook” you can dive in and use any recipe, rather than read the book in order. Where you have a recipe that relies on some knowledge or skills from another recipe, there will be a link back to the prerequisite recipe. The recipes are arranged in chapters, with Chapters 1 to 6 providing more fundamental recipes, some concerning theory but mostly about different types of component (your recipe ingredients). These chapters are: Chapter 1, Theory. As the title suggests, the recipes in this chapter provide you with the few theoretical concepts such as Ohm’s Law and the power law you just can’t avoid. Chapter 2, Resistors. These most common of electronic components are explained and recipes provided for some of their uses. Chapter 3, Capacitors and Inductors. Here you will find recipes explaining how these components work, how to identify them, and recipes for making use of them. Chapter 4, Diodes. In this chapter you will find recipes explaining diodes and uses for different types of diode including Zener diodes, photodiodes, and LEDs. Chapter 5, Transistors and Integrated Circuits. This chapter mostly contains fundamental recipes for using transistors and guides for using different types of transistors in different settings. ICs (integrated circuits) are introduced, but you will find individual recipes for ICs scattered throughout the rest of the book. Chapter 6, Switches and Relays. The section ends with a look at these common but often overlooked components. The next section of chapters looks at how the components introduced in the first section can be used together in various recipes covering pretty much anything electronic that you might like to design. Chapter 7, Power Supplies. Whatever your project, you are going to need to provide it with power. You will find recipes here for both traditional power supply designs as well as switched mode power supplies (SMPS) and more exotic high-voltage power supplies. Chapter 8, Batteries. This chapter contains recipes for selecting batteries and also practical circuits for charging batteries (including LiPo batteries) and automatic battery backup. Chapter 9, Solar Power. In this chapter, you will find recipes to help you power your projects using solar panels, including providing solar power to an Arduino and Raspberry Pi. Chapter 10, Arduino and Raspberry Pi. Most Maker projects now include the use of a computing element like an Arduino or Raspberry Pi. These boards are introduced along with some recipes for using them to control external electronics. Chapter 11, Switching. Not to be confused with “switches,” this chapter provides recipes that