Sub topic 1
What an Arduino board is and what the IDE does for it
Prerequisites
This chapter assumes you have:
- Basic comfort installing software on Windows, macOS, or Linux.
- A supported Arduino board, such as an Uno, and a USB data cable. You can read the chapter without them, but later chapters need both.
- A general idea of a program as a list of instructions that run in order.
You do not need electronics experience. You do not need to know C or C++ yet. Later chapters introduce the parts of the language you need.
What an Arduino board is
An Arduino board is a small circuit board built around a microcontroller. A microcontroller is a single chip that contains a processor, working memory, program storage, and input/output pins. It is built to run one program, forever, as soon as it gets power. It has no operating system, no screen, and no file manager.
A laptop is different. It runs many programs at once and loads them from disk when you ask. A microcontroller runs exactly the program you put on it, and it starts that program every time power is applied.
The Arduino board adds everything that makes the chip usable by a person:
- A power circuit that turns the 5 V from USB, or a higher voltage from a barrel jack, into the voltage the chip needs.
- Header sockets that bring the chip's pins out to places where you can connect wires, sensors, and LEDs.
- A USB connection for power and for sending programs to the board.
- A built-in LED wired to one pin, so you can see a result with no extra parts.
- A reset button that restarts the program from the beginning.
On an Arduino Uno, the microcontroller is an ATmega328P. It runs at 16 MHz and has 32 KB of flash memory, which is where your program is stored, and 2 KB of SRAM, which holds variables while the program runs. Other boards use different chips. Boards in the same family can differ a lot in speed and memory, which is why the IDE needs to know which board you have.
The three kinds of memory
Three kinds of memory matter even this early, because error messages refer to them later.
| Memory | What it holds | Survives power off? |
|---|---|---|
| Flash | Your compiled program | Yes |
| SRAM | Variables while the program runs | No |
| EEPROM | Small values you save on purpose | Yes |
When you upload a sketch, the result goes into flash. When the board loses power and gets it back, the program is still there and starts again.
What the IDE does for it
IDE stands for integrated development environment. It is one application that bundles the tools you need to write a program and put it on a device. The Arduino IDE handles four jobs:
- Editing. You write source code in a text editor.
- Compiling. A compiler turns your source code into machine code, which is the binary instructions the microcontroller's processor understands. The microcontroller cannot read C++ text directly.
- Uploading. The IDE sends the machine code over USB and writes it into the board's flash memory.
- Talking back. The Serial Monitor shows text that your program sends to the computer, which helps with debugging.
Without an IDE you would run a compiler from the command line, pass it the correct chip settings, and then run a separate uploader tool with the right port and speed. The IDE runs these tools for you. It chooses their options based on the board and port you select. That is why the next chapters spend time on installing the IDE, selecting the board, and selecting the port. Those selections feed directly into the compile and upload steps.
What happens between "Upload" and a running board
Here is the path your code takes. You will see each step in detail later, but the overall shape helps you place each setup task.
Several parts of this path are worth knowing about.
The sketch. Arduino calls a program a sketch. A sketch is a folder containing a main file with the extension .ino. The folder and the main file must have the same name, and the IDE creates them that way for you.
The board definition. The compiler needs to know the chip type, clock speed, and memory layout. These details come from a board package, a set of files that describes a family of boards. The Uno's definition ships with the IDE. Many other boards need you to install their package first, which is the job of the Boards Manager.
The bootloader. A bootloader is a small program stored permanently on the board. When the board resets, the bootloader runs first and waits briefly for new code to arrive over USB. If code arrives, it writes that code to flash. If nothing arrives, it starts the program already stored. This is how a board can accept new programs over a plain USB cable without special programming hardware.
The serial link. The computer talks to the board through a serial port, a channel that sends bytes one after another. When you plug in a board, your operating system creates a port for it. On Windows it is named like COM3. On macOS it looks like /dev/cu.usbmodem14101. On Linux it looks like /dev/ttyACM0 or /dev/ttyUSB0. The IDE needs to know which port to use, so you select it. Selecting the wrong one is the most common reason an upload fails.
Why the board selection matters
The compiler generates machine code for one specific processor. Code built for an ATmega328P does not run on an ARM-based board, and the reverse is also true. The IDE also uses the board setting to pick the right upload method and speed. If you select the wrong board, one of two things happens:
- The compile fails with an error about missing files or unknown pins.
- The upload fails, because the uploader speaks the wrong protocol to the bootloader.
A third case is worse: the code compiles and uploads but behaves strangely, because pin numbers or timing do not match the real hardware. When a working sketch misbehaves, check the board setting early.
A first look at a sketch
You do not need to understand this code yet. Chapter 8 explains it line by line. It is shown here so you can see what the IDE compiles and uploads.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}Every sketch has two functions. setup() runs once when the board starts. loop() runs over and over after that, for as long as the board has power. This sketch turns the built-in LED on for one second and off for one second, repeatedly. It is called Blink.
The code uses names such as pinMode, digitalWrite, delay, and LED_BUILTIN. You did not define them. They come from the Arduino core, a library of ready-made functions and constants that the IDE adds to every build. LED_BUILTIN is also set by the board definition, so it points to the correct pin on each board. This is one of the ways the board selection affects your code.
What the IDE adds behind the scenes
The compiler needs a C++ program with a main() function, but your sketch has none. The Arduino core supplies one. In simplified form, it calls setup() once, and then calls loop() in an endless cycle. The IDE also adds the needed declarations for your functions before compiling, so you can define them in any order. Because of these steps, a short sketch is enough to produce a complete program.
Common misunderstandings
- "The board runs the IDE." It does not. The IDE runs on your computer. The board only receives the compiled result. After the upload, you can unplug the USB cable from the computer and power the board from another source, and the program keeps running.
- "Uploading adds to what is on the board." It does not. Each upload replaces the previous program. A board holds one program at a time.
- "Unplugging erases the program." It does not, because flash memory keeps its contents without power. Variables in SRAM are lost, so the program starts fresh each time.
- "Any Arduino-looking board works the same." Boards differ in chips, voltage, memory, and USB hardware. Some clone boards use a different USB chip, which can need an extra driver on some systems. The troubleshooting chapter covers this.
What comes next
The following chapters set things up in the order that the dependencies require. You install the IDE and look around. You connect the board and, if needed, install a board package. You then select the board and port, which are the two settings that the compile and upload steps depend on. After that you open Blink, upload it, and read output in the Serial Monitor.
Exercise
You select the Arduino Uno in the IDE, but your sketch targets a board with a different processor. Name one problem this causes and explain why the IDE cannot fix it for you.
Show answer
The compiler builds machine code for the chip of the selected board, so the output does not match the real hardware. The compile may fail on missing pins or files, the upload may fail because the bootloader protocol differs, or the sketch may upload and behave wrongly. The IDE cannot fix this because it has no way to detect which board you meant. It only uses the board setting you chose.
Exercise
A board is plugged in and its power LED is on, but no new serial port appears on your computer. Give two likely causes.
Show answer
The USB cable may be power-only and not carry data. The board's USB chip may need a driver that is not installed. A faulty port or a loose connection are also possible.
Exercise
You upload Blink to a board, then upload a different sketch to the same board. What happens to Blink, and why?
Show answer
Blink is replaced. The upload writes the new machine code into flash memory over the old program, and a board holds only one program at a time.