Course Content
📘 MODULE 11 – Edge Avoiding Robot
📦 MODULE 12 – Smart Multi-Function Robot (Mega Project)
Arduino Hands-On Programming and Robotics Course

📘 Lesson P2 – Structure of an Arduino Program

🎯 Learning Objectives

After completing this lesson, students will be able to:

✅ Understand the structure of an Arduino program

✅ Understand how Arduino executes code

✅ Understand the purpose of setup()

✅ Understand the purpose of loop()

✅ Understand the sequence of program execution

✅ Write simple Arduino programs

✅ Understand comments in programming

✅ Understand the Arduino program life cycle


1. Introduction

Every language has a structure.

For example:

English Sentence

Subject + Verb + Object

Example:

 
Shiv writes code.
 

Similarly, Arduino programs also follow a specific structure.

Without proper structure:

  • Program will not compile
  • Arduino will not understand the instructions
  • Errors will occur

Therefore understanding the structure of an Arduino program is the first step toward writing successful code.


2. What is a Sketch?

In Arduino, a program is called a:

Sketch

Whenever you create a new file in Arduino IDE, you create a sketch.

Examples:

  • LED Blink Sketch
  • Servo Control Sketch
  • Robot Sketch
  • Sensor Reading Sketch

All Arduino programs are called sketches.


3. Basic Arduino Program Structure

Every Arduino program contains two important functions:

 
void setup()
{

}

void loop()
{

}
 

These two functions are mandatory.

Without them, Arduino programs cannot work properly.


4. Understanding Program Execution

When Arduino receives power:

Step 1

Arduino starts.

Step 2

setup() runs.

Step 3

setup() finishes.

Step 4

loop() starts.

Step 5

loop() repeats forever.


Visual Representation

 
Power ON
   ↓
setup()
   ↓
loop()
   ↓
loop()
   ↓
loop()
   ↓
loop()
   ↓
Forever

5. Understanding setup()

The setup() function runs only once.

Syntax:

void setup()
{

}
Purpose:

Used for initialization.


What Does Initialization Mean?

Initialization means preparing everything before the main program starts.

Examples:

  • Setting pin modes
  • Starting Serial Communication
  • Initializing sensors
  • Configuring modules

Example

void setup()
{
pinMode(13, OUTPUT);
}

Here Arduino prepares Pin 13 as an output.

This needs to be done only once.


Real-Life Example

Imagine starting a classroom.

Before teaching:

  • Open the classroom
  • Switch on lights
  • Arrange chairs

These tasks happen once.

This is similar to setup().


6. Understanding loop()

The loop() function runs continuously.

Syntax:

void loop()
{

}

After setup() finishes:

Arduino enters loop().

The loop repeats forever until power is removed.


Example

void loop()
{
digitalWrite(13,HIGH);
}

Arduino continuously executes this instruction.


Real-Life Example

Imagine a security guard.

He continuously checks:

  • Gate
  • Visitors
  • Cameras

Again and again.

This continuous activity is similar to loop().


7. Why Does loop() Repeat Forever?

Microcontrollers are designed for continuous operation.

Examples:

Traffic Signal:

Must continuously work.

Robot:

Must continuously monitor sensors.

Smart Door:

Must continuously check access.

Therefore Arduino keeps executing loop() repeatedly.


8. Complete Example Program

void setup()
{
pinMode(13,OUTPUT);
}

void loop()
{
digitalWrite(13,HIGH);
}

What happens?

Step 1:

Pin 13 configured as output.

Step 2:

LED turns ON.

Step 3:

loop repeats.

LED remains ON.


9. Program Flow Analysis

Consider:

void setup()
{
Serial.begin(9600);
}

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

Execution:

Power ON

Start Serial Communication

Print Hello

Print Hello

Print Hello

Repeat Forever


10. What is a Function?

A function is a block of code designed to perform a specific task.

Example:

void setup()
{

}

