Arduino Software Internals A Complete Guide to How Your Arduino Language and Hardware Work Together 2nd Edition Norman Dunbar 2024 Scribd Download
Arduino Software Internals A Complete Guide to How Your Arduino Language and Hardware Work Together 2nd Edition Norman Dunbar 2024 Scribd Download
com
https://ebookname.com/product/arduino-software-internals-a-
complete-guide-to-how-your-arduino-language-and-hardware-
work-together-2nd-edition-norman-dunbar/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookname.com/product/arduino-cookbook-2nd-edition-michael-
margolis/
ebookname.com
https://ebookname.com/product/beginning-arduino-2nd-edition-michael-
mcroberts-auth/
ebookname.com
https://ebookname.com/product/the-portable-mba-in-project-
management-1st-edition-eric-verzuh/
ebookname.com
Armenian Christianity Today Identity Politics and Popular
Practice 1st Edition Alexander Agadjanian
https://ebookname.com/product/armenian-christianity-today-identity-
politics-and-popular-practice-1st-edition-alexander-agadjanian/
ebookname.com
https://ebookname.com/product/children-s-literature-a-reader-s-
history-from-aesop-to-harry-potter-1st-edition-seth-lerer/
ebookname.com
https://ebookname.com/product/police-brutality-an-anthology-jill-
nelson/
ebookname.com
https://ebookname.com/product/the-oxford-handbook-of-africa-and-
economics-volume-1-context-and-concepts-1st-edition-celestin-monga/
ebookname.com
https://ebookname.com/product/global-warming-3rd-edition-john-
houghton/
ebookname.com
Osteoporosis Pathophysiology and Clinical Management 1st
Edition Eric S. Orwoll
https://ebookname.com/product/osteoporosis-pathophysiology-and-
clinical-management-1st-edition-eric-s-orwoll/
ebookname.com
MAKER
I N N O VAT I O N S
SERIES
Arduino
Software
Internals
A Complete Guide to How Your Arduino
Language and Hardware Work Together
—
Second Edition
—
Norman Dunbar
Maker Innovations Series
Jump start your path to discovery with the Apress Maker Innovations series! From the basics of
electricity and components through to the most advanced options in robotics and Machine Learning,
you’ll forge a path to building ingenious hardware and controlling it with cutting-edge software. All
while gaining new skills and experience with common toolsets you can take to new projects or even
into a whole new career.
The Apress Maker Innovations series offers projects-based learning, while keeping theory and
best processes front and center. So you get hands-on experience while also learning the terms of the
trade and how entrepreneurs, inventors, and engineers think through creating and executing hardware
projects. You can learn to design circuits, program AI, create IoT systems for your home or even city,
and so much more!
Whether you’re a beginning hobbyist or a seasoned entrepreneur working out of your basement
or garage, you’ll scale up your skillset to become a hardware design and engineering pro. And often
using low-cost and open-source software such as the Raspberry Pi, Arduino, PIC microcontroller,
and Robot Operating System (ROS). Programmers and software engineers have great opportunities
to learn, too, as many projects and control environments are based in popular languages and operating
systems, such as Python and Linux.
If you want to build a robot, set up a smart home, tackle assembling a weather-ready meteorology
system, or create a brand-new circuit using breadboards and circuit design software, this series has all
that and more! Written by creative and seasoned Makers, every book in the series tackles both tested
and leading-edge approaches and technologies for bringing your visions and projects to life.
Second Edition
Norman Dunbar
Rawdon
West Yorkshire, UK
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone
1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit https://www.springeronline.com. Apress
Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM
Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights,
please e-mail [email protected].
Apress titles 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 Print and eBook Bulk Sales web page at http://www.apress.com/
bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers on Github
(https://github.com/Apress). For more detailed information, please visit https://www.apress.com/gp/services/source-code.
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Arduino Installation Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Coding Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Number Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 The Arduino Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Coming Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Arduino Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 Settings.json . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 Finding Other Hidden Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.2 Setting Tab Stops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Globally Defined Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 Boards.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.1 Arduino Uno Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.4 Boards.local.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.5 Platform.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5.1 Build Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5.2 Pre- and Post-Build Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.6 Programmers.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.7 Compiling a Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.7.1 Arduino Sketch (*.ino) Preprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.7.2 Arduino Sketch (*.ino) Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.7.3 After the Build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.8 The Arduino main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.9 Header File Arduino.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.9.1 Header File avr/pgmspace.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.9.2 Header File avr/io.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.9.3 Header File avr\interrupt.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.9.4 Header File binary.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.9.5 Header File WCharacter.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.9.6 Header File WString.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.9.7 Header File HardwareSerial.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.9.8 Header File USBAPI.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.9.9 Header File pins_arduino.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.10 The init() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.10.1 Enabling the Global Interrupt Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.10.2 Enabling Timer 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
vii
viii Contents
H NormDuino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
I No ICSP? No Problem! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
J Breadboard 8 MHz Board Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
K AVRAssist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
About the Author
xiii
About the Technical Reviewer
xv
Acknowledgments
I would like to thank everyone involved in the production of this book—the people you almost never
hear about. Without them, there would be no book.
I’m grateful to the following people at Apress/Springer:
Miriam Haidara who convinced me to update Arduino Software Internals to cover changes for the
new version of the IDE and software. Jessica Vakili who has had the misfortune to have worked with
me on three books now, thanks! Also, Joseph Quatela, James Markham, and Nirmal Selvaraj who kept
answering my silly questions!
The people who turned my manuscript into a proper book, who did the indexing for me, the people
running the print machines, and the cover designers. You never get named or mentioned, but authors
do appreciate you—thanks.
Finally, to my wife Alison and Wesley the cockapoo, thanks for letting me write an update and
keeping me exercised!
xvii
Preface
There are many books which discuss the abilities of the Arduino hardware and how best the maker
can use this to their benefit. I have many of them in my bookcase and digital versions on my phone
and tablet—in case I get bored with life and need something interesting to read. Many of these books
explain what the hardware does, and some even dig deeper into the hardware to explain how, in fairly
easy-to-understand terms, it does it.
There are no books which take a similar view of the Arduino software. There is now!
This book takes you on a journey (why do we always have to be on a journey these days?) into the
world of Arduino sketches and the various files involved in the compilation process. It will delve deep
into the supplied software and look at the specific parts of the Arduino Language which deal with the
underlying hardware, the ATmega328P (or ATmega328AU) microcontrollers—henceforth, referred
to as ATmega328P.
Once the Arduino Language has been explained, the book takes a short look at how you can
strip away the Arduino hand-holding and get down and dirty with the naked hardware. It’s not easy,
but equally it’s not too difficult. Don’t worry; this is still the C/C++ language; there’s no assembly
language required. Perhaps!
xix
Preface to the Second Edition
Since the first edition of Arduino Software Internals was published in 2020, the Arduino environment
has moved onward with new microcontroller boards being added, numerous bugs being fixed, new
bugs introduced—albeit, not deliberately—and many improvements made.
One of the bigger changes has been to the IDE itself; it is now at release 2.1.0 and has changed
completely from the old 1.x releases. It now provides a much more modern experience with code
completion, IntelliSense, and much, much more. It still has drawbacks—when you open a new sketch,
you get a new IDE—but progress has indeed been made in lots of areas.
Another big change is the Arduino Command Line Interface. It has moved on from version 0.6
to version 0.30, and it has become a very usable tool. A couple of major improvements that come
immediately to mind are the ability to upload code with an ICSP device and the ability to burn
bootloaders. It has improved so much that the Arduino IDE has replaced the old preprocessing and
compilation subsystems with the “arduino-cli” under the covers. Sadly, it still cannot upload EEPROM
data.
PlatformIO, another alternative to the Arduino IDE, has itself improved and now, at the time of
writing—May 2023—supports over 1,500 boards, 50 platforms, and 24 different frameworks, not to
mention over 13,400 libraries!
The standard IDE for use with PlatformIO is Visual Studio Code (VSCode) rather than Atom or
the command line, although those are still available. Don’t worry if you don’t like or use VSCode;
PlatformIO Core—the command-line option—can still generate project files for an even larger number
of common IDEs such as Atom, CLion, Code::Blocks, Eclipse, Emacs, NetBeans, Qt Creator, Sublime
Text, Vim, Visual Studio, and VSCode.
The first edition of this book occasionally mentioned Windows, and at that time, I had limited
access to Windows 7. The current version, as of May 2023, is Windows 11, but unfortunately, I no
longer have access to any versions of Windows.
I hope you find the second edition as useful as, if not more than, the first edition.
xxi
Introduction
1
The Arduino is a great system for getting people into making with electronics and microcontrollers. I
was reintroduced to a long-lost hobby when I was gifted an Arduino Duemilanove (a.k.a. 2009) by my
wife’s late grandmother, and since then, I’ve had lots of fun learning and attempting to build things.
I’ve even built a number of Arduino clones based on just AVR microcontrollers and a few passive
components—it’s cheaper than fitting a new Arduino into a project!
Much has changed over the intervening years; LEDs used to cost about £10 each and came in one
color, red. These days, I can get a pack of 100 LEDs for about £2 in various different colors. Even
better, my old faithful Antex 15W soldering iron still worked, even after 35 years. Sadly, after the first
edition was published, it finally died. I bought another one, exactly the same!
The Arduino—and I’m concentrating on either the Uno version 3 or the Duemilanove here
as those are two which I’ve actually purchased (or been given)—is based around an Atmel
ATmega328 microcontroller. On the Uno, it’s the ATmega328PAU, while the Duemilanove uses the
ATmega328PPU.
Roughly, the only difference between the two is the Uno’s ATmega328PAU version is a surface
mount, while the ATmega328PPU version is a 28-pin through-hole device. They are, or should be,
identical to program, although the ATmega328PAU version does have two additional analog pins that
are not present on the ATmega328PPU.
Occasionally though, I may mention in passing the Mega 2560 R3—as I have a cheap Chinese
clone of one of these—which is based on the Atmel ATmega2560 microcontroller.
Some older Arduino boards had the ATmega168 microcontroller, which also was a 28-pin through-
hole version, but it only had 16 Kb of flash memory as opposed to the 32 Kb in the later 328 chips.
The EEPROM and RAM size is also half that of the ATmega328P devices.
The Arduino was designed for ease of use, and to this end, the software and the “Arduino
Language” hides an awful lot from the maker and developer. Hopefully, by the time you have finished
reading this book, you will understand more about what it does and why and, when necessary, how you
can bypass the Arduino Language (it’s just C or C++ after all) and use the bare-metal AVR-specific
C or C++ code instead. Doing this can lead to more space for your code, faster execution, and lower
power requirements—some projects can be run for months on a couple of batteries.
The version of the Arduino IDE described in this book is 2.1.0. The version of the underlying Arduino
Language is 1.8.6.
I used an installation on Linux Mint while writing this book as Linux is my operating system of
choice, plus I do not have access to Windows anymore. The IDE was installed by downloading the
zip file version, as opposed to the flatpak version, and extracted. The location I extracted into is
• /home/norman/arduino-2.1.0/arduino-ide
On first execution, the IDE will create two new hidden directories—if they don’t already exist:
• /home/norman/.arduino15 which will contain the appropriate Arduino Language files for
your chosen board(s)
• /home/norman/.arduinoIDE which holds installation log files and is now where the IDE
preferences are stored
When I compiled a sketch for my Uno board, I was prompted to install the AVR package
required by my Uno. I agreed, and AVR package version 1.8.6 was installed into /home/norman/
.arduino15, which just happens to be the same location used by the 1.x version of the IDE.
The range of preferences offered by the new IDE is not as large as previous versions. Well, it seems
that that is the case, but all is not as it seems. This shall be explained soon!
Within this book, there are references to various files provided by the Arduino software. Because of
the way I’ve installed my software and the fact that the installer versions of the download may install to
a different location, most paths used in this book will be relative to /home/norman/.arduino15.
Paths used will be as follows:
When executing the IDE, it will be found in /home/norman/arduino-2.1.0/
arduino-ide where the downloaded zip file was extracted. However, most of the interesting
files, those for the Arduino Language, are to be found elsewhere.
• $ARDBASE is /home/norman/.arduino15, the location where the IDE installed the AVR
packages for the Uno and other AVR boards.
• $ARDINST is /home/norman/.arduino15/packages/arduino/hardware/avr/
1.8.6, the location of the main Arduino files for AVR microcontrollers. This is where the various
cores, bootloaders, and so on can be found, beneath this directory.
• $ARDINC is /home/norman/.arduino15/packages/arduino/hardware/avr/1.
8.6/cores/arduino, the location of many of the *.h header files and most of the *.
c and *.cpp files that comprise the Arduino Language for AVR microcontrollers. This is
$ARDINST/cores/arduino.
• $TOOLS is where the AVR tools reside in the downloaded packages for the AVR boards. Here, you
will find avrdude and the AVR Library which underlies a lot of the Arduino Language itself. On
my system, this is /home/norman/.arduino15/packages/arduino/tools.
• $AVRINC is where the header files for the version of the AVR Library provided by the Arduino
IDE are located. In the new IDE, these are now dependent on the version of the compiler in use—
avr-g++ by default—so the path can be quite convoluted.
The Arduino Language (eventually) compiles down to calling functions within the AVR
Library (henceforth referred to as AVRLib), and the header files are to be found in location
1.2 Coding Style 3
/home/norman/.arduino15/packages/arduino/tools/avr-gcc/xxxxx/avr/
include/avr.
Here, “xxxxx” is the avr-g++ compiler version and name, currently 7.3.0-atmel3.6.
1-arduino7, but this may change as new releases of the compiler are implemented by the IDE.
So, if you see a file referred to as $ARDINC/Arduino.h in the text, you will know that this
means the file
• /home/norman/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/
arduino/Arduino.h on Linux.
You can see why I’m using abbreviations now, can’t you?
If you wish to examine the files on your system that I am discussing in the book, see Appendix A
for a couple of useful tips on how to avoid always having to type the full paths.
(1) This is a callout that attempts to bring your attention to something in the code which will be
described beneath the code listing in question.
(2) This is another callout; there can be more than one.
In the book’s main text, where you see words formatted like USCR0A or PORTB, then these are
examples of Arduino pin names, AVR microcontroller registers, bits within those registers, and/or
flags within the ATmega328P itself, as well as references to something listed in the data sheet for the
device. Where code listings are being explained, then variables from the code will be shown in this
style too.
Arduino pin numbers will be named Dn or An as appropriate. This is slightly different from the
normal usage of the digital pins, which normally just get a number; I prefer to be a little more formal
and give the digital pins their full title. <grin>
4 1 Introduction
Tip
Tips are exactly that. They give you a clue about something that may not be too well known in
the Arduino world, but which might be incredibly useful. (Or, maybe, just slightly useful!)
Note
This is a note. It brings your attention to something that may require a little more information.
It could be useful to pay attention to these notes. Maybe!
Warning
Warnings are there to highlight potential problems with something in the software or just
something that the data sheet needs you to take extra care over. There may be a possibility
of damage to your Arduino if you don’t pay particular attention. Occasionally, the data sheet
warns against doing something—so it’s best not to do what it says not to do!
Throughout this book, I need to refer to numbers in decimal, binary, or hexadecimal, from time to
time. To this end
Binary Binary numbers are written with a prefix of “0b” and a space every four bits, for
example, 0b0101 1011 0000 1101. All binary numbers will have this prefix, apart
from those which are single bit, that is, 0 and 1.
Hexadecimal Hexadecimal numbers are written with a prefix of “0x”. There are no spaces in
hexadecimal numbers, for example, 0x5B0E.
Decimal These numbers are written as you and I would normally write them, with no prefixes.
Commas will be used to separate the major groupings, for example, 23,310.
I should perhaps point out that there isn’t really such a thing as the Arduino Language. I may refer
to it frequently within the pages of this book, but technically, it doesn’t exist. What it is is simply an
abstraction of the C/C++ language, written in such a way as to make life easier for people learning to
make stuff with their Arduino. Which of the following is easier to understand?
digitalWrite(13, HIGH);
or
PORTB |= (1 << PORTB5);
1.4 Coming Up 5
The first is definitely the easiest to understand; however, the latter is by far the quicker of the
two as it just does what it says; it sets pin 5, on PORTB of the ATmega328P, to HIGH. The name,
digitalWrite(), appears to be a different language, but it isn’t; it’s that abstraction away from
plain AVR C/C++ which makes life easier for us all.
1.4 Coming Up
In Chapter 2, I explain how a sketch gets massaged into a proper C++ program and how the libraries
used in the sketch are incorporated into it. Following the brief overview of how compiling a sketch
operates, I then document the Arduino’s main() function, the various header files that it includes,
and the initialization carried out by the init() function. These initializations are part of every sketch
that you compile, so it helps if you know what the Arduino system is doing, hidden in the background,
just for you.
In Chapter 3, I explain about the features and facilities of the Arduino Language. This will include
all the commands such as pinMode(), digitalWrite(), and so on. I talk through all the
functions that relate to the Arduino, with particular emphasis on the code that applies to the standard
Arduino boards, those based on the ATmega328P family of AVR microcontrollers.
Chapter 4 looks into a number of the C++ classes (or objects) which are supplied with, and
used by, the Arduino Language. The classes of main interest here are the HardwareSerial
class which provides us with the Serial interface and its commands like Serial.begin() or
Serial.println(). However, the HardwareSerial class is not fully self-contained, so the
other, lesser known, supporting classes are also explained in this chapter.
Chapter 5 takes a brief look at how to cast off the bonds of the Arduino Language and delve into
the brazen world of AVR C++ itself, where you bypass the likes of pinMode() calls and talk to the
AVR microcontroller in something akin to its own language. Here, you will learn how you can set
the pinMode() for up to eight pins with a single instruction or how to digitalWrite() those
same eight pins, again with one instruction, and other efficient methods of communication with your
board.
Chapter 6 demonstrates a couple of alternatives to the Arduino IDE. Some people don’t get on
with it; I myself have a sort of love-hate relationship with it as I find versions 1.x of the editor a little
clumsy and slow for my liking. The new, improved versions 2.x of the IDE are much, much better,
however.
In this chapter, I will show you how you can write code for Arduino boards in both the Arduino
Language and plain AVR C/C++ code using the “PlatformIO” system and also give you a hefty
overview of the latest release of the arduino-cli utility used in versions 2.x of the IDE but available
for stand-alone use in Makefiles.
Chapter 7 is where I delve deeper into some features of the ATmega328P which, while not strictly
software, are fundamental to configuring the ATmega328P how you might like it and not as the
Arduino designers, however talented they may be, have decided.
In this chapter, I’ll be looking at the ATmega’s fuses, power reduction modes, sleep modes, and
similar features which determine how the ATmega328P works, but not necessarily what it does.
Chapters 8 and 9 are where I delve deeper into some more features of the ATmega328P which,
while not strictly software, are either important in understanding the Arduino Language or just useful
to know about. Hardware features such as the Analog Comparator (AC), Timer/counters—referred to
as timers henceforth—the Analog-to-Digital Converter (ADC), and the Universal Synchronous/Asyn-
chronous Receiver/Transmitter (USART) are covered in some detail.
6 1 Introduction
Finally, in the Appendixes, there are a number of topics that may be of interest or are kept together
in one place for reference. In here, you will find all the helpful reference material you might need,
such as pinout diagrams, and potentially useful (or unusual) code to upload to your Arduino.
There’s even an index!
Without any further ado, let’s dive in to what happens when you want to compile a sketch in the
Arduino IDE.
Arduino Compilation
2
This chapter is all about what happens when you compile an Arduino sketch and how the various
header files are used. Hopefully, by the time you have read (and understood) this part of the book,
you’ll have a much better idea of what happens during the compilation of an Arduino sketch. However,
before we dive into the gory details of a sketch’s compilation, we need to understand a bit about some
of the text files that live in and around the $ARDINST directory.
These files are used to set up the IDE’s menu options and to define the AVR microcontroller and
Arduino board to be used. Additionally, the IDE needs to know how to compile and upload sketches,
and with lots of different boards nowadays, not just those with AVR microcontrollers, these numerous
text files help the IDE configure the build tools and so on, for the specific board chosen from the
Boards menu in the IDE.
Once we have discussed the various text files, we can then get down and dirty in the compilation
process and also take a look at the hidden C++ files that the Arduino environment keeps well away
from us.
2.1 Settings.json
The file settings.json holds all the preferences for the Arduino IDE, and under versions
2.x of the IDE, it is found in /home/norman/.arduinoIDE which is a new hidden
directory, created on the first run of the version 2 IDE. On Windows, this would be
C:\Users\norman\.arduinoIDE.
You should be able to find the file after the first run of the IDE; if you have not yet done so, there
will not be a settings.json file to be found.
As you may have guessed, the file is in JSON format, which is still a text format, but has a different
layout to the preferences.txt file in previous versions of the IDE.
In the IDE, if you click File Preferences, a dialog will be displayed showing the current
preferences. These have been read from settings.json. Just like the old preferences.txt
file, there are limited preferences that can be set on this dialog. However, there are a lot more
preferences than meets the eye!
The new IDE has a hidden preferences system similar to that in VSCode. You access it via the
CTRL+SHIFT+P key combination. This opens a new search bar in the editor and waits for you to
type something. Type “settings” without the quotes, which will give you a number of options. For
example:
The last option will allow you to edit the settings.json file directly in the IDE; this is very
useful and, at times, quicker than using CTRL+SHIFT+P. The others all appear to display the same
dialog in the IDE, and there are numerous settings available, too many to be honest.
A small example of using this option follows where we will search for the current tab size and
change it to “4,” but inserting spaces instead of a hard TAB character.
Now, you would think that an editor, for writing code, would at least allow you the ability to easily
adjust the width of the tab stops—not so the Arduino IDE!
All is not lost, as we do have that ability, but it involves editing the settings.json file;
however, we don’t have to edit it manually. These instructions only apply to version 2.x of the IDE:
A new tab named “Preferences” will open in the IDE. There are two main options to the left side:
• User
• Workspace
The former will affect everything the current user does in the IDE; the latter will affect only the
current workspace or sketch.
• Click “User” and note that there are a number of categories of settings that can be changed.
• In the search box, type “tab size” without quotes. One of the displayed options is “Editor: Tab
Size.”
• Change the default of “2” to some other value that you prefer; I like four spaces, so I’ve configured
mine to be “4.”
Another useful setting to search for is “spaces.” This will allow you to configure the IDE to insert
spaces instead of actual tab characters. “Editor: Insert Spaces” is the appropriate setting.
Other documents randomly have
different content
The Project Gutenberg eBook of Lectures on
the Principles of Political Obligation
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: English
Credits: GDurb
LECTURES ON THE
PRINCIPLES OF POLITICAL
OBLIGATION
BY
NEW IMPRESSION
PREFACE.
BERNARD BOSANQUET
CONTENTS
3. St. Paul and Kant. It would seem that with Kant 'freedom'
means merely consciousness of the possibility of it, ('knowledge of
sin')
8. The extension of the term from the outer to the inner relations
of life, though a natural result of reflection, is apt to be misleading
10. The failure to see this has led to the errors (1) of regarding
motive as something apart from and acting on will, (2) of regarding
will as independent of motive
11. Thus the fact that a man, being what he is, must act in a
certain way, is construed into the negation of freedom
12. And to escape this negation recourse is had to the notion of
an unmotived will, which is really no will at all
13. The truth is that the will is the man, and that the will cannot
be rightly spoken of as 'acting on' its objects or vice versa, because
they are neither anything without the other
16. If taken (as by the Stoics, St. Paul, Kant (generally), and
Hegel) as applying only to good will, it must still be recognised that
this particular sense implies the generic
18. And though the former is only the beginning of full freedom,
this identity of source will always justify the use of the word in the
latter sense
19. But does not the conception of 'freedom' as = the moral ideal
imply an untenable distinction like that of Kant between the 'pure'
and 'empirical' ego?
20. The 'pure' and 'empirical' ego are one ego, regarded (1) in its
possibility, (2) as at any given time it actually is
21. In man the self-realising principle is never realised; i.e. the
objects of reason and will only tend to coincide
11. Hence two principles for the criticism of law, (1) only external
acts can be matter of obligation proper, (2) the ideal of law must be
determined by reference to the moral end which it serves
14. Law then can only enjoin or forbid certain acts; it cannot
enjoin or forbid motives
15. And the only acts which it ought to enjoin or forbid are those
of which the doing or not doing, from whatever motive, is necessary
to the moral end of society
16. The principle of 'natural law,' then, should be to enjoin all acts
which further action from the highest motive, and no acts which
interfere with such action
18. This, and not the principle of 'laissez-faire,' is the true ground
of objection to 'paternal government'
19. The theory of political obligation (i.e. of what law ought to be,
and why it ought to be obeyed) is not a theory (a) as to how
existing law has come to be what it is
21. 'Natural' rights (like law itself) are relative to moral ends, i.e.
they are those which are necessary to the fulfilment of man's moral
vocation as man
22. This however is not the sense in which political obligation was
based on 'natural rights' in the seventeenth and eighteenth
centuries, previously to utilitarianism
23. The utilitarian theory so far agrees with that here advocated
that it grounds existing law, not on a 'natural' law prior to it, but on
an end which it serves
27. Rights then can only subsist among 'persons,' in the moral
sense of 'persons,' i.e. being possessed of rational will
B. Spinoza.
32. Spinoza, seeing that 'jus naturae' = 'potentia,' and not seeing
that it is not really 'jus' at all, identifies all 'jus' with 'potentia,' both
in the state and in the individual
33. From which it follows that the 'right' of the state against its
individual members is only limited by its 'power'
34. And the same principle applies to the relations of one state to
other states
36. This conclusion does not seem consistent with his starting-
point, according to which men are 'naturally enemies'
C. Hobbes.
45. Though by his theory the sovereign may be one or many, and
sovereignty is transferable by the act of a majority, he tacitly
vindicates the absolute right of a de facto monarchy
46. The radical fiction in his theory is that there can be any 'right'
after the institution of sovereignty, if (as he holds) there is none
before it
48. His 'contract' can confer none but natural right, and that is
either not a right at all, or (if it is) it belongs to all men, subject and
sovereign alike
D. Locke.
59. And this distinction between the supreme community and the
supreme executive enables him to distinguish between dissolution of
the political society and dissolution of the government, which
Hobbes had confused
E. Rousseau.
66. His statement of the origin and nature of the 'social contract'
69. The practical result of his theory has been a vague exaltation
of the will of the people, regardless of what 'the people' ought to
mean
73. His distinction between the 'will of all' and the 'general will':
the latter always wills the common good, though it may be mistaken
as to means
88. This has been the case in ancient despotisms, and in the
modern empires of the East
90. Under the Roman Empire, in British India, in Russia, where the
technical is also the real sovereign, its strength rests in different
degrees on the general will
92. But (a) it need not be the supreme coercive power, and (b) if
it is so, it is not because it is so that it commands habitual obedience
93. Thus (retaining the technical use of 'sovereign') it is true that
if the sovereign is to be so really, it must express and maintain a
general will
94. Though this is compatible with the fact that some of the laws
of the sovereign conflict with the general will
96. An antithesis between sovereign 'de jure' and 'de facto' can
only arise from a confusion between 'sovereign' as = the source of
law and 'sovereign' as = the 'general will'
100. The individual must indeed judge for himself whether a law is
for the common good; but though he judge it not to be, he ought as
a rule to obey it
103. In such cases the truth generally is that the 'right,' on the
particular issue, has not yet formed itself
104. But it does not follow that because the 'right' is on both
sides, one is not 'better' than the other; though this may be the case
106. The individual, having no 'right' to guide him, should take the
side whose success seems likely to be best for mankind
109. Some general questions which the good citizen may put to
himself in such dilemmas
114. For the desire for freedom in the individual is no real desire
unless he is one of a society which recognises it. (Slaves are not a
real exception to this)
118. And both imply the twofold conception, (a) 'I must though I
do not like,' (b) 'I must because it is for the common good which is
also my good'
121. We must admit (a) that the idea of the state as serving a
common interest is only partially realised, even by the most
enlightened subject, though so far as realised it is what makes him a
loyal subject
123. And (c) that even then his patriotism will not be a passion
unless it includes a feeling for the state analogous to that which he
has for his family and home
124. But are we not again assuming what was disputed, viz. that a
sense of its serving a common interest is necessary to the existence
of the state?
128. But though human motives are never unalloyed, they only
produce good results so far as they are fused with and guided by
some unselfish element
130. And further reflect that the idiosyncrasy of such men plays
but a small part in the result, which is mainly due to agencies of
which they are only the most conspicuous instruments
132. The fact that the state implies a supreme coercive power
gives colour to the view that it is based on coercion; whereas the
coercive power is only supreme because it is exercised in a state, i.e.
according to some system of law, written or customary
133. In the absence of any other name, 'state' is the best for a
society in which there is such a system of law and a power to
enforce it
141. But though in this way there may be rights outside the state,
the members of a state derive the rights which they have as
members of other associations from the state, and have no rights
against it
142. i.e. as they derive their rights from their membership in the
state, they have no right to disobey the law unless it be for the
interest of the state
143. And even then only if the law violates some interest which is
implicitly acknowledged by the conscience of the community
146. And it may be held that the claim of the slave upon the
citizen, as a man, overrides the claim of the state upon him, as a
citizen
150. All rights are 'personal'; but as a man's body is the condition
of his exercising rights at all, the rights of it may be called 'personal'
in a special sense
151. The right of 'life and liberty' (better, of 'free life'), being based
on capacity for society, belongs in principle to man as man, though
this is only gradually recognised
154. This last is the logical complement of the idea that man as
such has a right to life; but the right is only negatively recognised in
modern Christendom
157. (1) Has the state a right to override this right in war? It must
be admitted that war is not 'murder,' either on the part of those who
fight or of those who cause the war
158. Yet it may be a violation of the right of life. It does not prove
it not to be so, that (a) those who kill do not intend to kill anyone in
particular
159. Or that (b) those who are killed have incurred the risk
voluntarily. Even if they have, it does not follow that they had a
'right' to do so
161. But this only shifts the blame of war to those who are
responsible for those exigencies; it remains a wrong all the same
162. But in truth most wars of the last 400 years have not been
wars for political liberty, but have arisen from dynastic ambition or
national vanity
163. Admitting, then, that virtue may be called out by war and
that it may be a factor in human progress, the destruction of life in it
is always a wrong
164. 'But if it be admitted that war may do good, may not those
who originate it have the credit of this?'
165. If they really acted from desire to do good, their share in the
wrong is less; but in any case the fact that war was the only means
to the good was due to human agency and was a wrong
166. (2) (See sec. 157). Hence it follows that the state, so far as it
is true to its principle, cannot have to infringe the rights of men as
men by conflicts with other states
167. It is not because states exist, but because they do not fulfil
their functions as states in maintaining and harmonising general
rights, that such conflicts are necessary
168. This is equally true of conflicts arising from what are called
'religious' grounds
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com