Back to resources
Chip programming
Firmware
HIL

Flashing an Arduino Uno R3 over USB with a BenchPod

How a BenchPod flashes an Arduino Uno R3 through its own USB cable: the ATmega16U2 serial bridge, the DTR reset, the STK500v1 subset optiboot speaks, bench results and one USB detail of this board.

Edward Viaene · November 23, 2026 · 9 min read

The Arduino Uno R3 is built around an ATmega328P with 32 KB of flash, and it ships with the optiboot bootloader in the top 512 bytes of that flash. You never touch an ISP header: you plug in the USB cable and the Arduino IDE or avrdude writes your sketch through it. The USB side is a second chip, an ATmega16U2, that presents itself to the computer as a serial port and passes the bytes to the 328P's UART.

A BenchPod can take the computer's place. Its USB port works as a USB host, so it can enumerate the Uno, reset it, talk to optiboot and write a sketch, with no computer attached to the board. That matters in a test rig: the pod flashes the board, then runs the test against it, and the whole thing works the same from a CI job. This article is part of a series, one per chip we have run on real hardware; the overview of how the BenchPod flashes microcontrollers lists them all. It covers the path from the USB cable to the flash, the part of the STK500v1 protocol optiboot answers, what we measured on an Uno R3 compatible board, and one USB detail of this board.

The path from USB to flash

Three things sit between the cable and the flash:

  1. The 16U2 enumerates as a USB CDC ACM device: vendor ID 2341, product ID 0043, manufacturer string "Arduino (www.arduino.cc)". Once the host sets the line parameters, it forwards bytes between USB and the 328P's UART.
  2. The auto-reset circuit. The 16U2's DTR line is coupled through a capacitor to the 328P's RESET pin. When the host drops DTR and raises it again, RESET gets a short pulse and the 328P restarts.
  3. optiboot runs first after every reset. It listens on the UART for a short window. If a programmer syncs with it in time, it takes commands and writes flash; if not, it starts the sketch.

So flashing an Uno is: toggle DTR, then talk to optiboot quickly, over a serial port that happens to be on the far side of a USB chip.

Host, pod and Uno: the host uploads the image, the pod's MCU hosts the Uno's USB port, and the 16U2 bridges to the ATmega328P
Host, pod and Uno: the host uploads the image, the pod's MCU hosts the Uno's USB port, and the 16U2 bridges to the ATmega328P

STK500v1 as optiboot speaks it

optiboot speaks a small subset of the STK500v1 protocol, the old Atmel starter-kit protocol that avrdude calls the arduino programmer. Every command is a byte string that ends with CRC_EOP, 0x20. Every answer starts with STK_INSYNC (0x14) and ends with STK_OK (0x10), with any data in between. A command that lacks the 0x20 end gets no 0x14: optiboot resets through its watchdog and starts the sketch.

These are the commands the pod uses:

CommandBytes sentAnswerWhat it does
GET_SYNC30 2014 10Checks that the bootloader is listening
READ_SIGN75 2014 sig0 sig1 sig2 10Reads the chip's 3-byte signature (1E 95 0F for an ATmega328P)
ENTER_PROGMODE50 2014 10Starts programming mode
LOAD_ADDRESS55 lo hi 2014 10Sets the word address for the next page
PROG_PAGE64 len_hi len_lo 46 data... 2014 10Writes one flash page (46 is F, the flash memory type)
READ_PAGE74 len_hi len_lo 46 2014 data... 10Reads one flash page back
LEAVE_PROGMODE51 2014 10Leaves programming mode; optiboot starts the sketch

Two details trip people up. First, LOAD_ADDRESS takes a word address: AVR flash is organized in 16-bit words, so byte address 0x0080 goes out as 0x0040. The pod divides the byte address by two. Second, there is no erase command to send. A page write erases the page it writes, which is why there is no erase phase for this bootloader.

One page on the wire: LOAD_ADDRESS with a word address, then PROG_PAGE with 128 bytes and the closing 0x20
One page on the wire: LOAD_ADDRESS with a word address, then PROG_PAGE with 128 bytes and the closing 0x20

The ATmega328P's flash page is 128 bytes. The pod writes the image page by page, padding the last page with 0xFF, then goes back and reads every page with READ_PAGE and compares it with the image. A mismatch ends the job with a verify error. The pod also reads the signature before writing and checks the image against the chip: an image that would not fit, or would overwrite the bootloader, is refused. For the ATmega328P that leaves 32256 bytes (32768 minus the 512-byte bootloader).

One session, step by step

The session between the pod and optiboot: DTR reset, sync, signature, programming mode, write, verify, leave
The session between the pod and optiboot: DTR reset, sync, signature, programming mode, write, verify, leave
  1. Reset. The pod opens the 16U2's serial port at 115200 baud and drops DTR and RTS. It waits 250 ms, raises both, waits 50 ms more and clears its receive buffer. The pulse reaches RESET through the capacitor and optiboot starts.
  2. Sync. The pod sends 30 20 and waits up to 200 ms for 14 10. It tries up to 8 times, because optiboot's listening window is short and the first bytes can arrive while the chip is still coming out of reset.
  3. Signature and programming mode. READ_SIGN, then ENTER_PROGMODE.
  4. Write. For each page: LOAD_ADDRESS, then PROG_PAGE.
  5. Verify. For each page: LOAD_ADDRESS, then READ_PAGE, compared with the image.
  6. Leave. LEAVE_PROGMODE, and the new sketch runs.

