Skip to content
LSLab

How Does Arduino Run setup and loop?

  • Basic
  • Article
  • Ages 13+
  • 4 min read
A microcontroller development board is connected by USB with its amber status LED illuminated.

An Arduino sketch normally defines two functions: setup() runs once at startup and loop() repeats while the sketch is running. After the board is powered up or reset, setup() runs first. Understanding these functions is a first step to reading an Arduino sketch.

Contents
The computer loads a sketch onto the board, which runs and sends signals.
  • Computer with the Arduino IDE
  • Arduino board
  • Output signal

What you will learn

Before you start

It helps to know what GPIO is.

What is Arduino?

Arduino is an open-source platform used for building electronics projects. The Arduino IDE uses a simplified version of C++, making it easier to learn to program.

The USB connection is also how you load code onto an Arduino board. The Arduino UNO contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable.

The example sketch has setup and loop; their placement in the code does not determine execution order.
  • Sketch
  • setup function
  • loop function

In what order do setup() and loop() run?

A new sketch in this tutorial is created with the two functions setup() and loop(). The setup() function runs once when the sketch runs, while the loop() function repeats continuously.

After the board is powered up, setup() runs first, followed by loop(). If both are removed and the sketch is left completely blank, it does not pass verification.

Setup runs once, loop returns to itself forever.
  • Power up
  • setup runs once
  • loop runs
  • Back to loop

Why does loop() have no pause?

There is no delay automatically built into the loop. When the code in loop() finishes, it starts again immediately.

To slow the loop down, a delay can be added with the delay() function. The first example sketch in the Arduino IDE is Blink.

In this simple loop, adding delay creates a pause; other operations can take time too.
  • Loop without delay
  • Loop with delay
  • The pause

Real-world example

With an adult, think of a blinking indicator light. The preparation is done once in setup(). Turning the light on and off again and again sits in loop().

Common misunderstandings

setup() repeats along with loop(). setup() runs once, while loop() is the one that repeats continuously.

loop() waits by itself between rounds. No delay is automatically built into the loop; delay() or another timing approach can space operations out.

Frequently asked questions

How do setup() and loop() differ?

setup() runs once when the sketch runs, while loop() repeats continuously.

Can loop() have an empty body?

Yes. The function can be present with no statements inside its braces. Removing both setup() and loop() leaves a blank sketch that does not pass verification.

What is delay() for?

To add a delay and slow the loop down.

How is code loaded onto an Arduino?

Through the USB connection.

Summary

Terms in this article

English and Vietnamese terms, with the meanings used here. Hover, tap or Tab to a term with a dotted underline for an explanation as you read.

Arduino
An open-source platform of boards and software for building electronics projects.
Sketch
A program written for Arduino, built around the setup() and loop() functions.
setup functionHàm setup
The function that runs once when the sketch starts after the board is powered up.
loop functionHàm loop
The function that repeats continuously after setup(), starting again as soon as it finishes.
Arduino IDE
The programming environment that uses a simplified version of C++ to write and load sketches.
delay functionHàm delay
A function that adds a pause to slow the loop down.

This article is not marked as viewed

Spotted something wrong or unsafe?

Tell the administrator by email. The message already has the article title and address.

Report this article
  • An air-quality sensor, two-antenna Wi-Fi gateway and tablet are arranged together on a lab bench.
    Basic

    How Does MQTT Route Messages Through a Broker?

    MQTT is a publish and subscribe protocol: a broker takes messages by topic and passes them to subscribers. Covers topics, wildcards and three QoS levels.

    Internet of Things (IoT)4 min read
  • A full-size microcontroller development board and a small wireless module sit side by side.
    Starter

    Overview of ESP32, Arduino and Other Boards

    An overview of ESP32 and Arduino boards and how to choose by connectivity, voltage, power and microcontroller.

    Internet of Things (IoT)4 min read
  • An industrial controller and a DIN-rail digital meter are connected by cable on a test bench.
    Basic

    How Does Modbus RTU Exchange Data?

    Modbus RTU works by request and response between a master and slaves. See addresses, function codes, error checks and how frames are split by silent gaps.

    Internet of Things (IoT)4 min read
  • Three measurement modules connect along one shared red-and-black twisted-pair cable.
    Basic

    How Does RS-485 Transmit Differential Signals?

    RS-485 sends signals over two wires and compares the voltage between them, so it resists noise, reaches far and lets many devices share one line.

    Internet of Things (IoT)4 min read
See all articles of the topic

Sources

These sources are written for older readers. If they feel hard, read them with an adult.