Atmospheric Monitoring with Arduino Patrick Di Justo and Emily Gertz Atmospheric Monitoring with Arduino by Patrick Di Justo and Emily Gertz Copyright © 2013 Patrick Di Justo, Emily Gertz. 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://my.safaribooksonline.com). For more infor- mation, contact our corporate/institutional sales department: 800-998-9938 or corpo [email protected]. Editors: Shawn Wallace and Brian Jepson Production Editor: Kara Ebrahim Proofreader: Kara Ebrahim Cover Designer: Mark Paglietti Interior Designer: David Futato Illustrator: Rebecca Demarest November 2012: First Edition Revision History for the First Edition: 2012-11-19 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449338145 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trade- marks of O’Reilly Media, Inc. Atmospheric Monitoring with Arduino and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Me- dia, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-33814-5 [LSI] We dedicate this book to our sisters and brothers: Andy, Lucy, Mathius, and Melissa Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii 1/The World’s Shortest Electronics Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Is Arduino?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Electronic Circuits and Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Programming Arduino. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 First Sketch: Make an LED Blink. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Parts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Install the IDE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Breadboard the Circuit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Write the Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Things to Try. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2/Gadget: Tropospheric Gas Detector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 How Gas Sensors Work. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Which Gases Can We Monitor?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 How This Gadget Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Transistorized!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Build the Gadget. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Load the Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Displaying and Storing Your Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Liquid Crystal Displays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Reading Data Off EEPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Reading Data from an SD Card. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Things to Try. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Other Sensors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Solar Powered. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 GSM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Do Not Deploy Your Gadget in Public Without Official Permission. . . . . . . . . . . . 29 Get Official Permission. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Get Your Community Involved. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3/A Brief Introduction to LEDs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 v What Is a Diode?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 What Is a Light Emitting Diode?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 How Are We Using LEDs in the LED Photometer?. . . . . . . . . . . . . . . . . . . . . . . . 35 4/Gadget: LED Sensitivity Tester. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Mission: Inputtable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Build the Gadget. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 5/Gadget: LED Photometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Build the Gadget. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Load the Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Calibrate the Gadget: Air Mass, Atmospheric Optical Thickness, and Extraterrestrial Constant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Calculating Atmospheric Optical Thickness. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Things to Try. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Detecting “Ozone Holes”: Measuring the Ozone Layer. . . . . . . . . . . . . . . . . . 64 Add an Accelerometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 6/Using the LED Photometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Atmospheric Aerosols. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Photosynthetically Active Radiation (PAR). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Water Vapor (WV). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Extracting Data from the LED Photometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Graphing Data in a Spreadsheet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Sending Data to COSM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 7/Doing Science: How to Learn More from Your Atmospheric Data. . . . . . . . 73 The Scientific Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Steps in the Scientific Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Observe Something in the World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Ask an Answerable Question. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Formulate a Hypothesis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Compare the Predicted to Actual Results, Considering the Results. . . . . . . . 75 Ask Another Question. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 vi Contents Preface There’s a story (it’s either an old vaudeville joke or a Zen koan) in which a fisherman asks a fish, “What’s the water like down there?” and the fish replies “What is water?” If the story is just a joke, the point is to make us laugh; but if it’s a koan, the point is that the most obvious and ubiquitous parts of our immediate environ- ment are, paradoxically, often the easiest to overlook. We as a species are probably a little bit smarter than fish: at least we know that we spend our lives “swimming” at the bottom of an ocean of air. About 4/5th of that ocean is the relatively harmless gas nitrogen. Around another 1/5 of it is the highly reactive and slightly toxic gas oxygen. The Earth’s at- mosphere also contains trace amounts of other harmless or slightly toxic gases like argon, carbon dioxide, and methane. And depending on where you live, it may contain even smaller, but much more toxic, amounts of pollutants like soot, carbon monoxide, and ozone. Yet how many of us, like the fish in the koan, overlook the atmosphere? Who in your life can tell you the general composition of the air around them? How many people know what’s inside every breath they take? Do you? Reading this book and building these gadgets will take you on the first steps of a jour- ney toward understanding our ocean of air. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file exten- sions. vii Constant width Used for program listings, as well as within paragraphs to refer to pro- gram elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by val- ues determined by context. This icon signifies a tip, suggestion, or general note. This icon indicates a warning or caution. Using Code Examples This book is here to help you get your job done. In general, if this book includes code examples, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does re- quire permission. Answering a question by citing this book and quoting ex- ample code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Atmospheric Monitoring with Arduino by Patrick Di Justo and Emily Gertz (O’Reilly). Copyright 2013 Patrick Di Justo and Emily Gertz, 978-1-4493-3814-5.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at [email protected]. Safari® Books Online Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business. viii Preface