Arduino C Programming Structure: setup() and loop() Functions
Arduino C Programming Structure: setup() and loop() Functions
Understanding the Arduino Program Structure
Arduino’s C language is different from standard C because it comes with a ready-made framework. Every Arduino sketch (program) follows the same basic structure with 3 main sections: Header, setup(), and loop()
Section 1: Header
The Header sits at the top of your code, before any functions. It contains:
#includestatements for libraries- Global variable declarations
- Constants
#include <Servo.h> // Include the Servo library
int servoPin = 9; // Define servoPin as Digital Pin 9
Servo myservo; // Create an object to control the Servo
Adjustable parts: If you’re not using a Servo, you can remove this entire section
The Header section is optional. If your program doesn’t need any libraries or global variables, you can skip it entirely.
Section 2: The setup() Function
setup() is a mandatory function that must exist in every Arduino program—even if it contains no code at all
void setup() {
// Set pin 11 as OUTPUT mode
pinMode(11, OUTPUT);
// Set baud rate for Serial Monitor
Serial.begin(9600);
// Attach the Servo to the defined pin
myservo.attach(servoPin);
}
Main purposes of setup()
| Command | What it does |
|---|---|
pinMode() | Configures pin as INPUT or OUTPUT |
Serial.begin() | Sets serial communication speed |
myservo.attach() | Assigns control pin to Servo |
setup() runs only once when the program starts. Use it for initializations that should happen just one time.
Section 3: The loop() Function
loop() is the second mandatory function in every Arduino sketch. Unlike setup(), it repeats forever
void loop() {
myservo.write(180); // Move Servo to 180 degrees
delay(1000); // Wait 1 second
myservo.write(0); // Move Servo to 0 degrees
delay(1000); // Wait 1 second
}
What should go in loop()
- Commands to read sensor values
- Commands to control devices (fans, LEDs, motors)
- Display commands
- Condition checks and responses
loop() is essentially Arduino’s version of the main() function in standard C, but it automatically repeats in an infinite loop.
Complete Example with Full Structure
// === Header (optional) ===
#include <Servo.h>
int servoPin = 9;
Servo myservo;
// === setup() runs once ===
void setup() {
myservo.attach(servoPin);
Serial.begin(9600);
}
// === loop() repeats forever ===
void loop() {
myservo.write(180);
delay(1000);
myservo.write(0);
delay(1000);
}
[image: Complete Arduino code example with all 3 sections clearly labeled with comments explaining each part]
Summary
Arduino’s C programming structure has 3 main sections: Header, setup(), and loop(). setup() runs once at startup, while loop() repeats indefinitely. Once you understand this pattern, you’ll be able to read and understand any Arduino sketch you come across.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย