loop() Runs Forever
The heartbeat that repeats your logic endlessly.
loop() Runs Forever is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Heartbeat
After setup finishes, the second block takes over. Meet loop(), the part of your sketch that never stops running. 🔁
Runs Over and Over
The key idea: loop() repeats endlessly. The board runs it, reaches the bottom, then jumps straight back to the top.
What loop() Looks Like
Just like setup, it returns void and holds your code inside curly braces. This is where your device's behavior lives.
void loop() {
// repeating code goes here
}Setup First, Then Loop
Order matters. The board runs setup() once, then hands control to loop forever. They are partners, not rivals.
A Blinking Example
Inside loop you might turn an LED on, wait, then off. The board repeats this, so the light blinks again and again.
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}Reading Inputs Live
Because loop runs constantly, it can check sensors thousands of times a second. That is how your device stays responsive to the world.
No while(true) Needed
You do not write your own forever-loop. Arduino already repeats loop() for you, so just describe one pass of behavior.
It Never Truly Ends
loop only stops when power is cut or the board resets. As long as it has power, your loop() keeps cycling.
Order Inside Matters
Lines in loop run top to bottom each pass. Rearranging them changes the sequence of actions your device performs.
Empty Loop Is Allowed
An empty loop() compiles fine, but your device just sits idle. The braces are still required even with nothing inside.
void loop() {
}Together They Are a Sketch
Every Arduino program needs both blocks: setup() to prepare and loop() to act. With these two, you can build anything.
void setup() {
}
void loop() {
}Quick Check
Let's check how loop() behaves.
Recap: loop()
You learned that loop() runs forever after setup, repeating top to bottom. It is where your device senses and reacts on every pass. 🎉
Frequently asked questions
Is the “loop() Runs Forever” lesson free?
Yes — the full text of “loop() Runs Forever” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.
What will I learn in “loop() Runs Forever”?
The heartbeat that repeats your logic endlessly. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Arduino & IoT Academy?
No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “loop() Runs Forever” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Arduino & IoT Academy lesson?
Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- setup() Runs Once
- loop() Runs Forever
- Comments & Clean Sketches
- Compile Errors & How to Read Them