Sub topic 10
Verifying and Uploading Your First Sketch
Prerequisites
Assume that you have installed the Arduino IDE, connected an Arduino Uno or compatible board with a USB data cable, and selected the correct board and serial port. A serial port is the computer connection that lets the IDE communicate with the board. Close other programs that might be using the port.
A sketch is an Arduino program. Before the board can run it, the IDE must check the code, translate it into machine instructions, and transfer those instructions to the board.
The Blink sketch
Open the built-in Blink example from the IDE menu:
- Select File > Examples > 01.Basics > Blink.
- Save the sketch with a different name if the IDE asks you to save it.
- Confirm that the selected board and port match your connected hardware.
The example uses the board's built-in LED. The setup() function runs once when the board starts. The loop() function runs repeatedly after setup() finishes.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}The pinMode() function configures the LED connection as an output. digitalWrite() sets that output to HIGH, which turns the LED on, or LOW, which turns it off. The delay() function pauses the program for a number of milliseconds. One thousand milliseconds equals one second.
Verify and compile
Click the Verify button, which usually has a checkmark icon. Verification compiles the sketch without sending it to the board. Compilation checks the program's structure and converts the code into a form that the board's microcontroller can execute.
The message area at the bottom of the IDE reports the result. A successful compile usually ends with a message such as “Done compiling.” The IDE may also show the amount of program memory used.
A compile error includes a file location and a line number. Read the first error first because later messages may be consequences of that error. Common causes include a missing semicolon, a misspelled function name, or an unmatched brace.
Upload the sketch
Click the Upload button, which usually has a right-arrow icon. The IDE compiles the sketch again, resets the board, and transfers the compiled program through USB. The board then starts the new sketch automatically.
On an Uno, the built-in LED marked L should turn on for one second and off for one second. The repeated pattern confirms that the board is executing loop().
The upload process has separate stages. A successful compile does not guarantee a successful upload. The IDE must still find the selected port, communicate with the board's bootloader, and transfer the program without interruption.
| Message or symptom | Likely cause | Action |
|---|---|---|
| No port appears | Cable, driver, or connection problem | Try another data cable or USB port |
| Board not found | Wrong board selected | Select the exact board model |
| Port busy | Another program is using the port | Close Serial Monitor and other serial tools |
| Upload timed out | Unstable connection or incorrect port | Reconnect the board and select its port again |
> callout warning > Disconnect motors, external power supplies, and other wiring while testing the first upload. Incorrect wiring can interfere with reset or damage the board.
A reliable retry sequence
When an upload fails, use this order:
- Check that the USB cable supports data.
- Reconnect the board and wait for the port to reappear.
- Select the board under Tools > Board.
- Select the newly available port under Tools > Port.
- Close any Serial Monitor window.
- Verify the sketch, then upload again.
Exercise
What is the difference between verifying a sketch and uploading a sketch?
Show answer
Verification compiles the sketch and checks for code errors without changing the board. Uploading compiles it and transfers the result to the board so the microcontroller can run it.