Arduino - Code Structure

Basic Structure

Arduino code (also called Arduino sketch) includes two main parts: setup code and loop code.

Setup Code

  • Is code in setup() function.
  • Executed right after power-up or reset
  • Executed only one time.
  • Used to initialize variables, pin modes, start using libraries,

Loop Code

  • Is code in loop() function.
  • Executed right after setup code.
  • Executed repeatedly (infinitely).
  • Used to do the main task of application

Example

void setup() { // put your setup code here, to executed once: Serial.begin(9600); Serial.println("This is setup code"); } void loop() { // put your main code here, to run repeatedly: Serial.println("This is loop code"); delay(1000); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • See the output on Serial Monitor
  • COM6
    Send
    This is setup code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

As you can see, “This is the setup code” is printed once, but “This is loop code” is printed many times. It means the setup code is executed once, loop code is executed repeatedly. The setup code is executed first.

※ NOTE THAT:

setup() and loop() functions MUST be in Arduino code. If not, an error is generated.

Optional Parts

Apart from setup and loop code, an Arduino sketch can include some of the following parts:

  • Block comment: usually used to write some information about the author, the wiring instruction, the license ... Arduino will ignore this part.
  • Libraries inclusion: is used to include libraries into the sketch.
  • Constant definition: used to define constant
  • Global variables declaration
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-code-structure */ #include <Servo.h> #include <LiquidCrystal.h> #define MAX_COUNT 180 Servo servo; LiquidCrystal lcd(3, 4, 5, 6, 7, 8); int loop_count = 0; void setup() { Serial.begin(9600); lcd.begin(16, 2); servo.attach(9); Serial.println("This is setup code"); } void loop() { loop_count++; Serial.print("This is loop code, count: "); Serial.println(loop_count); lcd.print("Hello World!"); servo.write(val); if(loop_count >= MAX_COUNT) loop_count = 0; delay(1000); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • See the output on Serial Monitor
  • COM6
    Send
    This is setup code This is loop code, count: 1 This is loop code, count: 2 This is loop code, count: 3 This is loop code, count: 4 This is loop code, count: 5 This is loop code, count: 6 This is loop code, count: 7
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

We do NOT need to understand code line by line now. We just need to know about code structure. The line-by-line code will be explained in the next tutorials.

Comments

If you have any questions or suggestions, please comment on our YouTube channel