Now that you have an understanding of where all the magic code comes from when you write a simple Arduino sketch, let’s look at exactly how that code gets into the flash memory of an Arduino board’s microcon- troller when you click the Upload button in the Arduino IDE.
Arduino sketches are held in a text file with the .ino extension, in a folder of the same name but without the extension. You may occasionally come across Arduino sketches with the extension .pde, which is no longer used. To make use of such a sketch in the Arduino IDE, you can just change its extension to .ino.

What actually happens is that the Arduino IDE controls a number of utility programs that do all the actual work. First, a part of the Arduino IDE, which (for want of a better name) I have named the Arduino IDE pre- processor, assembles the files provided as part of the sketch. Note that nor- mally only one file is in the sketch folder; however, you can place other files in the folder if you wish, but you need to use a separate editor to create them.
If you have other files in the folder, they will be included in this build process. C and C++ files are compiled separately. A line to include arduino.h is added to the top of the main sketch file.
As there are many different types of Arduino boards that use different microcontroller chips that have different pin names, the Arduino IDE must use the right pin definitions for the board. If you look in the hard- ware/arduino/variants folder, you’ll find a folder for each type of Arduino board, and inside each folder, you’ll see a file called pins_arduino.h. This file contains constants for the pin names for that platform.
When everything has been combined, the next step is to invoke the GCC compiler. This compiler is an open source C++ compiler that is bun- dled as part of the Arduino distribution. It takes the sketch, header, and C implementation source code files and converts them into something that can be run on an Arduino. It does this in a number of steps:
- The preprocessor interprets all the #if and #define commands and determines what actually goes into the build.
- Next, the code is compiled and linked into a single executable file for the type of processor used by the board.
- After the compiler has finished its work, another piece of open source software called avrdude actually sends the executable code, saved as a hexadecimal representation of the binary, to the board over the USB serial interface.
We are now in the Arduino’s realm. The Arduino has a small resident program installed on every microcontroller that is included with its board. This program is called a bootloader. The bootloader actually runs very briefly every time an Arduino is reset. This is why when serial communi- cation starts to an Arduino Uno, the hardware serial link forces a reset to give the bootloader chance to check for any incoming sketches.
If there is a sketch, then the Arduino effectively programs itself by unpacking the hexadecimal representation of the program into binary. It then stores the sketch in the flash memory. The next time that the Arduino restarts, after the usual bootloader check for a new sketch, the program that was stored in flash is automatically run.
You might wonder why the host computer cannot program the micro- controller directly rather than taking this convoluted path. The reason is that programming a microcontroller requires special hardware that uses a different interface to the Arduino board (that’s what the ICSP 1 header in Figure 1-8 is for). By using a bootloader that can listen on a serial port, you can program the Arduino through USB without having to use special pro- gramming hardware.
However, if you do have such a programmer, such as the AVRISPv2, AVRDragon, or the USBtinyISP, then you can program the Arduino directly through such a programmer, bypassing the bootloader entirely. In fact, as you shall see later in this chapter, you can also use a second Arduino as a programmer.
Bypassing the bootloader both reduces the amount of flash memory needed for the sketch and allows the Arduino to start more quickly, as the Bootloader spends a second or so checking to see if there is an incoming program each time the Arduino resets.