Course Content
IoT Engineering Course using ESP32 Wifi Robots

📘 Structure of Arduino Program


🎯 Lesson Objective

By the end of this lesson, students will:

  • Understand how an Arduino/ESP32 program is structured

  • Learn what setup() and loop() functions are

  • Understand program execution flow

  • Learn how microcontrollers execute code continuously

  • Understand why loop() never stops

  • Understand how this structure applies to IoT systems


1️⃣ Why Arduino Has a Fixed Structure

Unlike normal C/C++ programs, Arduino programs follow a fixed template.

Every Arduino/ESP32 program must contain:

 
void setup() {
}

void loop() {
}
 

These two functions are mandatory.

Without them → Code will not run.


2️⃣ What is setup()?

🔹 Definition

setup() is a function that runs only once when the device starts.

It is executed:

  • When ESP32 is powered ON

  • When reset button is pressed

  • When code is uploaded


🔹 What Do We Put Inside setup()?

We initialize things here:

  • Serial communication

  • Pin modes

  • WiFi connection

  • Library initialization

  • Sensor startup

Example:

 
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
}
 

This code:

  • Starts Serial Monitor

  • Sets GPIO 2 as output

After this, setup() finishes.

It will not run again (unless reset).


3️⃣ What is loop()?

🔹 Definition

loop() is a function that runs repeatedly forever.

After setup() completes:

ESP32 enters loop()
And keeps repeating it continuously.


🔹 Why Does loop() Run Forever?

Microcontrollers are designed to:

  • Monitor sensors continuously

  • React to events

  • Run automation logic

They never “close” like a computer program.

They always stay active.


🔹 Example

 
void loop() {
Serial.println("Running...");
delay(1000);
}
 

Output:

Running…
Running…
Running…

This continues forever.


4️⃣ How Program Execution Works

Let’s understand the internal flow.

When ESP32 powers ON:

Step 1 → Load program from memory
Step 2 → Run setup() once
Step 3 → Enter loop()
Step 4 → Repeat loop() infinitely

Flow diagram:

Power ON

setup()

loop() → loop() → loop() → loop() → forever


5️⃣ Why setup() and loop() Are Separated

This separation keeps code organized.

setup() → Initialization

Only things that need to run once.

loop() → Repeated tasks

  • Sensor reading

  • Decision making

  • Communication

  • Control logic

This makes embedded systems efficient.


6️⃣ Real IoT Example

Let’s take Smart AC system.

setup()

  • Start Serial

  • Connect to WiFi

  • Initialize DHT sensor

  • Set relay pin mode

loop()

  • Read temperature

  • Compare with setpoint

  • Turn AC ON/OFF

  • Send data to Blynk

This pattern repeats in every IoT project.


7️⃣ What Happens If loop() Has No Delay?

Example:

 
void loop() {
Serial.println("Fast");
}
 

This will print extremely fast.

Thousands of lines per second.

Why?

Because loop() runs continuously with no pause.


8️⃣ What is delay()?

delay(milliseconds);

It pauses program execution for given time.

Example:

 
delay(1000);
 

Means wait 1 second.

This slows down loop execution.


9️⃣ Important Concept – Infinite Loop

In normal C programming, main() function runs once.

In Arduino, loop() acts like:

 
while(true) {
}
 

Which means:

Repeat forever.

This is required for real-time systems.


🔟 Where Do We Declare Variables?

Variables can be declared:

🔹 Outside functions (Global Variables)

Accessible everywhere.

Example:

 
int temperature;
 

🔹 Inside functions (Local Variables)

Accessible only inside that function.

Example:

 
void loop() {
int value = 10;
}
 

Understanding scope is important.


1️⃣1️⃣ What Happens If We Remove loop()?

Program will not compile.

Arduino requires both:

  • setup()

  • loop()

These are mandatory.


1️⃣2️⃣ What Happens During Reset?

When you press reset button:

  • ESP32 restarts

  • setup() runs again

  • loop() restarts

Reset clears runtime state.


1️⃣3️⃣ Advanced Insight – Hidden main()

Internally, Arduino has something like:

 
int main() {
setup();
while(true) {
loop();
}
}
 

This is hidden from user.

Arduino simplifies it.


1️⃣4️⃣ Common Beginner Mistakes

❌ Putting WiFi.begin() inside loop()
❌ Putting pinMode() inside loop()
❌ Forgetting Serial.begin()
❌ Using heavy code inside setup()
❌ Blocking loop with long delays

Always remember:

Initialization → setup()
Repeating logic → loop()


1️⃣5️⃣ Clean Program Example

 
int ledPin = 2;

void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
}

void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
 

This follows correct structure.


📌 Lesson Summary

In this lesson, we learned:

  • Every Arduino program has setup() and loop()

  • setup() runs once

  • loop() runs forever

  • Program execution flow

  • Initialization vs repeating logic

  • Infinite loop concept

  • Where to declare variables

You now understand how microcontrollers execute programs.

Scroll to Top