Table Of ContentPractical AVR
Microcontrollers
Games, Gadgets, and Home Automation with
the Microcontroller Used in Arduino
Alan Trevennor
Practical AVR Microcontrollers
Copyright © 2012 by Alan Trevennor
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter
developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly
analysis or material supplied specifically for the purpose of being entered and executed on a computer system,
for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only
under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission
for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the
Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN 978-1-4302-4446-2
ISBN 978-1-4302-4447-9 (eBook)
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial
fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither
the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may
be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul Manning
Lead Editor: Michelle Lowman
Developmental Editor: Matthew Moodie
Technical Reviewer: Cliff Wootton
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan
Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie,
Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing,
Matt Wade, Tom Welsh
Coordinating Editor: Jill Balzano
Copy Editor: Lori Jacobs
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science + Business Media New York, 233 Spring Street, 6th
Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348–4505, e-mail o rders-ny@springer-sbm.com ,
or visit w ww.springeronline.com .
For information on translations, please e-mail rights@apress.com , or visit w ww.apress.com .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Special Bulk
Sales–eBook Licensing web page at www.apress.com/bulk-sales .
Any source code or other supplementary materials referenced by the author in this text is available to
readers at w ww.apress.com . For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code .
“To Wendy, who made it all possible.”
Contents at a Glance
Foreword ......................................................................................................................xv
About the Author ........................................................................................................xvii
About the Technical Reviewer .....................................................................................xix
Acknowledgments .......................................................................................................xxi
Introduction ...............................................................................................................xxiii
■ Part 1: The Basics ....................................................................................1
■ Chapter 1: A Brief History of Microcontrollers ............................................................3
■ Chapter 2: Building Our AVR Test Bed .......................................................................13
■ Chapter 3: Arduino and the Naked AVR .....................................................................49
■ Chapter 4: Moving On! ...............................................................................................85
■ Chapter 5: Smarten Up! ...........................................................................................155
■ Chapter 6: Digitally Speaking ..................................................................................167
■ Part 2: The Projects .............................................................................189
■ Chapter 7: Introduction to the Projects Section ......................................................191
■ Chapter 8: Project 1: Good Evening, Mr. Bond: Your Secret Panel ...........................195
■ Chapter 9: Project 2: Crazy Beams—Exercise Your Pet! .........................................213
■ Chapter 10: Project 3: WordDune ............................................................................229
■ Chapter 11: Project 4: The Lighting Waterfall ..........................................................249
■ Chapter 12: Moving to Mesmerize ...........................................................................277
■ Chapter 13: Smart Home Enablers ..........................................................................305
v
■ CONTENTS AT A GLANCE
■ Appendix A: Common Components..........................................................................333
■ Appendix B: A Digital Electronics Primer ................................................................347
■ Appendix C: Breadboards ........................................................................................359
■ Appendix D: Serial Communications .......................................................................365
Index ...........................................................................................................................377
vi
Contents
Foreword ......................................................................................................................xv
About the Author ........................................................................................................xvii
About the Technical Reviewer .....................................................................................xix
Acknowledgments .......................................................................................................xxi
Introduction ...............................................................................................................xxiii
■ Part 1: The Basics ....................................................................................1
■ Chapter 1: A Brief History of Microcontrollers ............................................................3
A Microcontroller Timeline .....................................................................................................4
Why Microcontrollers? ...........................................................................................................5
Why Should You Learn About Microcontrollers? ............................................................................................5
What Can You Do with a Microcontroller? .....................................................................................................5
Why AVR? .......................................................................................................................................................7
Summary ..............................................................................................................................11
Coming Up Next ....................................................................................................................11
■ Chapter 2: Building Our AVR Test Bed .......................................................................13
Test Bed Details ....................................................................................................................13
Test Bed Ingredients .............................................................................................................13
A Desktop Computer ....................................................................................................................................14
Summary List of Other Parts .......................................................................................................................15
Toolkit: Required Items ................................................................................................................................16
Toolkit: Optional Items .................................................................................................................................17
Piece Parts ...................................................................................................................................................18
vii
■ CONTENTS
AVR ISP Programmer ...................................................................................................................................24
Other Programmers .....................................................................................................................................33
AVR ISP Programming Adaptor and Pin Headers .........................................................................................34
Assembling the Programming Adaptor ........................................................................................................35
Test Bed Software—Installation and Setup .........................................................................39
Choosing the Software: Why Arduino? .................................................................................39
Test Bed Testing ...................................................................................................................42
TTL Level Serial Port ............................................................................................................45
Summary ..............................................................................................................................47
Coming Up Next ....................................................................................................................47
■ Chapter 3: Arduino and the Naked AVR .....................................................................49
Comparing Approaches ........................................................................................................49
AVR Pulls Ahead ...........................................................................................................................................52
Go AVR! ........................................................................................................................................................53
Putting All the Pieces in Place ..............................................................................................55
The AVR Type ........................................................................................................................57
Arduino’s Confi guration Files: ......................................................................................................................57
AVR Out of the Box .......................................................................................................................................60
Using an External Crystal ............................................................................................................................61
The AVR and Its Fuses .................................................................................................................................62
AVRDude ...............................................................................................................................66
The Trouble with AVRDude’s Terminal Mode ................................................................................................67
AVRDude: Getting Started ............................................................................................................................67
Arduino and the AVR ID Problem ..........................................................................................76
Pin Name Translations ..........................................................................................................77
AVR: Speak to Me! ................................................................................................................79
Terminal Emulators ..............................................................................................................82
Summary ..............................................................................................................................83
Coming Up Next ....................................................................................................................83
viii
■ CONTENTS
■ Chapter 4: Moving On! ...............................................................................................85
Making Things That Move ....................................................................................................85
The Servo Motor ..........................................................................................................................................85
Giving a Servo Motor a Testing Time ...........................................................................................................91
The Gem Light ...........................................................................................................................................101
Drive On! ....................................................................................................................................................108
MOS-What? ...............................................................................................................................................114
Relays ........................................................................................................................................................117
Solenoids ...................................................................................................................................................121
Motors (Non-Servo) ...................................................................................................................................124
Sensing Movement .............................................................................................................138
Sensor Switches ........................................................................................................................................138
Active Sensors ...........................................................................................................................................144
Summary ............................................................................................................................154
Coming Up Next ..................................................................................................................154
■ Chapter 5: Smarten Up! ...........................................................................................155
Intelligent Devices ..............................................................................................................155
Increased Functionality .............................................................................................................................156
Adaptive Behavior ......................................................................................................................................157
Increased Effi ciency ..................................................................................................................................158
Improved Problem Management ...............................................................................................................159
Capacity for Repurposing ..........................................................................................................................159
Greener Products .......................................................................................................................................164
Support from the Mother Ship ...................................................................................................................164
Embedded Intelligence Benefi ts ................................................................................................................164
Downsides of Intelligent Devices .......................................................................................164
The Anatomy of a Smart Device .........................................................................................165
Summary ............................................................................................................................166
Coming Up Next ..................................................................................................................166
ix
■ CONTENTS
■ Chapter 6: Digitally Speaking ..................................................................................167
When Intelligent Devices Speak .........................................................................................167
Communications Channel Types ................................................................................................................168
Error Checking ...........................................................................................................................................169
Microcontroller KISS Communications ...............................................................................171
The RS-232 Connection .............................................................................................................................172
RS-232 or Direct Logic Level Connection? ................................................................................................174
Designing the Time-Getter Protocol ...................................................................................174
Talking Tokens ...........................................................................................................................................178
Object Models ............................................................................................................................................179
Implementing a Tokenized Object Model on AVR .......................................................................................187
Summary of Object Models and Tokenization............................................................................................188
Summary ............................................................................................................................188
Coming up ..........................................................................................................................188
■ Part 2: The Projects .............................................................................189
■ Chapter 7: Introduction to the Projects Section ......................................................191
Project Bases .....................................................................................................................191
Project Chapter Formats ....................................................................................................192
Project Scope and Diffi culty ...............................................................................................193
■ Chapter 8: Project 1: Good Evening, Mr. Bond: Your Secret Panel ...........................195
A Life Ruined by Movies and TV .........................................................................................195
Making It Real ....................................................................................................................195
The Fireline Fiasco ....................................................................................................................................196
Thinking Again ...........................................................................................................................................197
Racking Up a Success ...............................................................................................................................198
Hiding the Button .......................................................................................................................................201
Position Sensors ........................................................................................................................................203
The Secret Compartment ...........................................................................................................................203
The Electronics ..........................................................................................................................................205
x