Sub topic 7
How a sketch gets from your computer to the board
This chapter assumes you know what the USB connection provides and what the USB-to-serial bridge does. It also assumes you have uploaded a sketch from the Arduino IDE. Here you will learn what happens between pressing Upload and your code running, and why uploads fail.
What you actually upload
The Arduino IDE does not send your .ino text to the board. The microcontroller on an Uno R3, the ATmega328P, only runs machine code, so the IDE first builds the sketch:
- It adds the Arduino core (the code behind
digitalWrite,delay,Serial, and so on) and your libraries. - The
avr-gcccompiler turns everything into machine code. - The linker joins the pieces into one program image, saved as a
.hexfile. - A tool called
avrdudesends that image to the board over the serial port.
The program image is written to flash memory, which keeps its contents with the power off. Your variables live in SRAM, which is empty at every power-up or reset. That is why a sketch survives unplugging but its variable values do not.
The ATmega328P has 32 KB of flash, and the bootloader takes about 0.5 KB of it. The IDE's size report is measured against the remaining roughly 31.5 KB.
The bootloader
A microcontroller cannot write its own program memory from a blank state. It needs something already running that accepts new code. That something is the bootloader, a small program stored at the top of flash. A programmer is dedicated hardware that writes flash directly, and the bootloader saves you from needing one.
The Uno ships with a bootloader called Optiboot. It does three things:
- It runs first after every reset.
- It listens on the serial pins (RX on pin 0, TX on pin 1) for a short time, about a second, for an upload request.
- If a valid request arrives, it receives the new program and writes it to flash. If nothing arrives, it jumps to your sketch.
Optiboot speaks the STK500 protocol at 115200 baud. The IDE's board selection tells avrdude the protocol, baud rate, and chip, so choosing the wrong board is a common cause of failed uploads.
The bootloader also makes the on-board LED on pin 13 blink a few times at each reset. This gives you a visual check that the board restarted.
Auto-reset: how the IDE gets the bootloader's attention
The bootloader only listens right after a reset, so the IDE has to reset the board just before it sends data. You do not press the reset button because the board does it for you.
The ATmega16U2, the USB-to-serial bridge, has a control line called DTR (Data Terminal Ready). When a program opens the serial port, DTR goes low. DTR connects to the 328P's RESET pin through a 100 nF capacitor. The capacitor passes the falling edge as a brief low pulse, which resets the chip. A pull-up resistor then returns RESET to HIGH, so the chip starts and the bootloader begins listening.
The same mechanism has a side effect that surprises many people. Opening the Serial Monitor also opens the port, so it also resets an Uno. Your sketch restarts from the top of setup() every time you open the monitor.
Seeing the reset
This sketch makes the restart visible. It prints a line from setup() and then reports how long it has been running.
void setup() {
Serial.begin(9600);
Serial.println("setup() ran: the board just reset");
}
void loop() {
Serial.print("Running for ");
Serial.print(millis() / 1000);
Serial.println(" s");
delay(1000);
}Upload it and open the Serial Monitor. You will see the "setup() ran" line and the counter starting from 0. Close and reopen the monitor and the counter starts over. Press the physical reset button and it restarts too. Unplug and replug the USB cable and it restarts yet again.
Stages of an upload
| Stage | Where it happens | What can go wrong |
|---|---|---|
| Compile and link | On your computer | Syntax errors, missing libraries, sketch too large for flash |
| Open the port | Computer and USB bridge | Wrong port selected, missing driver, charge-only cable |
| Reset | DTR pulse to RESET | Board has auto-reset disabled, or something holds RESET |
| Handshake | Bootloader over pins 0 and 1 | Wrong board selected, another device using pins 0 and 1 |
| Write and verify | Bootloader and flash | Cable disconnects mid-upload, a damaged bootloader |
What a failed upload tells you
The IDE reports upload errors from avrdude. The most common is:
avrdude: stk500_recv(): programmer is not respondingIt means the computer sent the handshake and got no valid answer. Work through the likely causes in this order:
- Wrong port. Check Tools > Port. If no port appears, the computer does not see the board at all.
- Wrong board. The protocol and baud rate come from the board choice.
- Charge-only cable. Some cables carry power but no data lines. Try another cable.
- Missing driver. Many clone boards use a CH340 or CP2102 bridge chip instead of the 16U2, and they may need a driver installed.
- Something on pins 0 and 1. These pins are shared with the USB connection. A module wired to RX or TX can hold the line and block the handshake. Disconnect those wires and upload again.
- No bootloader. A chip that has been wiped or replaced has nothing to answer. You then need a programmer (see the callout below).
A few details worth knowing
Uploading erases the old sketch. The bootloader erases the application area before writing. If the upload fails halfway, the board has no valid sketch until a later upload succeeds. The bootloader itself is protected, so you can retry.
Flash wears out. Flash on the ATmega328P is rated for about 10,000 erase and write cycles. This is rarely a problem during development. It is one reason to avoid sketches that write to flash in a loop, and EEPROM has its own separate wear limit.
The bootloader costs a second at startup. After power-up or reset, the sketch begins running only after the bootloader's wait time ends. If your design needs an instant start, you can remove the bootloader and program the chip through the ICSP header.
ICSP is the bypass. The 6-pin ICSP header connects directly to the chip's programming interface. An external programmer can write flash through it without any bootloader. It is also how you restore a bootloader on a chip that lacks one. The IDE does this under Tools > Burn Bootloader.
Boards that do it differently
The Uno's reset-by-DTR design is not universal. The Leonardo and other boards with native USB have no separate bridge chip. The IDE asks them to reset by opening the port at 1200 baud, and the board then shows up as a different USB device while it is in bootloader mode. The newer Uno R4 uses a different microcontroller and bootloader again. Later chapters on board variants cover these differences. For now, treat everything above as specific to the Uno R3 and boards built like it, such as the classic Nano and Mega.
Exercise
You upload a sketch and it works. You open the Serial Monitor and notice the sketch's startup message is printed again, and the counter restarts at 0. Explain why, and say where on the board the signal that causes it comes from.
Show answer
Opening the Serial Monitor opens the serial port on the computer. That makes the USB-to-serial bridge (the ATmega16U2) pull its DTR line low. DTR is connected to the ATmega328P's RESET pin through a 100 nF capacitor, so the falling edge becomes a short reset pulse. The 328P restarts, the bootloader runs and waits briefly for an upload, then jumps to the sketch, so setup() runs again and millis() starts from 0.
Exercise
Your upload fails with "stk500_recv(): programmer is not responding". The board has a GPS module wired to pins 0 and 1, and the correct board and port are selected. What do you try first, and why?
Show answer
Disconnect the GPS module's wires from pins 0 and 1, then upload again. The bootloader communicates over those same pins, so a connected module can interfere with the handshake. Once the upload succeeds, reconnect the module.