Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Understanding the Arduino Sketch / Syntax

in Arduino Basics

In case you never programmed before, there are some terms and concepts that are unfamiliar to you. Therefore it is wise to know your programming environment before you write your first line of code. Arduino programs are usually referred to as sketches; the first users were artists and designers, and sketch highlights the quick and easy way to realize it. 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 communicate between the Arduino Board and various external components such as sensors, modules,, or your computer, we need 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.

This part of the lesson outlines the elements of an Arduino sketch.

Arduino IDE

You will need a programming environment to be able to work with an Arduino Board. For Arduino, we will use the Arduino Integrated Development Environment (IDE). The program consist of a couple of components:

  • A text editor
  • A compiler
  • Controls to run and stop the program
  • A debugger

Arduino Text Editor: The text editor in Arduino IDE is the main thing you will see when programming sketches. It’s where you are writing your code. The Arduino text editor consists of features. A couple is noteworthy to clarify.

Line numbers: are displayed at the bottom of the editor, as seen in the image below. You can turn these line numbers on / off in the preference part of the IDE. These line numbers are essential if there is an issue in your sketch. You can quickly look the fault up by using line numbers.

Figure 1. Arduino editor line numbers

The toolbar: contains all the control buttons you need to control the compilation and upload your code. The first button compiles your code, while the second compiles and uploads it to the board that is selected. Search icon far the right will open the Serial Monitor.

Figure 2. The Arduino IDE toolbar

The compiler: is a program that takes the code/text that you write and translates it into the instructions that the Arduino Board needs to run the program that you have been written. You will not see this program in the Arduino IDE since the compiler does it work in the background. The toolbar is your interface to the compiler, and the message pane is where you will see status and error messages from it.

The message pane: at the bottom of the editor is where the IDE will send messages. These may be error messages or messages that you programmed in your sketch. The figure below shows two messages: one where the compiler successfully compiled and one where it did not.

Figure 3. The “Done compiling” message at the top of the message pane indicates that compiling was successful.

Figure 4. there is an error in our code. In the message pane, instructions are given to detect where the problem is. You can also copy the message to look it up on google.

Serial Monitor: the Arduino IDE includes a monitor program that displays data coming from your Arduino Board’s serial port and allows tos end data to the board. To be able to communicate with the Serial Monitor, you will need to establish a connection. This can be done using the command Serial.begin(9600). To send data from your board to the Serial Monitor, the command Serial.print() is used. You can read more about this at the Serial Communication section. Below is an example of a “printed” message to the Serial Monitor.

Figure 5. by using the Serial.print() command we can send messages tot he Serial Monitor.

Parts of the Arduino Sketch

just like written languages, programming languages have a syntax. If you understand these terms, it will be must easier to program. Arduino is based on a programming language called C.

We will discuss briefly what these parts are and then move on to an Arduino Sketch workflow.

Variables: are the constructs to store changing information. Every variable must be declared in a Sketch by giving it a name and a data type.

Int pinLed = 10;

The first part of the variable contains the datatype following by a name for the variable. For more on variables, visit the lesson on variables

Operators: is a symbol that tells the compiler to perform a specific mathematical or logical function. The coding language that is used by Arduino is rich with in-built operators. There are a couple of common operators:

Addition: +
Subtraction: –
Multiplication: *
Division: /

There are also special math operators that combine other operators. These can be used to simplify your code regarding math operations.

Increment value: ++
Decrement value: —

For more on operators, SEE THE TUTORIAL ON Operators.

Semi column: 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:

Int pinLed = 10;

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, that is a horrible form of coding.  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.

Brackets: An opening and closing parenthesis follow all functions.  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.

For example here is a part of code with brackets/parenthesis:

if (buttonCount == 4) {
  digitalWrite(ledPin, HIGH);
}

 

Comments: Let’s go in some further detail about one of your Arduino program’s most elements – 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 */.

Comments help us explain code while the compiler hides it when it is uploaded to the board.

Structure of an Arduino Sketch

The program that you write in Arduino IDE is executed one line at a time until it reaches the end. However, in Arduino, there is a function called loop(). This will run forever and ever. When the end of the lines of code is reached, the loop() function ensures that it will repeat from the beginning until there is no power left in the power source. Below is some code that gives an idea of the basic elements of an Arduino sketch.

int pinLed = 10; // LED is attached to pin 10
void setup()
{
  pinMode(pinLed, OUTPUT);  // set pin 10 as an output pin
  digitalWrite(pinLed, LOW); // turn the LED off
  Serial.begin(9600); // use the Serial Monitor
}

void loop()
{

  digitalWrite(pinLed, HIGH); // turn the LED on
  Serial.println("LED is turned on");
  delay(1000); // wait for 1 second
  digitalWrite(pinLed, LOW); // turn the LED off
  Serial.println("LED is turned off");
  delay(1000); // wait for 1 second
}

 

The program that you write in Arduino IDE is executed one line at a time until it reaches the end. However, in Arduino, there is a function called loop(). This will run forever and ever. When the end of the lines of code is reached, the loop() function ensures that it will repeat from the beginning until there is no power left in the power source. Below is some code that gives an idea of the basic elements of an Arduino sketch.

In the setup part, code will only run once when the program starts.

In the loop() we use the build-in function digitalWrite()to set the pin HIGH or LOW. We are printing a message to the Serial Monitor by using the command serialPrintln(). You can also use Serial.print(), however, that will print messages on one line after the code is repeated. There is a delay() function between the commands that allows us to stop the loop for a given time in milliseconds.

When the LED is turned off we reached the end of the code in the loop() section. The sketch will restart at the first line of code that is in the loop() section forever.

There are many commands and function that you can use in your sketch. Most of them are covered in future lessons.

Next Post
Blink a LED with Arduino
You must be logged in to post a comment.
Menu