The pod does not know in advance which bootloader a board has. Without a better hint it tries them in order: STK500v1 at 115200 baud (optiboot, the Uno and newer Nanos), then STK500v2 at 115200 (the Mega 2560 bootloader), then STK500v1 at 57600 (the older Nano and Pro Mini bootloader), then AVR109 (a 32u4 board already in its Caterina bootloader). Each attempt starts with its own DTR reset. The Uno answers the first probe. Boards with native USB that run a sketch, such as the Leonardo, take a 1200 baud touch instead of a DTR reset; the Uno's 16U2 product ID is not one of those, so it gets the reset.

How the pod runs it

The work is split three ways:

  • The host (the Python SDK, the CLI or the MCP server) reads the .hex, converts it to a binary with its start address, uploads it into the pod's memory and starts the job. It then polls the job's progress.
  • The pod's MCU is the USB host. It enumerates the Uno, opens the 16U2's serial port, sets baud rate and the DTR and RTS lines, and runs the STK500v1 exchange above as a state machine that does one exchange per step. The pod keeps answering other commands between steps, and it holds off captures, DAC playback and uploads while the flash runs. The protocol runs next to the USB port, so it does not depend on network latency: the same job takes the same time over the LAN or the cloud.
  • The FPGA is not involved. No logic analyzer pin carries any of this; the only connection to the Uno is the USB cable.

The pod reports the bootloader it found (stk500v1 here) and the phase it is in (start, reset, sync, info, write, verify, leave, done), and done and total count bytes: first written, then verified.

Wiring

One USB cable, into the pod's USB-C connector ("port":"usb_c") through a USB-A breakout: D+, D- and GND to the board, and 5 V from a supply into VBUS. The USB docs page has the details.

While the pod is a USB host it is off the computer's USB: no USB console and no USB network adapter. Start host mode over the network (LAN or Wi-Fi) or the cloud. Host mode ends by itself after its timeout (1 to 3600 seconds, default 60), so a client that disappears never leaves the pod off USB.

Bench results

We ran an Elegoo Uno R3 (an Uno R3 compatible board) on a USB-A breakout wired to the pod's USB-C port:

  • It enumerated in 0.44 s and reported itself as "Arduino (www.arduino.cc)", 2341:0043.
  • usb_flash in avr mode found optiboot on the first probe and wrote and verified a sketch in 0.77 s.
  • The pod's USB checks on the same board passed: enumerate, power, suspend and resume, reset, and replug.
  • The pod's USB serial port reached optiboot on the same board, so a test can talk to the board over the cable it was flashed through.

A USB host note: endpoint number 4

The 16U2 puts its CDC data OUT endpoint on endpoint number 4. Many simple USB devices use endpoint numbers 1 to 3 only, so a USB host stack that sizes its endpoint tables by endpoint number must allow numbers above 3. USB allows endpoint numbers up to 15, and the pod's host stack covers all of them. If a host enumerates an Uno but never mounts it, check this first.

Using it

From Python, with the pytest package:

from embeddedci.benchpod import BenchPod

with BenchPod("192.168.1.50") as bp:
    bp.usb_host(timeout_s=120)          # the pod becomes the Uno's USB host
    print(bp.usb_wait_device().vid_pid) # 2341:0043
    res = bp.usb_flash("build/sketch.ino.hex")
    print(res.mode, res.reason)         # avr, and why the SDK chose it
    bp.usb_host(False)                  # back to being a USB device

There is no mode to choose: the SDK picks avr from the file and the device that mounted. A failure raises UsbFlashError with the pod's result, phase and mode. leave=False keeps the board in its bootloader.

From the command line (use the pod's network address, not --connection usb):

benchpod usb-flash build/sketch.ino.hex

The CLI starts host mode when it is off, waits for the board, shows progress and exits non-zero with the pod's reason when the flash fails.

From an AI agent, the MCP server's usb_flash tool takes the file the same way, and usb_host reports what enumerated.

Underneath all three is one JSON command. Start host mode, stage the binary with load_bin into the pod's memory, then:

{"cmd":"usb_host","enabled":true,"port":"usb_c","timeout_s":120}
{"cmd":"usb_flash","mode":"avr","address":0,"len":10240}
{"cmd":"usb_flash"}

The last command reads the state: phase, done and total, the protocol found and, at the end, ok or the reason it failed. The full reference is in the API docs and the USB page.

The pod's USB serial port gives the Uno's own serial console over the same cable: uart_proxy_start with "port":"usb", or port="usb" on the SDK's open_uart and capture_uart. While that port is open, usb_flash is refused, so close it before flashing.

USB flashing needs pod firmware 4.0.3 or later. The same avr mode covers other Arduino boards with an AVR bootloader; the USB page lists the rest of the loaders the pod can drive over USB. Every chip and method we have verified on real hardware is in How the BenchPod flashes microcontrollers.