Mastering Microcontrollers Helped by Arduino
Mastering Microcontrollers Helped by Arduino
py
rig
ht
El
ek
to
rI
nt
er
na
tio
na
lM
ed
ia
ernarvuino_14x21_UK.book Page iii Vendredi, 20. septembre 2013 10:12 10
Table of Contents
ia
1.3 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
ed
2. lM
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
na
tio
3.2.3 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
co
3.2.4 Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.2.5 Input/Output Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.2.6 Analog-to-Digital Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.2.7 Digital-to-Analog Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.2.8 Communication Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2.9 Time Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.2.10 Other Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.3 Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.3.1 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3.2 Loading the Program into the MCU . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.3.3 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
ernarvuino_14x21_UK.book Page iv Vendredi, 20. septembre 2013 10:12 10
iv
ia
4.4.6 Manage the Tabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
ed
4.5 The Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
lM
4.5.1 Table Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.5.2 The Headwaiter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
na
tio
na
5. My First Offense . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
er
nt
5.7 Incarcerated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
co
6.1 Surprises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.2 More Surprises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
6.3 The Matrix Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.4 Charlie to the Rescue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
ernarvuino_14x21_UK.book Page v Vendredi, 20. septembre 2013 10:12 10
ia
6.12 A Little Scam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
ed
6.13 Making New Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
lM
6.14 Null Does Not Equal Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
na
6.15 Snow White’s Apple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
tio
vi
ia
ed
8.3 RS-232 or Serial Port? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
8.3.1 A Few Subtleties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
lM
8.3.2 Chaining Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
na
8.3.3 Breaking the Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
8.3.4 An NMEA 0183A Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
tio
vii
ia
9.11 The Structured Union of Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
ed
9.11.1 struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
9.11.2 union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
lM
9.11.3 typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
na
9.12 Is It an Image? Is It data? It’s Superfile! . . . . . . . . . . . . . . . . . . . . . . . . 255
tio
viii
ia
10.9.1 POR, BOR and BOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
ed
10.10 Let’s Switch Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
10.10.1 The Annoiser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
lM
10.11 La Cucaracha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
na
10.11.1 The 1-Wire Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
tio
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
py
co
ernarvuino_14x21_UK.book Page 35 Vendredi, 20. septembre 2013 10:12 10
ia
ed
4.1 The Godfather 1, 2 and 3
lM
na
Arduino is related to the open-source techno-popularizing projects Processing
tio
from which it borrowed the programming environment and Wiring that provided
na
Processing “is an open source programming language and environment for people
nt
ming within a visual context, Processing also has evolved into a tool for generat-
ek
beginners through experts from around the world share ideas, knowledge and
their collective experience.” (source: www.wiring.org.co)
py
co
35
ernarvuino_14x21_UK.book Page 36 Vendredi, 20. septembre 2013 10:12 10
The first Arduino boards were more basic than the Wiring boards, but over time
the Arduino hardware became more elaborated, whereas Wiring extended its range
by simplifying their boards. Now (in 2013), Wiring S and Arduino Uno boards are
very similar.
Despite the equivalence of the two projects, Wiring did not meet with the same
success as Arduino. From the early beginning the Arduino team has done every-
thing to spread their word by publishing all source code, circuit diagrams, printed
circuit board designs and detailed documentation for free, whereas the Wiring
team tried to control everything. In the Fall of 2012 over 600,000 Arduino boards
had been sold worldwide.
Arduino libraries are based on those from Wiring and the Integrated Development
ia
Environment (IDE), based on the one from Processing, is almost identical to the
ed
Wiring IDE. The only striking difference is its color: orange for Wiring and light
lM
blue for Arduino. Processing uses shades of grey for its IDE with looks very famil-
iar to the two others. Other projects have started to use the Processing IDE as well
na
and we can almost speak of an emerging standard.
tio
The Wiring IDE supports Arduino hardware, but the Arduino IDE does not know
na
from Atmel’s 8-bit AVR family while Wiring openly flirted with microcontrollers
nt
from for instance Microchip and Texas Instruments and also with MCUs based on
rI
an ARM core. Arduino too has been transformed into a multi-MCU platform, but
to
a little “unbeknownst to its own free will”. The Arduino philosophy has been
ek
from various manufacturers. It took a few years to modify the Arduino IDE to
allow it to include compilers for other microcontrollers as well, but with version
ht
1.5 and the introduction of the Arduino Due that sports an ARM Cortex-M3 based
rig
36
ernarvuino_14x21_UK.book Page 37 Vendredi, 20. septembre 2013 10:12 10
The website is the official reference for everything that concerns Arduino. The
website contains software and hardware updates and all the information necessary
to use it all. If you are looking for a reseller of authentic Arduino hardware, it is
here that you can find one. A forum to ask questions that are not answered in this
book is also available. The site is in English, but there are sub-forums accommo-
ia
dating other languages too.
ed
lM
na
tio
na
er
nt
rI
to
ek
El
Leonardo Lilypad
ht
rig
py
co
Figure 4-1 - Some less common Arduino boards: the Leonardo (without connectors) is
equipped with an ATmega32U4 and targets USB devices; the daisy-shaped Lilypad aimed
at wearable applications; the Mega ADK for communication with Android devices and a
Chinese Uno clone recognizable from the missing “Made in Italy” statement.
37
enarvuino_14x21_UK_04.fm Page 38 Mercredi, 25. septembre 2013 8:27 08
ia
ed
lM
na
tio
na
er
nt
rI
to
ek
El
ht
rig
py
co
Figure 4-2 - A collection of Arduino compatible add-on boards, or Shields. Shown are a
Wi-Fi/Bluetooth shield, an Ethernet shield (white), a graphical display shield with a mobile
phone display on it, a shield with a 4 x 4 matrix keypad, a monome 10h with 16 large white
LEDs (see www.monome.org) and left from the center an experimental shield that we will
build in Chapter 8.
1. The Arduino Due is not treated in this book. Not only did it arrive when I had almost
finished this book, but it also makes things more complicated. A bit of Arduino’s
simplicity has been traded in for a more powerful microcontroller.
38
ernarvuino_14x21_UK.book Page 39 Vendredi, 20. septembre 2013 10:12 10
ia
compatible add-on boards. Such an extension board is called a Shield and there are
ed
hundreds of them out there, designed by Arduino users all over the world for all
lM
kinds of applications. Several official Arduino-branded shields are also available,
such as the Ethernet Shield, the Wireless SD Shield, the Motor Shield or the per-
na
forated Proto Shield for prototyping.
tio
na
An Arduino board is fairly simple – until the appearance of the Due – since it is
to
basically an 8-bit microcontroller with expansion slots to which the in- and outputs
ek
ber) belong to the megaAVR family from Atmel. The first Arduino boards were
rig
the Uno has an ATmega328. The Mega 2560 is based on an ATmega2560, and the
co
Mega 1280 on an ATmega1280. All these MCUs are more or less compatible; the
only things that differ are the pin count, the memory sizes and the on-chip periph-
erals. The Arduino Due is different from all other boards because it is equipped
with an Atmel SAM3X8E 32-bit microcontroller based on an ARM Cortex-M3
core.
The microcontroller on an Arduino board contains a small boot loader, which
allows loading a new application into the MCU through a serial port without spe-
cial tools and without overwriting the boot loader. With this little piece of soft-
ware, reprogramming the board is easy. Unfortunately, modern computers no
longer have serial ports, only USB ports (besides of course other unusable ports
39
ernarvuino_14x21_UK.book Page 40 Vendredi, 20. septembre 2013 10:12 10
ia
ed
lM
na
tio
na
Figure 4-3 - Drawing of the Arduino Uno board showing the most important components.
er
The board was designed for educational use, which is why it has this atypical outline that
nt
allows to explain to students how to position the board without requiring any knowledge
rI
like HDMI, VGA, etc.), and an adapter that converts a USB port into a serial port
El
is required. To avoid having to buy a converter on top of the board, the Arduino
ht
During development the USB port of the PC is also used to power the board, but
py
when the application is finished and the board has to survive on its own, it must be
powered in another way. An on-board voltage regulator offers some freedom in the
co
40
ernarvuino_14x21_UK.book Page 41 Vendredi, 20. septembre 2013 10:12 10
ia
ed
lM
na
tio
na
er
nt
rI
to
ek
El
ht
rig
py
co
Figure 4-4 - The Arduino Mega (2560) is longer than the Uno because of the extra
connectors needed to access the additional I/O that the ATmega2560 offers compared to
the ATmega328 of the Uno. The Mega is compatible with the expansion boards for the
Uno.
41
ernarvuino_14x21_UK.book Page 42 Vendredi, 20. septembre 2013 10:12 10
ia
ed
lM
na
tio
Figure 4-5 - A homemade Arduino compatible board, component side (left) and solder side
(right).
na
er
That's all there is to know in case you would like to make your own Arduino com-
nt
patible board. Basically, all you have to do is to program the boot loader into the
rI
port converter or a computer with a real serial port, the USB portion of the board
ek
can be omitted. An RS-232 to TTL level adapter is essential in the last case (and
El
Building an Arduino compatible board seems easy, but there are some complica-
rig
tions. The most important one is the fact that an AVR programmer is required to
py
load the boot loader into the microcontroller. Such a tool is inexpensive, but it
co
means an extra piece of hardware. Another obstacle may very well be the boot
loader that is not necessarily available as an executable for the AVR of your
choice. Therefore use the ATmega328 (or ATmega168) or prepare yourself to
adapt and recompile the boot loader. This is not an impossible task, but being a
novice, you are probably not yet up-to-speed to do this successfully.
Finally, the MCU has some configuration fuses that must be programmed before
you can use it. For this you also need an AVR programmer. The fuse values depend
on the AVR type but, fortunately, the Arduino IDE contains all the necessary data.
The IDE is capable of programming the boot loader into the microcontroller and
configure its fuses for you, but your AVR programmer must be supported by the
IDE.
42
ernarvuino_14x21_UK.book Page 43 Vendredi, 20. septembre 2013 10:12 10
ia
ed
lM
na
tio
Figure 4-6 - This is all you need for an Arduino compatible board.
na
er
When you have completed your Arduino compatible board, you can give it a name.
nt
You are not allowed to call it “Arduino Something” since only boards approved by
rI
the Arduino team are entitled to the appellation. A solution is provided by the
to
name for homemade Arduino boards. On their website there even is a tool to gen-
El
The details on how to install the Arduino IDE can be found in Chapter 1.
The Arduino IDE is rather simple. It is written in Java and includes:
✦ A rudimentary text editor to write the program;
✦ Some shortcuts to configure the IDE and to quickly find examples and help;
✦ Functions to compile the program and load it into the MCU;
✦ A simple terminal to communicate with the board over the serial port.
The six most frequently used functions have buttons below the main menu. From
left to right we have Verify, Upload, New, Open, Save and Serial Monitor.
43
ernarvuino_14x21_UK.book Page 44 Vendredi, 20. septembre 2013 10:12 10
ia
ed
lM
na
tio
na
er
nt
rI
to
ek
El
ht
Figure 4-7 - The Arduino IDE and its three main areas.
rig
py
necessary).
With version 1.0.1 the IDE was extended with some major new features including
menu translation into some thirty languages. By default, the IDE is in English, but
it can be translated to, for instance, French, Danish or Lithuanian.
The introduction of version 1.5 of the IDE did not add many visible changes. The
real novelty of this version is the addition of support for the Arduino Due board
which, because it is based on a 32-bit microcontroller, needs a special program-
ming tool chain.
44
ernarvuino_14x21_UK.book Page 45 Vendredi, 20. septembre 2013 10:12 10
ia
ed
lM
Figure 4-8 - The File menu.
na
tio
Sketchbook – open the Arduino IDE’s working folder, the default location for stor-
rI
ing sketches. Consult Preferences to find out where this folder is.
to
Save as... (Ctrl + Shift + S) – saves the active sketch under a new name.
py
Upload (Ctrl + U) – compile and load the active sketch into the microcontroller
co
using the serial port previously selected in the menu Tools -> Serial Port.
Upload Using Programmer (Ctrl + Shift + U) – compile and load the sketch
into the microcontroller using the programmer previously selected in the menu
Tools -> Programmer.
Page Setup (Ctrl + Shift + P) – prepare the active file for printing.
Print (Ctrl + P) – print the active file.
45
ernarvuino_14x21_UK.book Page 46 Vendredi, 20. septembre 2013 10:12 10
Preferences (Ctrl + comma) – display the IDE’s preferences window. The window
that opens shows only the most useful parameters, many others are available in the
preferences.txt file whose location is indicated at the bottom of the window and
that can be edited outside the IDE. Unfortunately, the documentation of these
options leaves much to be desired.
✦ Sketchbook location specifies the default location where your sketches are
saved. These are the sketches that are accessible when you click on
Sketchbook in the File menu. Editor language lets you choose the language of
the IDE. Editor font size sets the font used to write the sketch. By checking
Show verbose output during the display of messages issued by the compiler
and/or programmer is activated. The option Verify code after upload verifies
ia
that the program has been loaded correctly into the microcontroller, but it
ed
slows down the programming process. I recommend activating it only when
lM
you suspect a communication problem between the computer and the Arduino
board.
na
✦ The option Use external Editor is also interesting. If you check this option, the
tio
IDE’s editor is disabled. The IDE will compile the latest version of the sketch
na
that you may have modified (and saved!) in an editor external to the IDE. It
er
allows you to write the program in the editor of your choice, which is
nt
probably more powerful than the one that comes with the Arduino IDE. A
rI
disadvantage is that the sketch must be opened in two tools (the IDE and the
to
external editor) and you have to go back and forth between the two to edit and
ek
✦ Check for updates at start-up: the Arduino IDE will contact the Arduino
website to see if a new version of the IDE is available;
ht
rig
✦ Update sketch files to new extension on save: IDE versions before 1.0 used the
py
file extension .PDE for sketches. With the introduction of version 1.0 of the
IDE the sketch file extension was changed to .INO. Check this option to
co
automatically change the extension of old sketches to .INO (with the result
that older versions of the IDE will no longer recognize the renamed sketches).
✦ Automatically associate .ino files with Arduino: this option allows you to
launch the IDE by clicking on a sketch with the extension .INO.
Quit (Ctrl + Q) – close all open windows of the IDE. Clicking on the small white
cross in the red square in the upper right of a window only closes that one.
46
ernarvuino_14x21_UK.book Page 47 Vendredi, 20. septembre 2013 10:12 10
ia
ed
lM
na
Figure 4-9 - The Edit menu.
tio
na
er
Copy for Forum (Ctrl + Shift + C) – copy the selected text including its colors and
ht
Copy as HTML (Ctrl + Alt + C) – copy the selected text including its colors and
py
Paste (Ctrl + V) – paste the selection that was previously cut or copied.
Select All (Ctrl + A) – select all the text in the editor.
Comment/Uncomment (Ctrl + /) – add “//” to the beginning of each line of the
selected text to comment it out or remove “//” from the beginning of each line of
the selection to uncomment it. Note that the shortcut Ctrl + / is for a QWERTY
keyboard and may not work on a keyboard with another layout. Tip for Windows:
to change your keyboard in a QWERTY keyboard and back, press Shift + Alt.
Now you can find the key that is at the position of ‘/’.
47
ernarvuino_14x21_UK.book Page 48 Vendredi, 20. septembre 2013 10:12 10
Increase Indent (Ctrl + ]) – shift the selection to the right. Note that the shortcut
Ctrl + ] is for a QWERTY keyboard and may not work on a keyboard with another
layout. Tip for Windows: to change your keyboard in a QWERTY keyboard and
back, press Shift + Alt. Now you can find the key that is at the position of ‘]’.
Decrease Indent (Ctrl + [) – shift the selection to the left. Note that the shortcut Ctrl
+ [ is for a QWERTY keyboard and may not work on a keyboard with another lay-
out. Tip for Windows: to change your keyboard in a QWERTY keyboard and back,
press Shift + Alt. Now you can find the key that is at the position of ‘[’.
Find… (Ctrl + F) – find the next occurrence of a string. You can also replace the
string with another.
ia
Find Next (Ctrl + G) – find the next occurrence of the string selected by Find or
ed
Use Selection For Find.
lM
Find Previous (Ctrl + Shift + G, since 1.0.1) – find the previous occurrence of the
string selected by Find or Use Selection For Find.
na
tio
Use Selection For Find (Ctrl + E, since 1.0.1) – select a string to use with the search
functions Find, Find Next and Find Previous This function has no visible effect.
na
Select some text, for example the name of a variable, press Ctrl + E and then start
er
Verify / Compile (Ctrl + R) – compile the sketch without loading it into the micro-
controller.
Import library... (version 1.5 or later) – add a line #include <xxx.h> to the top of
the sketches main file, the one that bears the name of the sketch. “xxx” is the name
of the library to import. This option provides a sub-option Add Library... for
installing libraries prepared by other users.
Show Sketch Folder (Ctrl + K) – open the folder of the active sketch in a separate
window.
48
ernarvuino_14x21_UK.book Page 49 Vendredi, 20. septembre 2013 10:12 10
ia
ed
lM
Add File... – add a file to the sketch. Files that can be compiled (.ino, .pde, .h, etc.)
are copied into the folder of the sketch and they are opened in new tabs in the IDE.
na
For other files a Data subfolder is created (if it didn’t exist yet) in the folder of the
tio
sketch and the file is copied to this subfolder. Such files will not be compiled.
na
Import Library... (version 1.0.x) – add a line #include <xxx.h> to the top of the
er
sketches main file, the one that bears the name of the sketch. “xxx” is the name of
nt
4.4.4
ek
Tools Menu
El
ht
rig
py
co
This menu may take some time to open, especially with Windows XP. So be
patient when you click on it.
49
Join the Community
Take out a GOLD Membership now!
Your GOLD Membership contains:
• 8 Regular editions of Elektor magazine in
print and digital
• 2 Jumbo editions of Elektor magazine in
print
and digital (January/February and
July/August double issues)
• Elektor annual DVD-ROM
ia
ed
• A minimum of 10% DISCOUNT on all
products in Elektor.STORE
lM
• Direct access to Elektor.LABS
na
• Direct access to Elektor.MAGAZINE; our
tio
online archive for members
na
projects
to
ek
Also Available:
The all-paperless GREEN Membership,
whick delivers all products and
services, including Elektor magazine,
online only.