setup() and loop()
Every Arduino sketch is two functions: setup(), which runs once, and loop(), which runs for ever. The rest of main() is written for you, and a reset sends the chip back to the top of it.
Two functions you write
A sketch has to contain two functions, and the IDE's File, New Sketch gives you both, empty:
setup()runs once, when the chip starts. Pin modes,Serial.begin, and anything that has to happen before the work starts go here.loop()runs aftersetup()returns, and when it returns it is called again, as fast as the chip can manage, until the power goes.
Leave either one out and the build fails with an "undefined reference" error, even if the empty one would do nothing.
The main() you did not write
A C++ program starts at main(), and an Arduino sketch is no exception. The
Arduino AVR core supplies it, in cores/arduino/main.cpp, and your two
functions are called from inside it.
init() runs first. It turns interrupts on, starts Timer0 (the timer behind
millis() and delay()), sets Timer1 and Timer2 up for PWM, and sets the
ADC's clock. Then your setup(), once. Then a for (;;) with no way out,
calling loop() and, between passes, serialEventRun(), which calls a
serialEvent() function if your sketch has one.
So loop() is not special. It is an ordinary function that main() keeps
calling. Anything that should survive from one pass to the next has to live
outside it: a variable declared inside loop() is made again on every pass.
Where variables live
passes in the sketch is declared above setup(). That makes it a global: it
is made once, when the chip starts, and every function in the sketch can see
it. Move the line inside loop() and the monitor prints loop() pass 1 for
ever, because each pass makes a new passes at 0 and adds one.
A reset starts the whole thing again
Press RESET, unplug and plug in, or open the Serial Monitor (the CH340C's DTR
line reaches the reset pin through a capacitor,
C6), and the chip goes back to
the top of main(). Every variable is lost, because variables live in SRAM.
The sketch itself is in flash and is still there, so init() and setup()
run again and passes starts again at 0.
After a press of the RESET button, L may flicker quickly a few times before your sketch starts. That is the bootloader, the small program that receives uploads, listening for about a second in case one is coming. The standard UNO bootloader does this after a reset and skips it after power-up; which one a Nano carries has not been checked. A board whose lamp never flickers is not faulty; it may simply carry a different one.
The Serial Monitor is where the rest of this book reads the board, starting with the next page.
The code
Upload it, open the Serial Monitor at 9600 baud, and watch the count climb while the lamp on pin 13 blinks. Then press RESET.
// Setup_And_Loop.ino — setup() runs once, loop() runs for ever
// Tools > Board: Arduino Uno, or Arduino Nano with
// Processor ATmega328P (Old Bootloader if the upload says
// "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: none. Press RESET to watch setup() run again.
unsigned long passes = 0; // back to 0 at every start
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // 13: L on Uno, D13 on Nano
Serial.begin(9600);
Serial.println("setup() ran");
}
void loop() {
passes = passes + 1;
digitalWrite(LED_BUILTIN, passes % 2); // odd: on, even: off
Serial.print("loop() pass ");
Serial.println(passes);
delay(500);
}After RESET the monitor prints setup() ran again and the count starts over at 1. The lines above it stay, because the monitor keeps them, not the board.
When it does not work
Opening the Serial Monitor resets this board, so setup() runs again. That is the DTR line of the CH340C pulsing RESET through a capacitor, the same trick the upload uses. It is expected, and it is why the first lines you see are always the start of the sketch.
The variable is declared inside loop(), so it is made again, at its starting value, on every pass. Move the declaration above setup(), where it is made once and keeps its value between passes.
Check the braces. If the closing brace of setup() is in the wrong place, your line is inside setup(). Tools, Auto Format in the IDE re-indents the file and makes a misplaced brace easy to see.
Something is resetting it: the monitor being opened, a loose cable, or a power dip when a load switches on. A sketch that prints a line in setup(), as this one does, shows every restart in the monitor, which is the quickest way to tell a reset from a crash that hangs.
The window the sketch talks to, the baud menu that has to match, and the graph view.
The Serial Monitor and Plotter →Edit this page — content/books/atmega328p/setup-and-loop.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.