resetATmega328PcountTimer001pin 131int main() {2 init();3 setup();4 for (;;) {5 loop();6 }7}1int count = 0;2void setup() {3 pinMode(13, OUTPUT);4}5void loop() {6 count = count + 1;7 digitalWrite(13, count % 2);8 delay(500);9}01210
Back: the board (reset, ATmega328P with Timer0 and count in SRAM, the LED on pin 13)Front: the hidden main() from the Arduino core, and your sketchTokens: the arrow (the line running now), new values of count, the bit for the LED, Timer0 ticksAn Arduino Uno R3 blinking its LED on pin 13. Your sketch has no main(): the Arduino core's hidden main() calls init(), then setup() once, then loop() inside for (;;), forever. Each pass adds one to the global count in SRAM and writes count % 2 to the LED, then delay(500) waits half a second. Set up once, then one endless loop: the superloop.

Before your code

  1. Plug in the Uno, or press reset, and the chip starts at its reset address. On the Uno that is a tiny program called a bootloader, kept at the end of flash: it runs first, then jumps to your code at the start of flash.

setup(): once

loop(): forever

What happens

;; step 1/10 · power
board   Arduino Uno R3  (ATmega328P, 16 MHz)
power on or reset  ->  jump to the reset address
bootloader  Optiboot, last 512 bytes of flash
            (BOOTRST fuse set: reset starts here)
            runs first, then jumps to address 0
your code   at the start of flash

The main() card is simplified. The real one in the Arduino AVR core (cores/arduino/main.cpp) also calls initVariant(), attaches USB on boards that have it, and calls serialEventRun() after each loop() when a sketch uses serial events. It ends with return 0, which is never reached. The figure moves slowly so you can follow it. On the board one pass of loop() takes just over 500 ms: the delay plus a few microseconds for the other two lines.