Course Content
Hands-On ESP32 Robotics: Build Smart Robots Step by Step

🔁 Setup() and Loop() Explained

🎯 Lesson Objective

In this lesson, students will understand:

• The purpose of the setup() function
• The purpose of the loop() function
• How these two functions control the execution of an Arduino program
• How microcontrollers repeatedly execute instructions

This lesson explains the two most important functions used in every Arduino program.


1️⃣ The Two Main Functions in Arduino Programming

Every Arduino or ESP32 program must contain two essential functions:

 
setup()
loop()
 

These functions form the core structure of every Arduino program.

The microcontroller executes these functions to control hardware and perform tasks.

Each function has a specific role in the program.


2️⃣ The setup() Function

The setup() function is the part of the program that runs only once when the microcontroller starts.

This happens when:

• The board is powered on
• The reset button is pressed
• A new program is uploaded

The setup() function is used to initialize the system and configure hardware components.


Common Tasks in setup()

Typical tasks performed in the setup() function include:

⚙️ Defining pin modes (INPUT or OUTPUT)
📡 Starting serial communication
🔧 Initializing sensors and modules
⚡ Preparing the system for operation

For example, before controlling an LED, we must first configure the pin connected to the LED as an output.

This configuration is done inside the setup() function.


3️⃣ The loop() Function

The loop() function contains the main logic of the program.

Unlike setup(), the loop() function runs continuously in an infinite loop.

This means that once the setup() function finishes, the microcontroller repeatedly executes the instructions inside the loop() function.

The loop continues running as long as the microcontroller has power.


Tasks Performed in loop()

The loop() function is used for tasks such as:

🔎 Reading sensor values
⚙️ Controlling motors
💡 Turning LEDs on or off
📡 Sending data to the serial monitor
🤖 Making decisions based on input data

Because the loop() function runs continuously, the robot can constantly monitor its environment and respond to changes.


4️⃣ Example of Program Execution

Let us understand how these two functions work together in a simple program.

1️⃣ The ESP32 receives power.
2️⃣ The microcontroller begins running the program.
3️⃣ The setup() function executes once and prepares the system.
4️⃣ After setup() finishes, the loop() function begins running.
5️⃣ The loop() function continues repeating indefinitely.

This repeating process allows the robot to operate continuously.


5️⃣ Example Program Structure

A simple Arduino program structure looks like this:

 
 
void setup() {

}

void loop() {

}

The setup() function is used for initialization.

The loop() function contains the instructions that run repeatedly.

This simple structure forms the foundation of all Arduino and ESP32 programs.


6️⃣ Example in a Robotics System

Let’s consider a simple robot example.

setup()

• Configure motor control pins as outputs
• Initialize sensor pins
• Start serial communication


loop()

• Read sensor values
• Check if an obstacle is detected
• Control motors based on sensor data
• Repeat continuously

This structure allows the robot to continuously monitor its environment and respond accordingly.


7️⃣ Why These Functions Are Important

Understanding setup() and loop() is essential because every robotics program depends on them.

These functions help organize the program into:

⚙️ Initialization stage (setup)
🔁 Continuous operation stage (loop)

Mastering these concepts will help you write more effective robotics programs.


🚀 What Happens Next

Now that you understand how setup() and loop() control program execution, the next step is to learn about variables and data types, which allow programs to store and process information.

Scroll to Top