to worry about in typical Arduino programming, but it’s
nice to know what leads to where.
FIGURE 3. Pin-out diagram of the Atmel ATmega328 chip with
the pin mapping to the Arduino I/O lines.
Writing and Downloading
If you’ve used any kind of microcontroller, you know
the process of programming it involves three steps: write
the program; compile the program; and run the program
(see Figure 4). The Arduino is no different, except that it
refers to its programs as sketches.
Sketches are written in a programming language very
similar to C. In fact, it is C (more accurately C++), but with
some simplifications to make it easier for newcomers to master
the system. If you’ve ever looked at a C/C++ program and
felt your eyes glazing over because of the obtuse appearance
of the code, you don’t have to worry about that with the typical
Arduino sketch. The Arduino is designed for beginners in mind,
but still provides power and flexibility for more advanced users.
Taken indepth, the three steps of writing and
downloading Arduino sketches are:
parentheses are alternative uses — if any — for the pins.
1. Develop your sketch on your PC. The Arduino comes with
a Java-based IDE that includes a fully featured text editor.
It supports syntax highlighting and coloring (different parts
of code are shown in different colors), but doesn’t give you
popup hints or keyword suggestions — like Microsoft’s
Intellisense. If you’re already familiar with another program
editor like Eclipse or SEPY, you can use it instead. The file
format for Arduino sketches is plain ASCII. (Even though SEPY
is intended for programming ActionScript — the language
used to create Adobe Flash applications — it inherently
understands most of the C syntax used in Arduino sketches.)
2. Once written, sketches must be compiled which in Arduino-land is referred to as verifying. During the compile/verify
phase, any errors are flagged and noted at the bottom of
the Arduino editor window. The compiling process includes
building the sketch from component files. An Arduino sketch
is not in itself completely compatible with C; for one thing,
there’s no main() function which is required to tell the compiler
where the program is supposed to begin.
In actuality, it’s still there, under the hood.
When you compile your sketch, the
main() function is added to the program
for you, along with some additional code.
3. The compiled program is downloaded to
the Arduino via a USB cable. The download
process is automatic; the bootloader
program residing in the Arduino detects
when a new sketch is arriving. It performs
the necessary steps of first erasing the
old sketch in memory — if present — then
accepting the new one. Once downloaded,
the sketch starts automatically.
When you download a compiled
62 SERVO 01.2011