What is the purpose of setup() and loop() functions in Arduino?
The setup()
and loop()
functions are the core structure of every Arduino sketch. The Arduino software (IDE) relies on this structure to know how to run your program.
Here’s a breakdown of each function's purpose:
1. The setup()
Function
Purpose: To initialize your program and run code only once at the very beginning.
When it runs: Immediately after the Arduino is powered on or reset.
What it's used for:
Setting pin modes (e.g.,
pinMode(13, OUTPUT);
to set pin 13 as an output).Initializing communication (e.g.,
Serial.begin(9600);
to start serial communication for debugging).Setting initial values for variables.
Running one-time startup routines (e.g., turning on an LED to indicate power-on).
Think of it like: The "setup" or "preparation" phase before a race. You get into your starting blocks, get focused, and wait for the signal. This happens only once.
2. The loop()
Function
Purpose: To contain the main logic of your program, which runs continuously and repeats forever.
When it runs: Immediately after the
setup()
function finishes. Once it reaches the end of theloop()
, it automatically jumps back to the beginning and runs again, and again, and again...What it's used for:
Reading sensor values (e.g.,
sensorValue = analogRead(A0);
).Making decisions based on those readings (using
if
,while
statements).Sending output signals (e.g.,
digitalWrite(13, HIGH);
to turn on an LED).Controlling motors, displays, and other components.
Think of it like: The race itself. Once the starting gun fires (after setup()
), the runner (the microcontroller) runs the lap (the loop()
code). When they finish the lap, they immediately start the next one, and this continues indefinitely.
Analogy: A Microwave Oven
setup()
: When you first plug in the microwave. It runs a self-test, lights up the display, and gets all its electronics ready. This happens once.loop()
: Once it's powered on, it constantly loops, doing three things:Check Inputs: Is the user pressing a button? Is the door closed?
Make Decisions: If the 'Start' button is pressed and the door is closed, then turn on the magnetron.
Set Outputs: Update the timer on the display, spin the turntable, beep when done.
This "check inputs -> make decisions -> set outputs" cycle repeats forever until you unplug it.
Code Example: Blinking LED
This classic example perfectly demonstrates the two functions:
// setup() runs ONCE at the start void setup() { // Initialize the digital pin 13 (the built-in LED) as an output. pinMode(13, OUTPUT); } // loop() runs over and over again FOREVER void loop() { digitalWrite(13, HIGH); // Turn the LED ON delay(1000); // Wait for 1000 milliseconds (1 second) digitalWrite(13, LOW); // Turn the LED OFF delay(1000); // Wait for another second }
In
setup()
: We do the one-time initialization of telling the Arduino that pin 13 will be used to output voltage.In
loop()
: We create the continuous, repeating behavior: turn on, wait, turn off, wait. This sequence will repeat for as long as the Arduino has power.
Summary Table
Feature | setup() | loop() |
---|---|---|
Runs... | Once at startup | Continuously and repeatedly |
Purpose | Initialization | Main Program Logic |
Analogy | Getting ready for the race | Running the race laps |
Even if your program seems to do nothing "continuous," like just waiting for a button press, the loop()
function is still constantly running, checking over and over again for that button press. This structure is what makes the Arduino ideal for real-time interactive projects.
评论
发表评论