# How to Structure an Arduino Program?

- Link: https://studyelectrical.com/2021/09/how-to-structure-an-arduino-program.html
- Published: 2021-09-16T06:07:52+00:00
- Author: Tesla

If you are new to programming and want to understand how Arduino programs work, 
read this. This article explains how to structure an Arduino program.

To emphasize the agile nature of Arduino development, Arduino programs are commonly
referred to as sketches. A sketch and a program can be used interchangeably.

These sketches contain the code – the instructions the board will follow. All sketches
are divided into two parts – setup function and loop function. You should put code
that will run only once (for example, code to set up a board for the application)
in the setup function. The loop function contains the code that will be run continuously
after the initial setup is complete.

![Arduino Program Structure](https://studyelectrical.com/wp-content/uploads/2021/
09/Arduino-Program-Structure.jpeg)

Arduino Program Structure

Here is a typical sketch:

    ```wp-block-code
    const int ledPin = 13; // LED connected to digital pin 13

    // The setup() method runs once, when the sketch starts
    void setup()
    {
    pinMode(ledPin, OUTPUT); // initialize the digital pin as an output
    }

    // the loop() method runs over and over again,
    void loop()
    {
    digitalWrite(ledPin, HIGH); // turn the LED on
    delay(1000); // wait a second
    digitalWrite(ledPin, LOW); // turn the LED off
    delay(1000); // wait a second
    }
    ```

Once the board has uploaded the code, or once it has been turned on with that code,
it executes the instructions sequentially from the top of the sketch. The setup 
function runs the code once. Then, it runs the code in a loop. The loop continues
until it reaches the end (denoted by the closing bracket, }), and then it returns
to the beginning of the loop.

![setup and loop function of arduino](https://studyelectrical.com/wp-content/uploads/
2021/09/setup-and-loop-function-of-arduino-ink.jpeg)

Setup and Loop function of Arduino

## Example of Structured Arduino Program

In this example, a HIGH and LOW output is written to a pin continuously to flash
the LED. See [An Introduction to the Arduino for Beginners](https://studyelectrical.com/2016/04/an-introduction-to-arduino-for-beginners.html)
to learn more about using Arduino pins.

    ```wp-block-code
    const int ledPin = 13; // LED connected to digital pin 13

    // The setup() method runs once, when the sketch starts
    void setup()
    {
    pinMode(ledPin, OUTPUT); // initialize the digital pin as an output
    }

    // the loop() method runs over and over again,
    void loop()
    {
    digitalWrite(ledPin, HIGH); // turn the LED on
    delay(1000); // wait a second
    digitalWrite(ledPin, LOW); // turn the LED off
    delay(1000); // wait a second
    }
    ```

Upon beginning the sketch, the code in setup sets the pin mode (which allows it 
to light an LED). Once the code in setup is run, the code in loop (which flashes
the LED) is continually used throughout the duration of the Arduino board is powered
on.

**Where is main()?**

While you do not need to know this to create Arduino sketches, experienced C/C++
programmers may wonder where the expected main() entry point function is. Although
it’s still there, the Arduino build environment hides it under its covers.

In addition to the sketch code, the build produces an intermediate file that includes
the following statements:

    ```wp-block-code
    int main(void)
    {
      init();

      setup();

      for (;;)
         loop();

      return 0;
    }
    ```

Init() is the first function to be called, which initializes the Arduino hardware.
This is followed by a call to the sketch’s setup() function. After that, the loop()
function is repeated over and over. Since the for loop never ends, the return statement
is never executed.

### Related posts:

 [ ⌊Silver oxide minature cell battery⌉Miniature cells and Batteries – Silver Oxide, Mercury and Lithium Cells](https://studyelectrical.com/2019/11/miniature-cells-batteries-silver-oxide-mercury-lithium-type.html)
[ ⌊Arduino Weather station⌉Simple Weather Station using Arduino [Explained]](https://studyelectrical.com/2022/09/weather-station-using-arduino.html)
[ ⌊Electromagnetic Flow Meters⌉Things You Should Know About Electromagnetic Flow Meters](https://studyelectrical.com/2020/01/electromagnetic-flow-meters-things-you-should-know.html)
[ ⌊newbie electronics kits⌉Beginner Friendly Kits That Most Newbie Used In Electronics](https://studyelectrical.com/2020/03/beginner-friendly-kits-electronics-newbie.html)
[ ⌊moldflow-analysis⌉Understanding Mold Flow Analysis](https://studyelectrical.com/2023/10/understanding-mold-flow-analysis.html)
[ ⌊electrical machine software electronic⌉Top Educational Resources and Software for Understanding Electrical Machines](https://studyelectrical.com/2023/10/educational-resources-software-for-electrical-machines.html)
[ ⌊Namur Mounted Pilot Operated Asco Spool Solenoid Valves.png⌉Advancements in Industrial Automation with ASCO Solenoid Valves](https://studyelectrical.com/2023/12/industrial-automation-with-asco-solenoid-valves.html)
[ ⌊Selecting the Right Servo Drive 12 Critical Factors for Engineers⌉Selecting the Right Servo Drive: 12 Critical Factors for Engineers](https://studyelectrical.com/2025/11/selecting-the-right-servo-drive-12-critical-factors-for-engineers.html)
[ ⌊The Ultimate Guide to Computer Memory⌉The Ultimate Guide to Computer Memory](https://studyelectrical.com/2025/11/ultimate-guide-to-computer-memory.html)