setup() is a function.


void loop()
{

}

loop() is also a function.

Functions help organize programs.


11. Understanding Curly Braces

Every function contains:

 
{

}

These braces define the beginning and end of a block.

Example:

void setup()
{

}

Everything inside belongs to setup().


Common Mistake

Wrong:

void setup()
{

Missing closing brace.

Compiler error occurs.


12. Understanding Semicolon

Most Arduino statements end with:

;
Example:
pinMode(13,OUTPUT);
Semicolon tells the compiler:

Instruction completed.


Common Mistake

Wrong:

pinMode(13,OUTPUT)
Missing semicolon.

Compiler generates an error.


13. Comments in Arduino

Comments are notes written for humans.

Arduino ignores comments.


Single-Line Comment

// LED connected to Pin 13
 

Multi-Line Comment

/*
This program
blinks an LED
*/

Why Comments Are Important

Comments help:

  • Explain code
  • Improve readability
  • Make debugging easier

Professional programmers always use comments.


14. Program Life Cycle

Every Arduino program follows this life cycle:

Power ON
    ↓
Initialize Hardware
    ↓
Run setup()
    ↓
Run loop()
    ↓
Repeat Forever

This cycle is the foundation of all Arduino projects.


15. Example: LED Blink Logic

Algorithm:

Step 1

Set Pin 13 as OUTPUT.

Step 2

Turn LED ON.

Step 3

Wait.

Step 4

Turn LED OFF.

Step 5

Wait.

Repeat.

This algorithm will later become our first Arduino project.


16. Common Beginner Mistakes

Mistake 1

Writing code outside setup() or loop()

Wrong:

 
pinMode(13,OUTPUT);
 

Outside any function.


Mistake 2

Missing braces.


Mistake 3

Missing semicolons.


Mistake 4

Confusing setup() and loop().

Remember:

setup() → Once

loop() → Forever


17. Best Practices

Use Proper Indentation

Makes code readable.


Add Comments

Helps understanding.


Organize Code

Avoid messy programs.


Test Frequently

Find errors quickly.


Read Error Messages

Compiler often tells exactly what is wrong.


18. Real-World Example

Automatic Water Dispenser

setup()

Initialize:

  • Sensor
  • Relay
  • Pump

loop()

Continuously:

  • Check hand detection
  • Turn pump ON/OFF

This shows how almost every Arduino project uses setup() and loop().


📊 Summary

In this lesson, we learned:

✅ What a Sketch is

✅ Arduino program structure

✅ setup() function

✅ loop() function

✅ Program execution flow

✅ Functions

✅ Comments

✅ Curly braces

✅ Semicolons

Understanding the structure of an Arduino program is essential because every project, robot, and automation system built in this course will follow the same structure.


📖 Key Terms

Sketch

Arduino program file.

Function

Block of code that performs a task.

setup()

Function that runs once.

loop()

Function that runs repeatedly.

Comment

Text ignored by Arduino, used for explanation.

Semicolon (;)

Marks the end of a statement.

Curly Braces {}

Define code blocks.


🎯 Quiz

1. What is an Arduino program called?

A. Script

B. Sketch ✅

C. File

D. Module


2. Which function runs only once?

A. loop()

B. setup() ✅

C. main()

D. start()


3. Which function runs continuously?

A. setup()

B. start()

C. loop() ✅

D. run()


4. What symbol ends most Arduino statements?

A. :

B. ,

C. ; ✅

D. #


5. What are comments used for?

A. Faster execution

B. Explaining code ✅

C. Power supply

D. Uploading code


🏠 Assignment

Task 1

Write the basic Arduino program structure from memory.

Task 2

Explain the difference between setup() and loop().

Task 3

Create a flowchart showing Arduino program execution.

Task 4

Write five examples of comments.

Task 5

Draw a diagram showing the Arduino program life cycle from power ON to continuous execution.

Scroll to Top