Arduino Sketch: Structure and Flow

Introduction

Arduino sketch is the name that Arduino uses for a program. It’s the unit of code that is uploaded to and runs on an Arduino board. A basic Arduino sketch consists of two functions:

  • setup()
  • loop()

The purpose of these functions will be explained later in the topic.

For now, open the Arduino IDE and click on the File tab. Then, click on New (or press Control + N on your keyboard) to have a look at the two functions.

Writing a Sketch

In this example, we will write a sketch, i.e. create a  program in Arduino IDE that will display the text Hello World on the screen. We will use the serial monitor window to write our message.

evive Notes Icon
Serial communication is very useful for controlling electronic components or devices that are connected to (interfaced with) the Arduino board from the computer. You can use it for debugging (finding errors in) Arduino programs when writing new programs. 

To write in the serial monitor, you must first initialize serial monitor, you can do it with the help of the following statement:

Serial.begin(9600);

Here 9600 is the baud rate at which you are communicating over USB with your device.

The statement to write on Serial Monitor is:

Serial.println(“Your Message”);

Now, modify the code by initializing serial monitor and writing print statement inside setup() function. You will end with this code:

Save your sketch by going to the File tab and then clicking on Save.

How to Run the Sketch

Follow the steps below to run the sketch:

  1. Connect your evive to your computer using a USB cable.
  2. Click the Upload button to load the program to the Arduino.
  3. Now open the Arduino IDE Serial Monitor Window to see the sketch run and print the text message. The text that the program shows should be visible in the serial monitor window.

evive Notes Icon
If you get an error, please make sure that you have written the code correctly, connected Arduino Board/evive and selected the appropriate COM port.

Arduino Sketch Program Flow

In an Arduino sketch, program statements (individual lines of code) are executed, i.e. run from top to bottom. This top-to-bottom execution of statements can be altered only by flow control statements.

There are few things to notice in your sketch:

  1. void setup(): It is the function initialisation/declaration process of the function named setup(). As the function does not return any value, it is initialized with the keyword void, meaning empty.
  2. Serial.begin(9600);
    Serial.println(“Hello World”);
    These statements are present in the setup function’s body.
  3. { is the opening brace of the functions that tells that all statements starting from here are inside the function.
  4. } is the closing brace of the function.
  5. ; is used to terminate the statement.

In the Hello World sketch, statements in the setup() function will run first, from top to bottom. The statement Serial.begin(9600); is the first statement in the setup() function, so it is the first to run. This statement sets up the speed of the serial port to 9600 baud. The baud setting in the serial monitor window must match this value so that the evive and serial monitor window can communicate at the same speed.

The second statement to run in the setup() function is Serial.println(“Hello, world!”);, which sends the text Hello World out for display in the serial monitor window. In this statement, any text can be put between the opening and closing quotes (” “) and it will be displayed in the serial monitor window.

The setup() Function

Statements in the setup() function are executed only once when the sketch is run (which you must have noticed in the Hello World sketch).

The loop() Function

Statements in the loop() function will run continuously from top to bottom and then back to the top.

If the loop() function contains two statements, the first statement will be executed first, then the second statement, then the first statement again and so on. Hence, the statements in the main loop will be executed continuously until the Arduino is switched off or reset.

In our Hello World sketch, since there is no statements in the loop() function, program execution ends up in the loop and gets stuck there, doing nothing.

It is important to have the loop() function in the sketch, even if it is empty because without it the microcontroller on the Arduino board will try to execute whatever it finds next in memory after the statements in the setup() function have been executed. But the loop() function prevents it from doing so by keeping the program execution in the loop.

Below is an example sketch that demonstrates the main loop execution. Copy the code to your Arduino IDE, upload the code to your evive, and start Serial Monitor.

evive Notes Icon
The delay() function in the statement delay(1000); introduces a waiting period of 1 second.

This is what you’ll get in the serial monitor:

The text in the setup() function is displayed only once when the serial monitor window is first opened and then the Arduino is reset. After this, program execution enters the loop() function and repeatedly executes the statements in the loop from top to bottom and back to the top again in a never-ending loop.