Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Boards and IDE

Compiling and Uploading: The Bootloader

Learn what happens when you press Upload: the compiler, the HEX file, the bootloader, the auto-reset trick, flash memory limits and what to do when upload fails.

When you press Upload, a surprising amount happens in a few seconds. The IDE compiles your sketch into machine code, resets the board, and sends the program over a serial link to a small piece of software already living on the chip, the bootloader, which writes it into flash memory. Knowing this chain helps you diagnose errors, recover bricked-looking boards and understand why your program survives power cycles.

Three kinds of memory

The ATmega328P has three memories:

  • Flash (32 KB): holds the program and the bootloader. It keeps its content without power and can be rewritten about 10,000 times.
  • SRAM (2 KB): variables and the stack. It is erased when power goes off.
  • EEPROM (1 KB): small storage for settings, kept without power (covered later in the course).

Uploading writes to flash; running the sketch uses SRAM. Big constant text strings can live in flash with the F() macro, saving RAM:

C++
Serial.println(F("This text stays in flash, not in RAM"));

What the bootloader does

The bootloader (Optiboot on the Uno, about 0.5 KB) lives at the end of flash. On reset, it runs first and waits a fraction of a second for a program arriving over the serial port. If one arrives, it writes it to flash. If not, it jumps to your sketch. This is why a freshly reset board waits briefly before blinking, and it is why you can upload with just a USB cable, with no special programmer.

sequenceDiagram
    participant PC as Computer
    participant B as Bootloader
    participant S as Your sketch
    PC->>B: reset pulse (DTR)
    B->>PC: ready for a program?
    PC->>B: HEX data in pages
    B->>B: write pages to flash
    B->>S: jump to start of sketch

Figure: an upload. The computer resets the board, the bootloader accepts the data and then starts your sketch.

The auto-reset trick

On an Uno, the USB-serial chip's DTR line is wired through a capacitor to the reset pin. When the IDE opens the port to upload, DTR toggles and resets the board automatically, so you never press the reset button. Two consequences: opening the Serial Monitor also resets the board, and a reset capacitor hacked off or a 5 V reset pin held low can break uploads.

From source to HEX

The compiler produces a .hex file in the Intel HEX text format: lines of address, data and checksum. You can see how simple it is. Each record starts with a colon, then the byte count, address, record type, data and a checksum that makes the sum of all bytes zero (mod 256).

C Stand-in: verify the checksum of Intel HEX lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The bootloader checks the same kind of integrity data, which is how a half-sent program is rejected rather than half-run. The bytes 0C 94 34 00 that start the first line are an AVR JMP instruction: the reset vector jumping to the startup code.

Flash pages and limits

Flash is written in pages (128 bytes on the ATmega328P), so uploading cuts the HEX into page-sized pieces. Your usable space is flash minus the bootloader: the IDE reports "maximum is 32256 bytes" on an Uno. If your sketch is bigger, you get "Sketch too big" at compile time. Remedies: remove unused features, use F() for text, replace float math with integers, use smaller libraries, or move to a bigger board.

C Stand-in: how many pages will an upload write?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

When uploads fail

  • "not in sync" or "programmer is not responding": wrong board or port, another program holding the port (close the Serial Monitor or a terminal), a power-only cable, or a pin 0/1 wire that blocks the serial link (unplug them while uploading).
  • Upload succeeds but behavior is odd: a previous sketch left a peripheral running; press reset, and check that you selected the same board type you compiled for.
  • Board vanished from the port list: try a different cable and port; double-press reset on boards with native USB to enter bootloader mode.
  • Bootloader erased or corrupted: burn it again from the IDE with Tools > Burn Bootloader, using an ISP programmer or a second Arduino as ISP.

You can also upload without the bootloader through the ICSP header with a programmer, which gives you back the 0.5 KB and removes the startup wait, at the price of needing the programmer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the bootloader?

    The bootloader runs after reset and accepts new programs over the serial port.
  2. 2. Why does opening the Serial Monitor restart an Uno sketch?

    DTR is wired to the reset pin through a capacitor, creating the auto-reset.
  3. 3. Which memory keeps its contents when power is removed?

    Flash (program) and EEPROM keep data; SRAM does not.
  4. 4. What does the F() macro do with a string literal?

    On AVR boards, F() avoids using scarce RAM for constant text.

Summary

  • Upload = compile, convert to HEX, reset the board, send pages to the bootloader, which writes flash.
  • Flash holds the program, SRAM holds variables, EEPROM holds saved settings.
  • The auto-reset (DTR through a capacitor) makes uploads button-free and makes the Serial Monitor restart sketches.
  • Upload errors are usually wrong board or port, a busy port or a bad cable.
  • Save RAM with F() and save flash by removing unused code and libraries.

Next: pinMode and digitalWrite