Arduino Programming: Sketch Structure

Every time you open a new sample sketch from the Arduino IDE, there are always 2 important functions in every Arduino sketch. They are setup() and loop() functions.

setup() function is used to initialized your codings, variables, pin modes and etc. It only executes once at the very beginning of your sketch. The second function is the loop() function, it works as an infinite loop after the setup() function been executed.

Below is the Basic – Blink example sketch from the Arduino IDE.

Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.

void setup() {
     // initialize the digital pin as an output.
     // Pin 13 has an LED connected on most Arduino boards:
     pinMode(13, OUTPUT);

void loop() {
     digitalWrite(13, HIGH); // set the LED on
     delay(1000);            // wait for a second
     digitalWrite(13, LOW);  // set the LED off
     delay(1000);            // wait for a second

The setup() function in the above sketch is used to initialize the Arduino digital pin 13 as output and it only executes once. The loop() function will set the digital pin 13 to high (5v), pause 1 second, then set to low (0v) and pause again another 1 second. Since the loop() function is an infinite loop and you will see the Arduino on-board LED keep on blinking infinitely in 1 second duration.



This entry was posted in Software and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>