Programming Arduino
Getting Started with Sketches
SECOND EDITION
Simon Monk
New York Chicago San Francisco
Athens London Madrid
Mexico City Milan New Delhi
Singapore Sydney Toronto
Library of Congress Control Number: 2016938700
McGraw-Hill Education books are available at special quantity discounts to use as premiums and sales promotions or for use in corporate training programs. To contact a representative, please visit the Contact Us page at www.mheducation.com.
Programming Arduino: Getting Started with Sketches, Second Edition
Copyright 2016, 2012 by McGraw-Hill Education. All rights reserved. Printed in the United States of America. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication.
McGraw-Hill Education, the McGraw-Hill Education logo, TAB, and related trade dress are trademarks or registered trademarks of McGraw-Hill Education and/or its affiliates in the United States and other countries and may not be used without written permission. All other trademarks are the property of their respective owners. McGraw-Hill Education is not associated with any product or vendor mentioned in this book.
Arduino is a trademark of Arduino LLC.
1 2 3 4 5 6 7 8 9 DOC 21 20 19 18 17 16
ISBN 978-1-25-964163-3
MHID 1-25-964163-5
This book is printed on acid-free paper.
Sponsoring Editor
Michael McCabe
Editorial Supervisor
Stephen M. Smith
Production Supervisor
Pamela A. Pelton
Acquisitions Coordinator
Lauren Rogers
Project Manager
Srishti Malasi,
Cenveo Publisher Services
Copy Editor
Claire Splan
Proofreader
Cenveo Publisher Services
Indexer
Jack Lewis
Art Director, Cover
Jeff Weeks
Illustration
Cenveo Publisher Services
Composition
Cenveo Publisher Services
Information has been obtained by McGraw-Hill Education from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill Education, or others, McGraw-Hill Education does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information.
To my boys, Stephen and Matthew, from a very proud Dad.
About the Author
Simon Monk has a bachelors degree in cybernetics and computer science and a doctorate in software engineering. He has been an active electronics hobbyist since his school days and is an occasional author in hobby electronics magazines. Dr. Monk is also author of some 20 books on Maker and electronics topics, especially Arduino and Raspberry Pi. You can find out more about his books at http://simonmonk.org. You can also follow him on Twitter, where he is @simonmonk2.
CONTENTS
PREFACE
The first edition of this book was published in November 2011 and has been Amazons highest ranking book on Arduino.
At the time the book was originally written, the current Arduino model was the Arduino 2009 and the software version was Beta018. Almost at the time the book arrived in stores, the Arduino Uno and version 1.0 of the Arduino software were released. Soon after, the second printing of the book had a minor update to cover the new board and software without formally being a second edition. This edition brings the book fully up to date and is based on Arduino 1.6.
The Arduino Uno R3 is still considered to be the standard Arduino board. However, many other boards, including both official Arduino boards (like the Leonardo, Zero, 101, Due, and Yun) and other Arduino programming languagebased devices like the Photon and Intel Edison, have also appeared.
This edition also addresses the use of Arduino in IoT (Internet of Things) projects and the use of various types of display including OLED and LCD.
Simon Monk
ACKNOWLEDGMENTS
I thank Linda for giving me the time, space, and support to write this book and for putting up with the various messes my projects create around the house.
Many thanks to Robert BobKat Logan and the many other eagle-eyed and helpful folk who reported errata for the first edition. I have done my best to fix what you found.
Finally, I would like to thank Michael McCabe, Srishti Malasi, and everyone involved in the production of this book. Its a pleasure to work with such a great team.
INTRODUCTION
Arduino interface boards provide a low-cost, easy-to-use technology to create microcontroller-based projects. With a little electronics, you can make your Arduino do all sorts of things, from controlling lights in an art installation to managing the power on a solar energy system.
There are many project-based books that show you how to connect things to your Arduino, including 30 Arduino Projects for the Evil Genius by this author. However, the focus of this book is on programming the Arduino.
This book will explain how to make programming the Arduino simple and enjoyable, avoiding the difficulties of uncooperative code that so often afflict a project. You will be taken through the process of programming the Arduino step by step, starting with the basics of the C programming language that Arduinos use.
So, What Is Arduino?
Arduino is a small microcontroller board with a universal serial bus (USB) plug to connect to your computer and a number of connection sockets that can be wired to external electronics such as motors, relays, light sensors, laser diodes, loudspeakers, microphones, and more. They can be powered either through the USB connection from the computer, from a 9V battery, or from a power supply. They can be controlled from the computer or programmed by the computer and then disconnected and allowed to work independently.
The board design is open source. This means that anyone is allowed to make Arduino-compatible boards. This competition has led to low costs for the boards and all sorts of variations on the standard boards.
The basic boards are supplemented by accessory shield boards that can be plugged on top of the Arduino board.
The software for programming your Arduino is easy to use and also freely available for Windows, Mac, and Linux computers.
What Will I Need?
This is a book intended for beginners, but it is also intended to be useful to those who have used Arduino for a while and want to learn more about programming the Arduino or gain a better understanding of the fundamentals. As such, this book concentrates on the use of the Arduino Uno board; however, almost all of the code will work unmodified on all the Arduino models and variants.
You do not need to have any programming experience or a technical background, and the books exercises do not require any soldering. All you need is the desire to make something.
If you want to make the most of the book and try out some of the experiments, then it is useful to have the following on hand: