Professional Documents
Culture Documents
Programming Arduino Next Steps: Going Further with Sketches 2nd Edition Simon Monk full chapter instant download
Programming Arduino Next Steps: Going Further with Sketches 2nd Edition Simon Monk full chapter instant download
https://ebookmass.com/product/programming-arduino-getting-
started-with-sketches-third-edition-simon-monk/
https://ebookmass.com/product/programming-arduino-getting-
started-with-sketches-3rd-edition-simon-monk/
https://ebookmass.com/product/programming-arduino-getting-
started-with-sketches-tab-monk/
https://ebookmass.com/product/hacking-electronics-learning-
electronics-with-arduino-and-raspberry-pi-2nd-edition-simon-monk/
Programming the Raspberry Pi, Third Edition: Getting
Started with Python Simon Monk
https://ebookmass.com/product/programming-the-raspberry-pi-third-
edition-getting-started-with-python-simon-monk/
https://ebookmass.com/product/programming-the-intel-galileo-
getting-started-with-the-arduino-compatible-development-board-
christopher-rush/
https://ebookmass.com/product/learning-scientific-programming-
with-python-2nd-edition-christian-hill-2/
https://ebookmass.com/product/learning-scientific-programming-
with-python-2nd-edition-christian-hill/
https://ebookmass.com/product/essentials-of-arduino-boards-
programming-step-by-step-guide-to-master-arduino-boards-hardware-
and-software-1st-edition-farzin-asadi-2/
Programming
Arduino®
Next Steps
Next Steps
Going Further with Sketches
Second Edition
Simon Monk
ISBN: 978-1-25-958860-0
MHID: 1-25-958860-2
The material in this eBook also appears in the print version of this title: ISBN: 978-1-25-958859-4,
MHID: 1-25-958859-9.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occur-
rence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner,
with no intention of infringement of the trademark. Where such designations appear in this book, they have been
printed with initial caps.
McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promo-
tions or for use in corporate training programs. To contact a representative, please visit the Contact Us page at
www.mhprofessional.com.
McGraw-Hill Education, the McGraw-Hill Education logo, TAB, and related trade dress are trademarks or reg-
istered 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.
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.
TERMS OF USE
This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use
of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store
and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify,
create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any
part of it without McGraw-Hill Education’s prior consent. You may use the work for your own noncommercial
and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if
you fail to comply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO
GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF
OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT
CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY
DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill
Education and its licensors do not warrant or guarantee that the functions contained in the work will meet your
requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill Education nor its
licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the
work or for any damages resulting therefrom. McGraw-Hill Education has no responsibility for the content of any
information accessed through the work. Under no circumstances shall McGraw-Hill Education and/or its licen-
sors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the
use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This
limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract,
tort or otherwise.
CONTENTS AT A GLANCE
1 Programming Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
7 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
A Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1 Programming Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Arduino? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Installation and the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Installing the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Blink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
A Tour of Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Power Supply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Power Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Analog Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Digital Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Uno and Similar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Big Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Small Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Unofficial Arduinos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Modifying the Blink Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Digital Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Digital Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
The Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Analog Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
vii
Analog Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Using Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Arduino Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Arduino Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Arduino Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Minimizing RAM Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Use the Right Data Structures . . . . . . . . . . . . . . . . . . . . . . . . 121
Store String Constants in Flash Memory . . . . . . . . . . . . . . . 122
Common Misconceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Measure Free Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Minimizing Flash Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Use Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Remove Unwanted Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Bypass the Bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Static vs. Dynamic Memory Allocation . . . . . . . . . . . . . . . . . . . 125
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
C char Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
The Arduino String Object Library . . . . . . . . . . . . . . . . . . . . 130
Using EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
EEPROM Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Using the avr/eeprom.h Library . . . . . . . . . . . . . . . . . . . . . . 135
EEPROM Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Using Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Using SD Card Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
A Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Components and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Suppliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Arduino Starter Kits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Since the first edition of this book was released in 2013, some things in
the world of Arduino have stayed surprisingly constant and others have
changed. The Arduino Uno is still most people’s idea of what an Arduino
should be and the Arduino IDE has kept its simple interface, while adding
some great new features such as the Library and Board Managers.
However, what has changed is that the word “Arduino” has come to
denote a way of programming boards as much as the boards themselves.
New Arduino-compatible boards provide a vast variety of hardware to
choose from, all of which can be programmed using the Arduino IDE.
The main changes to this edition include the addition of two new
chapters:
• Chapter 3, “When Is an Arduino Not an Arduino?” This chapter
shows you how to use the Arduino IDE with Arduino-compatible
boards such as the ESP8266 and ESP32 boards as well as ARM m0
boards such as the Adafruit Circuit Playground Express and Trinket
m0 boards.
• Chapter 8, “Interfacing with Arduino” This chapter explains the
theory behind interfacing with microcontrollers that you will need
to understand when working with I2C, SPI, and UART serial.
The “Network Programming” chapter from the first edition (now
Chapter 14, “Network and Internet of Things Programming”) has been
extensively revised to include both official Arduino network program-
ming and the more widely used ESP8266 and ESP32 WiFi programming.
The chapter has also been extended to look at parsing JSON web services
and use of the dweet.io IoT service.
xv
Chapter 16, “Managing with One Process,” has gained a new section on
designing programs using finite state machine diagrams.
The other chapters of the book have also been given a general update.
Simon Monk
Many thanks to all those at McGraw-Hill Education who have done such
a great job in producing this book. In particular, thanks to my editor Lara
Zoble and to Patricia Wallenburg, Elizabeth Houde, Lynn Messina, Stephen
Smith, and Claire Splan.
Thanks to Duncan Amos for his diligent technical review. This book is
greatly improved by his keen eye for detail and unerring ability to sniff
out waffle and vagueness.
I would also like to thank Adafruit, SparkFun, and CPC for supplying
many of the modules and components used in the preparation of this
book.
And last but not least, thanks once again to Linda, for her patience and
generosity in giving me space to do this.
xvii