Sub topic 7
How a Sketch Gets from Your Computer to the Board
The Arduino Uno R3 uses a bootloader to receive a compiled sketch over USB. This chapter assumes you know the Arduino IDE, the setup() and loop() functions, and the basic roles of voltage, current, and resistance.
The parts involved
A sketch begins as C++ source code on your computer. The Arduino IDE compiles it into machine code for the Uno's ATmega328P microcontroller. The IDE then sends that machine code through the USB cable.
The Uno does not have native USB on the ATmega328P. A second chip, the ATmega16U2, acts as a USB-to-serial bridge. Your computer sees this bridge as a serial port. The bridge converts USB data into UART serial data that the ATmega328P can receive.
The upload path is:
The bootloader is a small program stored in a protected section of the ATmega328P's flash memory. It runs briefly after reset and listens for an upload. If no upload begins, it starts the existing sketch.
What happens when you click Upload
The Arduino IDE performs several steps:
- It checks the selected board and serial port.
- It compiles the sketch and reports any errors.
- It opens the serial port used by the Uno.
- Opening the port toggles control signals that normally reset the board automatically.
- After reset, the bootloader listens for upload commands.
- The IDE transfers the compiled program.
- The bootloader writes the program into flash memory and verifies the transfer.
- The bootloader jumps to the new sketch.
A simple upload test is the built-in LED:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}Select Arduino Uno under the board menu and choose the port that appears when you connect the board. After a successful upload, the built-in LED should blink twice per second.
| Stage | What can fail | Typical symptom |
|---|---|---|
| Compile | Invalid C++ or missing library | Error messages before transfer |
| Port selection | Wrong or unavailable port | Port error or no device |
| Reset and bootloader | Timing or connection problem | “ avrdude ” timeout |
| Flash write | Communication interruption | Verification or upload error |
Troubleshooting upload failures
A compile error is a software problem. Fix the highlighted line before checking the cable.
If the IDE cannot find the board, check the USB cable, board selection, and port selection. Some USB cables provide power but have no data wires. The power LED can still turn on with such a cable.
If the bootloader is not receiving data, press the Uno's reset button just as the IDE begins uploading. This can help diagnose timing problems, but normal Uno boards reset automatically.
The bootloader occupies flash memory, so the available space for your sketch is slightly smaller than the ATmega328P's total flash capacity.
Exercise
Why can the built-in LED turn on even when the IDE cannot upload a sketch?
Show answer
USB power can reach the board while data communication fails. A charge-only cable, wrong port, or serial-driver problem can provide power without providing a usable upload path.