กลับหน้าหลัก
views
Arduino C Programming Structure: setup() and loop() Functions
Last updated on

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()

Diagram showing Arduino program structure flow: Header → setup() (runs once) → loop() (repeats infinitely)

Section 1: Header

The Header sits at the top of your code, before any functions. It contains:

  • #include statements 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()

CommandWhat 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
}
Timeline graph showing setup() running once at startup, then loop() repeating continuously

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

จ้างทำโปรเจคเลย

ประเมินราคาอัตโนมัติ + Reference Code

ขอให้ AI ประเมินราคาโปรเจคนี้

กรอกข้อมูลให้ครบ ระบบจะสร้างรหัสอ้างอิงและประเมินราคา/ระยะเวลาคร่าว ๆ จากรายละเอียดงาน แล้วให้กด Add LINE พร้อมพิมพ์รหัสนี้เพื่อคุยต่อ

คำถามให้ AI ประเมินแม่นขึ้น

หลังส่งฟอร์ม ระบบจะโชว์ Reference Code ให้ copy แล้วกด Add LINE เพื่อคุยต่อ ข้อมูลส่วนตัวจะไม่ถูกส่งเข้า GA4

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...