Learn to code


Previous Post
Components needed for the tutorials
Next Post
Blink a LED with Arduino

Understanding the Arduino Sketch / Syntax

in Arduino Basics

Programs for Arduino are usually referred to as sketches; the first users were artists and designers and sketch highlights the quick and easy way to have an idea realized. The terms sketch and program are interchangeable. Sketches contain code—the instructions the board will carry out. Code that needs to run only once (such as to set up the board for your application) must be placed in the setup function. Code to be run continuously, after the initial setup has finished, goes into the loop function. To be able to communicate between the Arduino Board and various external components such as sensors, modules or your computer we need tot to tell the board what it is supposed to do. This tutorial introduces the core elements of Arduino programming that are essential to know before making a sketch. We will use a built-in example to clarify these elements.

The syntaxes that are used by Arduino are the punctuation and grammar of computing language. When you are starting with programming you will get a lot of errors. However, do not worry. It will become your second nature over time. Here are some of the common functions that will be used in the following tutorials.

SerialprintIn(value) : Prints the value to the Arduino Serial monitor.

pinMode(pin, mode) : configures a digital pin to read (input) or to write (output).

digitalWrite(pin, value): writes the digital value (HIGH or LOW) to the pin set for output.

Delay(time): adds a delay to the sketch in milliseconds

Structuring an Arduino sketch with Void Setup() and Void Loop()

The example below contains the bare minimum of code you need for your sketch to compile without errors.

The setup() method and the loop() method.

The setup() function is called when the program starts. We can use the setup() function to initialize variables, pin modes, etc. 

Besides the setup()  function, the program will also need to consist out of a loop() function. After creating the setup() function we write a loop() function.

This function does exactly what the name suggests. It creates a loop allowing the program to respond to conditions. In the loop() we write code that we will use to control the Arduino Board. 

The code below won’t actually do anything, but its structure is useful for copying and pasting to get you started on any sketch of your own. The code below consists of comments, curly brackets and parenthesis. We will discuss these elements in more detail below.

Example code


you can declare variables here such as the pins that are connected to the Arduino board. We will discuss this in the next tutorial.


void setup() 
   // this part of the sketch will run once or when the board resets
void loop()
   // this part of the sketch will run in a loop    


Let’s go in some further detail about one on the most elements of your Arduino program – the comments. Comments are a way to explain the code written in simple language.

A comment line starts with two slashes //. The comment can be placed after a function or random in the program. Commenting in your code can be helpful for yourself or others that read your code.

In the above code, there is a comment about variables. It is written differently than explained above. Well, imagine that you will have a very long comment that consists of many lines of text. You can than simplify the // with /* comment */ .

When you write /*  and then press enter in the program the Arduino IDE will automatically close your comment by adding the closing statement */.

Thus, comments help us to explain code while the compiler hides it when the program is uploaded to the board.

Curly Brackets and Parenthesis

The Arduino language has tons of built-in functions, and, just like keywords, they also change color automatically when you type them.

Functions are kind of like the verbs of a programming language.  They make things happen, and they do stuff for us. The real reason I brought up functions isn’t that I want to explain how they work.  We’ll be learning, in detail, about all different types of functions throughout the course.  What I really want to talk about is function syntax.

All functions are followed by an opening and closing parenthesis.  Inside these parentheses is where you give the function what it needs to perform its task.  If the function needs multiple pieces of information, those items are separated by commas.

Some functions don’t take any data, but they still must have a pair of parentheses.  Also, every open parenthesis must have a closing parenthesis.

Notice when you the cursor on an opening parenthesis that the closing parenthesis is highlighted.


The next thing I want to talk about is the semicolon.  A semicolon lets the compiler know that a specific statement of computer code is complete.  Let’s take this line of code:

I know right now this code means nothing to you, but what I want you to see is the semicolon at the end.  This semicolon lets the compiler know that your next line of code is independent of the previous one.

If you wanted, you could move the semicolon to the next line of code or even insert spaces.  It wouldn’t make a difference.  However, this is a horrible form.

You really should keep your semicolon on the same line of code that it ends.  I just wanted to point this out to demonstrate that the compiler will ignore the spaces before and after a semicolon.

If you forget to type a semicolon and then compile your sketch, you may get several different types of errors.  

You are now ready to start making your first sketch: Arduino Tutorial: 2.1 Blink a LED

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed