The 4 basic sections of an Arduino sketch

The 4 basic sections of an Arduino sketch
Published: by
Last updated:
Categories
Electronics

When using the Arduino IDE to program Arduino or Arduino-like microcontrollers, there are 4 basic sections that each sketch should have. They are the comment section, the variables section, the setup section and the loop section.

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.

For explanationational purposes, Arduino Blink.iso and an basic Wi-Fi connection sketch for the ESP8288 will be used:

/*
  Blink
  Turns an LED on for one second, then off for one second, repeatedly.
  Most Arduinos have an on-board LED you can control. On the UNO, NANO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6.
*/

#define LED 13   //  define connection of LED, in this case GPIO13

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Blink.ino is probably the most often used Arduino sketch today.

#include <ESP8266WiFi.h>

void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.begin("network-name", "pass-to-network");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {}

A basic version of the ESP8266 Wi-Fi connection sketch. This sketch uses third party libraries to be able to program ESP8266 and other microcontrollers with Wi-Fi functionality.

The comment section

After opening almost any sketch file, the first thing you’ll see is the comment section. This is also a good place where coders can start to plan and keep track of their sketches.

This section is basically a text area, enclosed in /* and */. The information in the comment section can contain any non-code lines and usually include things such as the description and/or function of the sketch, the version, details, how to configure the sketch and author attribution. Comment sections are not encoded into the uploaded version of sketches.

Looking at Blink.ino, it has a very comprehensive comment section. This makes it easy to identify the features and to track changes.

/*
  Blink
  Turns an LED on for one second, then off for one second, repeatedly.
  Most Arduinos have an on-board LED you can control. On the UNO, NANO, MEGA and ZERO it is attached to digital pin 13, on MKR1000 on pin 6.
*/

Because the ESP8266 Wi-Fi connection sketch was copied as example code, it does not contain a comment section, but this doesn’t mean you cannot add your own comments. For me, apart from an comprehensive date and description, important details include URL information from where the sketch/code was obtained from.

The variables section

The varibles section comes before the setup section (see later) and can either be used to declare variables (i.e. #define) and/or to import libraries (e.g. #include).

Blink.ino has LED to the value of 13:

#define LED 13 // define connection of LED, in this case GPIO13

This means that LED will have a value of 13 and until it is chaged later in the code. This makes it easy to use ‘LED’ each time 13 needs to be used.

Libraries are a collection of code that contain additional functions to be used in sketches. The Arduino IDE has all its related functions built into their own libraries, but additional (third party) libraries can also be installed. To be able to use an installed library, it needs to be imported into the sketch.

Blink.ino needs no additional libraries, but the ESP8266 Wi-Fo connection skecth makes use of the Wi-Fi library for ESP8266. After its installation, a library is imported using its name and #include.

#include <ESP8266WiFi.h>

The best place to add the variables section is the uppermost code section of the sketch, i.e. just before or just after the comment section. If needs be, and memory allows, multiple variables and libraries can be declared/imported per sketch – where they are simply listed sequentially, each in its own line.

The setup section

The setup section of a sketch comes after the variables section and/or after the comment section. Whereas the provious two sections were optional, the setup section (and the loop section) is mandatory, even if it doesn’t contain any code. This is where the sketch starts and is used to place all the startup tasks and define variables. As suppose to the loop section (see later), the setup section is only called once.

In the Arduino IDE, the setup section is defined using a special function called void setup(). Its content is encalsulated by { and }. Both Blink.ino and the ESP8266 Wi-Fi connection sketch have a setup section defined:

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}
void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.begin("network-name", "pass-to-network");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

The loop section

The last section of an Arduino sketch is the loop section. This is where all the actual task(s) of the microcontroller is devined and executed. As long as the microcontroller has sufficient power, the loop section will run over and over. As with the setup section, the loop section is also defined by a special function (void setup()) with { and } encapsulating the code.

As this section is also mandatory, both Blink.ino and the ESP8266 Wi-Fi connection sketch have a loop setup section defined – even though it is not always used.

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}
void loop() {}
About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause.
Donate via PayPal

Save, share & discuss

Your comment is important, but don't be a knob. Keep it constructive and polite.

Leave a Reply

Your email address will not be published.

More Arduino related posts

Fiverr – Freelance services marketplace for the lean entrepreneur
Fiverr – Freelance services marketplace for the lean entrepreneur
16 January 2021
Ad: Fiverr is the world’s largest freelance services marketplace for entrepreneurs to focus on growth & create a successful business at affordable costs. More…
The FTDI USB/UART module (FTDI programmer)
The FTDI USB to TTL serial converter module
5 June 2018
The FTDI USB to TTL serial converter module is an UART (universal asynchronous receiver-transmitter) board used for TTL serial communication. It is a breakout board for the FTDI FT232R chip with an USB interface, can use 3.3 or 5 V DC and has Tx/Rx and other breakout points. More…
Categories
Electronics
Introducing the Arduino Nano Microcontroller Board
Introducing the Arduino Nano microcontroller development board
3 January 2016 | Updated 21 January 2018
The Arduino Nano is a small, popular, microcontroller development board. It is a breakout board for ATmega microcontrollers. The boards are readily available, cheap, breadboard-friendly and easy to work with. More…
Categories
Electronics