Sub topic 10
Verifying and uploading your first sketch
This chapter assumes you have installed the Arduino IDE (version 2.x), connected a board over USB, and selected the matching board and serial port. It also assumes you have opened the Blink sketch from the previous chapter. Here you will separate the two actions the IDE offers, Verify and Upload. You will also see what happens between pressing the button and seeing the LED blink, and learn to read the output of each stage.
Verify and Upload are different actions
The toolbar at the top of the IDE has two buttons you will use constantly.
- Verify (the check mark) compiles the sketch. It does not touch the board. You can run it with no board connected.
- Upload (the right arrow) compiles the sketch first, then sends the result to the board over the serial port.
Because Upload always compiles first, you do not need to press Verify before Upload. Verify is still useful when you want to check for errors quickly without disturbing the program that is running on the board. It is also the only option when the board is not plugged in.
| Action | Compiles | Needs a board | Needs a port | Changes the board |
|---|---|---|---|---|
| Verify | Yes | Selected board type only | No | No |
| Upload | Yes | Yes | Yes | Yes |
Verify still needs a board type selected, because the compiler must know which processor to build for. A sketch compiled for an Uno will not run on a different processor family.
What the IDE does when you press Verify
A sketch is a text file with the .ino extension, and a microcontroller cannot run text. The IDE turns it into machine code in several stages.
- Preprocessing. The IDE combines the
.inofiles in the sketch folder into one C++ file. It adds#include <Arduino.h>at the top. It also generates function prototypes, which are declarations of your functions, so you can call a function before its definition appears in the file. - Compiling. The compiler (for the Uno,
avr-gcc) translates each C++ file into an object file of machine instructions for that processor. Syntax errors and misspelled names stop the build here. - Linking. The linker joins your object files with the Arduino core library. The core provides functions such as
pinMode(),digitalWrite(), anddelay(). The result is one program image. - Converting. The tools convert that image into a
.hexfile, a text format that records the bytes to write into the board's flash memory.
If all four stages succeed, the Output panel at the bottom of the window ends with a summary like this for an Uno:
Sketch uses 924 bytes (2%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.Your numbers may differ slightly between IDE and core versions. The two lines tell you how much of each memory type the sketch uses.
- Program storage is flash memory. It holds your compiled code and survives power loss. The Uno has 32,256 bytes available to sketches. The rest holds the bootloader.
- Dynamic memory is SRAM. It holds global variables and the call stack while the program runs. The Uno has only 2,048 bytes, so this limit is reached much sooner than the flash limit in larger programs.
Reading the Blink sketch before you upload it
Here is the sketch you are about to upload.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}setup() runs once at power-up or reset. loop() runs repeatedly for as long as the board has power. LED_BUILTIN is a constant defined by the board package. It names the pin that is wired to the on-board LED, which is pin 13 on an Uno. On a different board the constant maps to a different pin, which is why the same code works across boards.
Uploading step by step
- Confirm the board name and port appear in the drop-down at the top of the window. Both must be correct.
- Click Upload.
- Watch the Output panel. The IDE compiles first, then reports that it is uploading.
- Wait for the message Done uploading in the notification area.
- Look at the board. The orange LED marked
Lshould turn on for one second and off for one second.
During the transfer, two small LEDs marked TX and RX flicker on an Uno. They show data moving over the serial connection. This flicker is a good sign that the upload reached the board.
What happens between the button and the blinking LED
The upload step is not a plain file copy. Several parts cooperate.
The key pieces are these.
- The uploader. For the Uno, the IDE calls a command-line program named
avrdude. Other boards use other tools, chosen by the board package. - The bootloader. This is a small program that lives in a protected area of flash. After every reset it runs first and waits a moment for new code to arrive. If none arrives, it starts the sketch already stored in flash. The bootloader is why you need no special programmer hardware for the Uno.
- The automatic reset. On an Uno, opening the serial port makes the USB-to-serial chip pull the reset line. That restarts the board into the bootloader at the right moment, with no button press.
A side effect follows from this design. Every time a program opens the board's serial port, the board resets. This includes the Serial Monitor, which a later chapter covers. If your sketch seems to restart whenever you open the monitor, this is the reason.
Boards with a native USB processor, such as the Leonardo, behave differently. They present themselves to the computer as a USB device, and the IDE triggers the bootloader by briefly opening the port at 1200 baud. During upload the board may disappear and reappear under a different port name. This is normal, and the IDE usually handles it.
Reading the Output panel
Use the Output panel, not only the notification pop-up, when something goes wrong. Compile errors and upload errors are reported in different places and mean different things.
Compile errors
These appear in red, with the file name and line number:
Blink:3:3: error: 'pinmode' was not declared in this scope; did you mean 'pinMode'?Read from the left. The sketch is Blink, the problem is at line 3, column 3, and the name pinmode is unknown. C++ is case-sensitive, so pinmode and pinMode are different names. The compiler also suggests the likely fix. Other frequent causes are a missing semicolon, an unmatched brace, and a function name typed incorrectly.
A compile error means nothing was sent to the board. Whatever program was on it before is still there.
Upload errors
These happen after a successful compile. The avrdude messages are the key clues:
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00The compiler succeeded, but the uploader could not talk to the bootloader. Likely causes are a wrong port, the wrong board type selected, a charge-only USB cable, or another program holding the port open. The next chapters on troubleshooting cover these in detail. Here, the useful rule is that a compile error points at your code, while an upload error usually points at your setup.
Turning on verbose output
By default the Output panel shows a short summary. For more detail, open File > Preferences (on macOS, Arduino IDE > Settings) and enable Show verbose output during for compilation and upload. The panel then prints every command the IDE runs, including full compiler and avrdude command lines. This helps when you need to see exactly which core, tool version, or port the IDE used.
Confirming the upload worked
Do not trust the LED alone. Run this short check.
- Change
delay(1000)todelay(250)in both places. - Click Upload.
- The LED should now blink about four times as fast.
If the blink rate changes, you have shown the whole chain works: your edit reached the compiler, the new program reached the board, and the board is running it. If the rate does not change, the board is still running the old program, and the upload did not succeed even if the IDE showed no obvious error.
Common edge cases
- The LED already blinks before you upload. New boards are often shipped with Blink preloaded. Change the delay as above so you can tell your upload from the factory program.
- Upload is greyed out or says no board selected. The IDE needs a board and a port. Select both from the drop-down at the top of the window.
- Upload takes a long time on the first run. The first compile for a board builds the whole core. Later builds reuse cached files and are much faster.
- The sketch folder and file names must match. A sketch named
Blinklives in a folder namedBlinkand containsBlink.ino. If you rename one, rename the other. - Unsaved changes. The IDE compiles the current editor contents, but it asks you to save the sketch when you close it. Save your work with Ctrl+S (Cmd+S on macOS) so you do not lose edits.
- The previous program keeps running. If compilation fails, the board keeps running the last successful upload. Do not conclude that your new code does nothing before you check the Output panel.
Summary of the workflow
The routine you will repeat for every project is short.
- Edit the sketch.
- Click Verify to catch compile errors quickly, or go straight to Upload.
- Read the Output panel for the memory summary or any errors.
- Observe the board to confirm the new behavior.
Exercise
You click Upload and the Output panel shows a red message that says 'digitalwrite' was not declared in this scope. The board is still blinking at the old rate. Did the board receive any part of your new sketch? What is the likely fix?
Show answer
No. This is a compile error, so the process stopped before the uploader ran, and the board keeps running the last program it received. The fix is to correct the capitalization to digitalWrite, because C++ names are case-sensitive. Then upload again.
Exercise
Verify finishes with "Sketch uses 924 bytes (2%) of program storage space" but you have no board connected. Why can Verify succeed without a board, and why can't Upload?
Show answer
Verify only compiles the code for the selected board type, which needs the compiler and core but no hardware. Upload must open a serial port and talk to the bootloader on a physical board, so it needs a connected board and a selected port.
Exercise
Why does the Uno restart its sketch every time you open a program that connects to its serial port?
Show answer
Opening the serial port makes the USB-to-serial chip pull the board's reset line, which is the same mechanism the IDE uses to start the bootloader before an upload. The reset restarts the board, so the sketch begins again from setup().