Menu
CoddyTech

Setup and Loop

Part of the First Circuits section of Coddy's Arduino journey. Lesson 2 of 22.

A sketch has two parts. When the board gets power, it runs setup() one time. Then it runs loop(), and every time loop() reaches its last line it starts again from the top, forever.

void setup() {
  // 1. runs first, one time
}

void loop() {
  // 2. runs next, then 3. again, 4. again...
}

The curly braces { } hold the lines that belong to each part. Each instruction ends with a semicolon ;, the way a sentence ends with a full stop.

What has to happen once goes in setup(): making a pin an output, or turning on an LED that should stay on. What has to keep happening, like a blink, goes in loop().

A pin keeps the last value it was given. After digitalWrite(13, HIGH) the LED stays on until something sets the pin LOW, even if loop() is empty.

challenge icon

Challenge

Easy

Turn the LED on pin 13 on and keep it on:

  • in setup(), make pin 13 an output
  • in setup() too, set pin 13 HIGH
  • leave loop() empty

Press Run to watch it, then Check.

Try it yourself

void setup() {
  // runs once: make pin 13 an output and turn it on here
}

void loop() {
  // runs again and again: nothing to do here this time
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in First Circuits

Practice on your own: Arduino playground