Sams Teach Yourself Arduino Programming In 24 Hours

Transcription

Richard BlumArduino Programming

Sams Teach Yourself Arduino Programming in 24 HoursCopyright 2015 by Pearson Education, Inc.All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, ortransmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, withoutwritten permission from the publisher. No patent liability is assumed with respect to the use ofthe information contained herein. Although every precaution has been taken in the preparation ofthis book, the publisher and author assume no responsibility for errors or omissions. Nor is anyliability assumed for damages resulting from the use of the information contained herein.ISBN-13: 978-0-672-33712-3ISBN-10: 0-672-337126Library of Congress Control Number: 2013955616Printed in the United States of AmericaFirst Printing: September 2014TrademarksAll terms mentioned in this book that are known to be trademarks or service marks have beenappropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Useof a term in this book should not be regarded as affecting the validity of any trademark or servicemark.Arduino is a registered trademark of Arduino and its partners.Warning and DisclaimerEvery effort has been made to make this book as complete and as accurate as possible, but nowarranty or fitness is implied. The information provided is on an “as is” basis. The author and thepublisher shall have neither liability nor responsibility to any person or entity with respect to anyloss or damages arising from the information contained in this book.Special SalesFor information about buying this title in bulk quantities, or for special sales opportunities (whichmay include electronic versions; custom cover designs; and content particular to your business,training goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419.For government sales inquiries, please contact governmentsales@pearsoned.com.For questions about sales outside the U.S., please contact international@pearsoned.com.Editor-in-ChiefGreg WiegandExecutive EditorRick KughenDevelopmentEditorKeith ClineManaging EditorKristy HartProject EditorAndy BeasterCopy EditorKeith ClineIndexerCheryl LenserProofreaderSarah KearnsTechnical EditorJason FosterPublishingCoordinatorKristen WattersonCover DesignerMark ShirarCompositorNonie Ratcliff

Contents at a GlanceIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Part I: The Arduino Programming EnvironmentHOUR 1 Introduction to the Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Creating an Arduino Programming Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Using the Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 Creating an Arduino Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Part II: The C Programming LanguageHOUR 5 Learning the Basics of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756 Structured Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897 Programming Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1199 Implementing Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13310 Creating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14711 Pointing to Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16312 Storing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18113 Using Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Part III: Arduino ApplicationsHOUR 14 Working with Digital Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21915 Interfacing with Analog Devices.23516 Adding Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25117 Communicating with Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26718 Using Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28719 Working with Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30320 Using an LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31921 Working with the Ethernet Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33722 Advanced Network Programming.35523 Handling Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37324 Prototyping Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403

Table of ContentsIntroduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 1: Introduction to the Arduino .What Is an Arduino? . . . . . . . . .Introducing the Arduino Family .Exploring Arduino Shields . . . . .Summary . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . .1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .711182020. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 2: Creating an Arduino Programming Environment .Exploring Microcontroller Internals . . . . . . . . . . . . .Moving Beyond Machine Code . . . . . . . . . . . . . . . . .Creating Arduino Programs . . . . . . . . . . . . . . . . . . .Installing the Arduino IDE . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232729323738. . . . . . . . . . . . . . . . . .39. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3940494951525454. . . . . . . . . . . . . . . . . . . . . . . . . . . . .57. . . . . . . . . . . . . . . . . . . . . . . . . . . . .5759647071HOUR 3: Using the Arduino IDE . . . . . . . . . . . . . . . . . .Overview of the IDE . . . . . . . . . . . . . . . . . . . . . .Walking Through the Menus . . . . . . . . . . . . . . . .Exploring the Toolbar . . . . . . . . . . . . . . . . . . . . .Exploring the Message Area and Console Window .Setting Up the Arduino IDE . . . . . . . . . . . . . . . . .Using the Serial Monitor . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 4: Creating an Arduino Program .Building an Arduino Sketch . . . . . .Creating Your First Sketch . . . . . . .Interfacing with Electronic Circuits .Summary . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ContentsHOUR 5: Learning the Basics of CWorking with Variables . . . .Using Operators . . . . . . . . .Exploring Arduino FunctionsSummary . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7580838787. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 6: Structured Commands . . . . . . . . .Working with the if Statement . . . . . .Grouping Multiple Statements . . . . . . .Using else Statements . . . . . . . . . . . .Using else if Statements . . . . . . . . .Understanding Comparison ConditionsCreating Compound Conditions . . . . . .Negating a Condition Check . . . . . . . .Expanding with the switch Statement .Summary . . . . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . .HOUR 7: Programming Loops . .Understanding Loops. . . . .Using while Loops . . . . . .Using do-while Loops . . .Using for Loops . . . . . . . .Using Arrays in Your LoopsUsing Multiple Variables . .Nesting Loops . . . . . . . . . .Controlling Loops . . . . . . .Summary . . . . . . . . . . . . .Workshop. . . . . . . . . . . . .v. . . . . . . . . . . . . . . . . . . . . . . . . .89. . . . . . . . . . . . . . . . . . . . . . . . .8990. 92. 93. 95. 97. 98. 98. 99100. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103104106107109112112113116116. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 8: Working with Strings . . . . . . . . . . .What’s a String? . . . . . . . . . . . . . . . . .Understanding C-Style Strings . . . . . . .Introducing the Arduino String ObjectManipulating String Objects . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119. . . . . . . . . . . . . . . . . . . . . . . . .119120126130131132. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

viSams Teach Yourself Arduino Programming in 24 HoursHOUR 9: Implementing Data Structures .What’s a Data Structure? . . . . . . . .Creating Data Structures . . . . . . . .Using Data Structures . . . . . . . . . .Manipulating Data Structures . . . .Arrays of Structures . . . . . . . . . . . .Working with Unions . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . .HOUR 10: Creating Functions . . . . . .Basic Function Use . . . . . . . . . .Returning a Value . . . . . . . . . . .Passing Values to Functions . . . .Handling Variables in FunctionsCalling Functions Recursively . . .Summary . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133134136138140142145145. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147150152154158160160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 11: Pointing to Data . . . . . . . . . .What Is a Pointer? . . . . . . . . . . . .Working with Pointers . . . . . . . . . .Using Special Types of Pointers. . . .Pointer Arithmetic . . . . . . . . . . . . .Strings and Pointers . . . . . . . . . . .Combining Pointers and Structures .Using Pointers with Functions . . . .Summary . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . .HOUR 12: Storing Data . . . . . . . . . .Arduino Memory Refresher . . .Taking a Closer Look at SRAM .Creating Dynamic Variables . .Using Flash to Store Data. . . . .Using the EEPROM Memory . . .Summary . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163166167168171173176179179. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181183185189194198198. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ContentsHOUR 13: Using Libraries . . . . . .What Is a Library? . . . . . . .Using the Standard LibrariesUsing Contributed Libraries .Creating Your Own LibrariesSummary . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . .vii. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201203206208214215. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 14: Working with Digital InterfacesDigital Overview . . . . . . . . . . . . . . .Using Digital Outputs . . . . . . . . . . .Experimenting with Digital Output .Working with Digital Inputs . . . . . .Experimenting with Digital Input . . .Summary . . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219. . . . . . . . . . . . . . . . . . . . . . . . . . .219221223226229231232. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 15: Interfacing with Analog Devices .Analog Overview . . . . . . . . . . . . . . .Working with Analog Input . . . . . . . .Modifying the Input Result . . . . . . . .Using Input Mapping . . . . . . . . . . . .Changing the Reference Voltage. . . . .Analog Output . . . . . . . . . . . . . . . . .Using the Analog Output . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . .HOUR 16: Adding Interrupts . . . . . .What Are Interrupts? . . . . . . . .Types of Interrupts . . . . . . . . .Using External Interrupts . . . . .Testing External Interrupts . . . .Using Pin Change Interrupts . .Working with Timer Interrupts .Ignoring Interrupts . . . . . . . . .Summary . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235238241242245246246248248. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251252254255260262264265265. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

viiiSams Teach Yourself Arduino Programming in 24 HoursHOUR 17: Communicating with Devices .Serial Communication Protocols . . .Using the Serial Port . . . . . . . . . . .Working with the SPI Port . . . . . . .Working with I2C . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267268274277284284. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287288293295296297298300301. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303305308313317317HOUR 18: Using Sensors . . . . . . . . . .Interfacing with Analog Sensors .Working with Voltage . . . . . . . .Using a Voltage-Based Sensor. . .Working with Resistance OutputUsing a Resistance-Based Sensor .Using Touch Sensors . . . . . . . . .Working with Touch Sensors . . .Summary . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . .HOUR 19: Working with Motors .Types of Motors . . . . . . . .Using DC Motors . . . . . . . .Experimenting with MotorsUsing Servo Motors . . . . . .Summary . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 20: Using an LCD . . . . . . . .What Is an LCD? . . . . . . . . .Interfacing with LCD Devices .The LiquidCrystal Library . . .The LCD Shield . . . . . . . . . . .Summary . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319321325329335335. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 21: Working with the Ethernet Shield .Connecting the Arduino to a Network .The Ethernet Shield Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337. . . . . . . . . . . . . . . . . . . . . . . . .337340. . . . . . . . . . . . . . . . . . . . . . . . .

ContentsWriting a Network Program .Summary . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 22: Advanced Network Programming . .The Web Protocol . . . . . . . . . . . . . . . . .Reading Sensor Data from a Web Server .Controlling an Arduino from the Web . .Summary . . . . . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . . . . .HOUR 23: Handling Files . . . . . . .What Is an SD Card Reader?SD Cards and the Arduino . .The SD Library . . . . . . . . . .Interfacing with the SD CardStoring Sensor Data . . . . . . .Summary . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . .349351352. . . . . . . . . . . . . . . . . . . . . . . .355. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355361364370370. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373375376378382385385. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .HOUR 24: Prototyping Projects . . . . . . . .Determining Project Requirements . .Determining Interface Requirements .Listing Components . . . . . . . . . . . .Creating a Schematic . . . . . . . . . . .Creating the Breadboard Circuit . . . .Designing the Sketch . . . . . . . . . . . .Writing the Sketch . . . . . . . . . . . . .Testing the Sketch . . . . . . . . . . . . . .Creating a Prototype Board . . . . . . .Summary . . . . . . . . . . . . . . . . . . . .Workshop. . . . . . . . . . . . . . . . . . . .Indexix. . . . . . . . . . . . . . . . . . . . . . . . . . .387. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .387389391392393394395398399401401. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

About the AuthorRichard Blum has worked in the IT industry for more than 25 years as a network andsystems administrator, managing Microsoft, UNIX, Linux, and Novell servers for a networkwith more than 3,500 users. He has developed and teaches programming and Linux coursesvia the Internet to colleges and universities worldwide. Rich has a master’s degree in management information systems from Purdue University and is the author of several programming books, including Teach Yourself Python Programming for the Raspberry Pi in 24 Hours(coauthored with Christine Bresnahan, 2013, Sams Publishing), Linux Command Line andShell Scripting Bible (coauthored with Christine Bresnahan, 2011, Wiley), Professional LinuxProgramming (coauthored with Jon Masters, 2007, Wiley), and Professional Assembly Language(2005, Wrox). When he’s not busy being a computer nerd, Rich enjoys spending time withhis wife, Barbara, and two daughters, Katie Jane and Jessica.

DedicationTo my Uncle George.Thanks for all your mentoring and troubleshooting help in my earlyelectronics projects. I never would have gotten started in my careerhad those projects not worked!“Iron sharpens iron, and one man sharpens another.” —Proverbs 27:17 (ESV)AcknowledgmentsFirst, all glory and praise go to God, who through His Son, Jesus Christ, makes all thingspossible and gives us the gift of eternal life.Many thanks go to the fantastic team of people at Sams Publishing for their outstandingwork on this project. Thanks to Rick Kughen, the executive editor, for offering us the opportunity to work on this book and keeping things on track, and to Andrew Beaster for all hisproduction work. I would also like to thank Carole Jelen at Waterside Productions, Inc., forarranging this opportunity and for helping out in my writing career.I am indebted to the technical editor, Jason Foster, who put in many long hours doublechecking all the work and keeping the book technically accurate, all while getting a newjob, having a new baby (congrats!), and moving to a new house in another state. His suggestions and eagle eyes have made this a much better book.Finally I’d like to thank my wife, Barbara, and two daughters, Katie Jane and Jessica, fortheir patience and support while I was writing this.

We Want to Hear from You!As the reader of this book, you are our most important critic and commentator. We valueyour opinion and want to know what we’re doing right, what we could do better, whatareas you’d like to see us publish in, and any other words of wisdom you’re willing to passour way.We welcome your comments. You can email or write to let us know what you did or didn’tlike about this book—as well as what we can do to make our books better.Please note that we cannot help you with technical problems related to the topic ofthis book.When you write, please be sure to include this book’s title and author as well as your nameand email address. We will carefully review your comments and share them with the authorand editors who worked on the book.Email:consumer@samspublishing.comMail:Sams PublishingATTN: Reader Feedback800 East 96th StreetIndianapolis, IN 46240 USAReader ServicesVisit our website and register this book at informit.com/register for convenient access to anyupdates, downloads, or errata that might be available for this book.

IntroductionSince being introduced in 2005 as a student project, the Arduino microcontroller has quicklybecome a favorite of both hobbyists and professionals. It’s a popular platform for creating manydifferent types of automated systems—from monitoring water levels in house plants

via the Internet to colleges and universities worldwide. Rich has a master’s degree in man-agement information systems from Purdue University and is the author of several program-ming books, including Teach Yourself