0
0
AutocadConceptBeginner · 3 min read

What is setup and loop in Arduino: Simple Explanation

In Arduino programming, setup() is a function that runs once when the board starts to set up initial settings. The loop() function runs repeatedly after setup(), allowing the Arduino to perform tasks continuously.
⚙️

How It Works

Think of setup() as the moment you prepare your workspace before starting a project. It runs only once to get everything ready, like turning on tools or setting initial conditions.

After that, loop() is like the main work cycle you repeat over and over, such as checking sensors or blinking lights continuously. This cycle keeps running until you turn off the Arduino.

This design helps the Arduino keep doing tasks without stopping, making it perfect for projects that need constant monitoring or control.

💻

Example

This example turns an LED on and off repeatedly using setup() and loop(). The setup() sets the LED pin as output once, and loop() turns the LED on and off forever.

arduino
void setup() {
  pinMode(13, OUTPUT); // Set pin 13 as an output
}

void loop() {
  digitalWrite(13, HIGH); // Turn LED on
  delay(1000);           // Wait 1 second
  digitalWrite(13, LOW);  // Turn LED off
  delay(1000);           // Wait 1 second
}
Output
The LED connected to pin 13 blinks on for 1 second and off for 1 second repeatedly.
🎯

When to Use

Use setup() to prepare your Arduino before it starts working, like setting pin modes or starting communication. Use loop() for tasks that need to happen again and again, such as reading sensors, controlling motors, or updating displays.

For example, a temperature monitor uses setup() to start the sensor and loop() to read and show temperature continuously.

Key Points

  • setup() runs once at the start to set up your Arduino.
  • loop() runs repeatedly to keep your program active.
  • This structure makes Arduino ideal for continuous tasks.
  • Every Arduino sketch must have both setup() and loop() functions.

Key Takeaways

setup() runs once to prepare the Arduino.
loop() runs repeatedly to perform ongoing tasks.
Use setup() for initial settings and loop() for continuous actions.
Both functions are required in every Arduino program.