🧾 Understanding the Arduino Programming Structure
🎯 Lesson Objective
In this lesson, students will understand:
• The basic structure of an Arduino program
• The main parts of a robotics program
• How programs are organized in Arduino IDE
• The role of different sections in a program
This lesson introduces the standard structure used when writing programs for ESP32 and other microcontrollers using Arduino IDE.
1️⃣ What is an Arduino Program?
A program written in the Arduino IDE is called a sketch.
A sketch is a set of instructions that tells the microcontroller how to interact with hardware components such as:
🔎 Sensors
⚙️ Motors
💡 LEDs
📡 Communication modules
These instructions are written using the Arduino programming language, which is based on C/C++.
Every Arduino program follows a standard structure that helps the microcontroller execute instructions correctly.
2️⃣ Main Sections of an Arduino Program
An Arduino program is usually divided into several sections.
These sections help organize the code and make it easier to understand.
The main sections include:
• Library declarations
• Global variable declarations
• Setup function
• Loop function
• Additional functions
Each section plays a specific role in controlling the behavior of the program.
3️⃣ Library Section
Libraries are collections of pre-written code that help control hardware devices easily.
Instead of writing complex code from scratch, libraries provide ready-made functions that simplify programming.
For example:
• Servo motor control libraries
• Ultrasonic sensor libraries
• Communication libraries
Libraries are usually included at the beginning of the program.
Using libraries helps make programs shorter, simpler, and easier to manage.
4️⃣ Global Variables
Global variables are variables declared outside the main program functions.
These variables are accessible throughout the entire program.
Global variables are commonly used to store:
• Pin numbers
• Sensor values
• Motor control variables
• System states
For example, we may define which pin controls a motor or which pin reads a sensor signal.
Declaring variables at the beginning of the program improves code organization and readability.
5️⃣ Setup Function
The setup() function is one of the most important parts of an Arduino program.
This function runs only once when the microcontroller starts.
The setup() function is used to perform initialization tasks such as:
⚙️ Configuring pin modes
📡 Starting serial communication
🔧 Initializing sensors and devices
Since setup() runs only once, it prepares the system before the main program begins.
6️⃣ Loop Function
The loop() function is the main part of the program.
This function runs continuously while the microcontroller is powered.
All the main logic of the robot is written inside the loop() function.
For example, the loop may perform actions such as:
🔎 Reading sensor values
⚙️ Controlling motors
📡 Sending data to the serial monitor
🤖 Making decisions based on sensor input
Because loop() repeats continuously, the robot can respond to environmental changes in real time.
7️⃣ Additional Functions
In larger programs, additional functions can be created to organize code more effectively.
Functions allow developers to group related instructions together.
For example, we might create functions such as:
• moveForward()
• turnLeft()
• stopRobot()
Using functions makes programs:
✔ Easier to read
✔ Easier to maintain
✔ Easier to debug
This approach helps keep complex robotics programs organized.
8️⃣ Why Program Structure is Important
Following a proper program structure helps developers write clean and organized code.
Benefits include:
✔ Easier debugging
✔ Better readability
✔ Improved code reuse
✔ Simpler project management
As robotics systems become more complex, maintaining a clear structure becomes even more important.
🚀 What Happens Next
Now that you understand the basic structure of an Arduino program, the next step is to explore the two most important functions used in every Arduino program.