setup() and loop(): the superloop
About this lesson
An Arduino sketch has no main(), yet it runs. Follow an Arduino Uno R3 from power on: startup code sets your globals, the Arduino core's hidden main() calls init(), then your setup() once, then your loop() again and again inside for (;;). One pass at a time, a global count goes up and the LED on pin 13 turns on and off. Set up once, then loop forever: that shape is called the superloop. The C++ basics lessons cover the language itself; here we stay with how the firmware runs.
Power on or reset
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.
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 flashRead the lesson step by step
Power on or reset
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.
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 flashGlobals get their first values
Before any of your functions run, startup code gets memory ready. Global variables, declared outside every function, get their first values: count = 0 is written into SRAM. A global stays there for the whole run.
sketch line 1 int count = 0; global declared outside every function startup code (before main) clears it to 0 SRAM count = 0, kept until power offThe hidden main()
Every C++ program starts in a function called main(). Your sketch has none: the Arduino core supplies it, hidden from you. The arrow marks the line running now. This is where the code you can see begins.
C++ rule every program starts in main() sketch has no main() core cores/arduino/main.cpp supplies it int main() { init(); setup(); for (;;) { loop(); } }init() starts the timers
main() first calls init(). It sets up the chip's timers. Timer0 counts clock ticks and interrupts the program every 1.024 ms, so millis() and delay() can tell the time. init() also turns interrupts on.
main line 2 init(); sei() interrupts on Timer0 1 tick per 64 clock cycles overflow every 256 ticks 64 x 256 = 16,384 cycles = 1,024 µs at 16 MHz millis() counts those overflowssetup() runs once
Next, main() calls setup(). The arrow jumps into your sketch. pinMode(13, OUTPUT) makes pin 13, wired to the board's LED, an output. Then setup() ends and the arrow returns to main(). setup() runs once per power on or reset.
main line 3 setup(); jump into the sketch sketch line 3 pinMode(13, OUTPUT); pin 13 now an output, starts LOW (LED off) sketch line 4 } return to main() setup() once per power on or resetfor (;;) calls loop()
Now main() reaches for (;;): a loop with no exit test, so it never ends. Inside, it calls loop(). The first line adds one to count: 0 + 1 = 1, written back into SRAM.
main line 4 for (;;) { no test: repeats forever main line 5 loop(); jump into the sketch sketch line 6 count = count + 1; count = 0 + 1 = 1Pin 13 HIGH: LED on
digitalWrite(13, count % 2). The % sign gives the remainder after dividing: 1 % 2 = 1. Writing 1, which is HIGH, puts 5 volts on pin 13 and the LED turns on.
sketch line 7 digitalWrite(13, count % 2); count % 2 1 % 2 = 1 (remainder) 1 = HIGH pin 13 at 5 V, LED ondelay(500) waits
delay(500) waits 500 milliseconds, half a second. Your code does nothing else meanwhile: delay() just watches the time Timer0 keeps. Interrupts still run, but no other line of your sketch can.
sketch line 8 delay(500); waits 500 ms, about 488 Timer0 overflows meanwhile interrupts run, your sketch does notRound again: LED off
loop() ends and returns to main(). The for (;;) goes round and calls loop() again. count still holds 1, because globals keep their values: 1 + 1 = 2, and 2 % 2 = 0, so the LED turns off.
sketch line 9 } return to main() main line 4 for (;;) round again main line 5 loop(); back into the sketch sketch line 6 count = 1 + 1 = 2 sketch line 7 2 % 2 = 0 = LOW, LED offThe superloop
This is the superloop: set up once, then run one loop forever, with no operating system. Each pass does every job in turn, here about half a second per pass. So a slow job delays every other job.
superloop setup once, then loop forever pass about 500 ms (mostly the delay) LED on one pass, off the next: a 1,000 ms blink count 1 -> 1 % 2 = 1 -> LED on count 2 -> 2 % 2 = 0 -> LED off count 3 -> 3 % 2 = 1 -> LED on count 4 -> 4 % 2 = 0 -> LED off one slow job delays every other job
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01An Arduino sketch has no main(). So where does the program start?
Every C++ program starts in main(). The Arduino core (cores/arduino/main.cpp) supplies it: it calls init(), then setup() once, then loop() forever.
02How many times does setup() run?
main() calls setup() once before it reaches the endless for (;;). Pressing reset or cycling power starts everything again, so setup() runs once more.
03count is a global that starts at 0, and each pass runs count = count + 1, then digitalWrite(13, count % 2). After the third pass, what are count and the LED?
A global keeps its value between passes: 1, 2, 3. 3 % 2 = 1, which is HIGH, so pin 13 goes to 5 V and the LED is on.
04What happens in your sketch while delay(500) is running?
delay() waits, watching the time that Timer0's interrupt keeps (one every 1.024 ms). Interrupts still run, but no other line of your sketch can until it returns.
05Which description fits a superloop?
setup() once, then loop() called forever by for (;;). Because jobs take turns, one slow job delays all the others; later lessons replace delay() with non-blocking code, interrupts and RTOS tasks.
Glossary
- main()
- The function where every C and C++ program starts. On Arduino the core supplies it, so your sketch does not write one.
- setup()
- Your function for one-time work, such as setting pin modes. It runs once after each power on or reset.
- loop()
- Your function for the repeating work. main() calls it again and again inside for (;;).
- for (;;)
- A C/C++ loop with no exit test, so it repeats forever.
- Global variable
- A variable declared outside every function. It lives in SRAM for the whole run and keeps its value between passes of loop().
- Bootloader
- A small program in flash that runs first at reset. On the Uno it lets the board take a new program over USB, then starts yours.
- Timer0
- A counter inside the ATmega328P. The Arduino core sets it to interrupt every 1.024 ms, which is how millis() and delay() keep time.
- Superloop
- A program shape with no operating system: set up once, then run one loop forever that does every job in turn.
Notes and sources
- 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.
- Before main(), startup code from avr-libc and libgcc sets up the stack, copies the first values of globals from flash into SRAM, and clears the rest to zero. A global set to 0, like count, sits in the cleared part (.bss).
- The Uno keeps 512 bytes of its 32 KB flash for the Optiboot bootloader (32,768 − 32,256 bytes, from the core's boards.txt). The Uno's fuses set BOOTRST (high fuse 0xDE in boards.txt), so a reset jumps to this boot section at the end of flash, not to address 0. Optiboot lets the board take a new program over USB, then jumps to yours at the start of flash.
- 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.
- On an ESP32 the Arduino core runs setup() and loop() inside a FreeRTOS task called loopTask (8,192-byte stack by default), so other tasks can run beside it. That is still a loop that never ends, but now a scheduler shares the chip.
- Arduino AVR core: cores/arduino/main.cpp (the hidden main())
- Arduino AVR core: cores/arduino/wiring.c (init(), Timer0, delay())
- Arduino AVR core: cores/arduino/Arduino.h (HIGH, LOW, OUTPUT)
- Arduino AVR core: variants/standard/pins_arduino.h (LED_BUILTIN 13)
- Arduino AVR core: boards.txt (Uno: ATmega328P, 16 MHz, Optiboot)
- Microchip ATmega328P datasheet (DS40002061A), ch. 27 Boot Loader Support: Boot Reset Fuse
- Arduino language reference: setup() (source)
- Arduino language reference: loop() (source)
- Arduino language reference: delay() (source)
- Arduino language reference: digitalWrite() (source)
- Arduino language reference: % remainder (source)
- Arduino language reference: variable scope (source)
- avr-libc user manual: Memory Sections (.data, .bss, .init)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 1 (UT Austin)
- E. A. Lee and S. A. Seshia, Introduction to Embedded Systems, 2nd ed. (MIT Press, 2017)
- Espressif arduino-esp32: cores/esp32/main.cpp (loopTask)
- ESP-IDF Programming Guide: Application Startup Flow (ESP32)
- FreeRTOS kernel source (README)
Quiz Unlocks after the last step.