2 – Our first program



    • Set some basic ideas about programming
    • Understand the structure of an Arduino program (also known as Sketch).
    • Define the block structures.
    • Introduce the first instructions


Bill of materials

Arduino Uno

Arduino Uno or equivalent.

This chapter accepts any other Arduino board.

USB cable A to B

A proper USB cable to connect your Arduino. Normally a USB A male  to USB B male.
PC laptop

A PC computer with the Arduino IDE properly installed and set up




A computer program is basically equivalent to a cook recipe… but aimed to a different audience. While we, people, are reasonably good at interpreting instructions of a cook recipe, generally in a vague way, when we are programming it is a computer which is supposed to understand our accurate instructions regarding what it must do, lacking, of course, in human imagination or in improvisational capabilities.

That is the reason why computer languages were developed, to give instructions to a machine.
To achieve it, the instructions must be:

  • Accurate: without any ambiguities inherent in human communication
  • Univocal: there is only a way in which they can be interpreted.
  • Concise: the shorter, the better.

The language we use to program in the Arduino IDE is a subset of C++, which is very widespread due to its features. C++ is not an easy language though because sets strict rules on how to write these instructions.

A program is a set of instructions that are executed one after each other, unless we specifically indicate accurate conditions in order to alter the sequence.

When we write instructions in the Arduino IDE, it checks that the syntax of our program conforms to the C++ norm, and if it is not very pleased with something will show an error, ending the checking and making us verify what we have written.

On the other hand, if it accepts our program then calls other program that translate what we have typed into comprehensible instructions for our Arduino processor. This new program is called the compiler.Compiler's purpose

The compiler transforms our instructions (source code) into processor instructions (object code).



An Arduino program or sketch consists of two sections or basic functions:

  • setup(): the instructions included in this section are executed only once, when we boot up the Arduino or when we press the reset button. It generally includes definitions and initializations, hence the name.
  • loop(): the instructions included in this section are executed in sequence until the end is reached… and when it finishes, it starts again from the beginning in a never ending loop.

When we open the Arduino IDE (select File\New) this two functions are already written in copper colour.

Watch out for the braces. The beginning of each function is marked by the opening brace “{” and the end with a closing brace “}“.

In fact, the set of instructions contained between the opening and closing braces is called a block. Blocks are a matter of vital importance when our Arduino has to interpret in some way or another the given instructions.

It is compulsory that to each opening brace corresponds another closing brace. In the following chapter we will go into more detail about the matter.
So far we are going to lay stress on the lines that appear inside the main blocks:

Everything written after a double slash , “//“, is a comment and will be ignored by the compiler. That is, we can leave information inside the code about the purpose of the instructions (that would raise errors otherwise). The compiler will ignore anything written between the double slash and the end of the line.




It seems to be compulsory that the first program we are supposed to do is the blinking LED, which is a good example that enlightens on some interesting ideas according to its possibilities:

  • The capability of the Arduino board to interact with the outside world. It is something quite unusual to those who are accustomed to traditional computing, where computer power has amazingly grown but keeps on being impossible (or nearly impossible) to actuate something in the outside world.
  • The simplicity of the development environment. In comparison to a traditional system in which an editor, a compiler and a linker must be used.

The Arduino board can interact in different ways with the world that surrounds it. We will start explaining the digital pins that can be used as:

  • Inputs: to read digital information from the outside world.
  • Outputs: to send signals to the outside world.


The Arduino Uno board provides 14 pins that can be used this way, numbered from 0 to 13:

Arduino UNO digital pins

Digital pins from 0 to 13

In the previous chapter we uploaded a sample program to the Arduino board that made a LED blink at a clearly-defined time interval. Let’s see how to program it.
We will set digital pin 13 as an output and then we will switch it on and off, making the on-board status LED to flash on and off at the desired rate.

To tell the Arduino board to set pin 13 as a digital output we use the following instruction:

     void setup()
               // initialize the digital pin as an output
               pinMode( 13, OUTPUT) ;
  • It is important to pay attention to the fact that, in spite of using a single instruction, we have defined the block inside this function by using the opening and closing braces.
  •  Watch out for the semicolon (“;“) at the end of the instruction. All C++ instructions must end with a semicolon, otherwise an error will be raised. The semicolon indicates that the end of the instruction has been reached.

We will use the following instruction to turn on the LED:

digitalWrite( 13 ,  HIGH) ;

And this similar instruction turns it off:

digitalWrite( 13 ,  LOW) ;

The first parameter indicates which pin to use, in this case we have chosen the pin 13, and the second parameter indicates the state. It can be either HIGH or LOW, depending on what value we want to use as an output. In Arduino +5V corresponds to a HIGH value and 0V to a LOW value.

  • If we write these two instructions one after each other inside the loop() function, Arduino would change these values so fast that we were unable to perceive it, so we need to slow it down a bit in order to do it .

To set a delay of, for instance, one second, we will use the following instruction:

delay(1000) ;                       //  delay(n) “freezes” Arduino n milliseconds

So, the steps we have to follow to make the LED turn on and off (as in a cook recipe) are:

  1. Inform Arduino of we are going to use pin13 to write values (inside the setup() function).
  2. Turn on the LED : set the pin high ( +5V).
  3. Wait a second.
  4. Turn off the LED : set the pin low ( 0V).
  5. Wait a second again.
    • If we had omitted this second delay, it would have turned off the LED and, suddenly, would have turned it on again. We wouldn’t see that the LED had turned off. (I won’t expect you to believe me, just check it out).
    • Arduino UNO’s processor is very slow, from an electronic point of view, but it is able to switch (to turn on and off) 15.000 times a second..

The first concept you have to bear in mind is that computers process instructions in a sequence, one after each other, and in the order they are given. Our program teach the computer to execute these instructions and establish the order in which they are executed.

The following program in Arduino C++ makes what we have previously discussed.

Sketch 2.1
  void setup()
         pinMode( 13 , OUTPUT); // We will use the pin 13 as output
void loop()
        digitalWrite(13 , HIGH); // Turn the LED on 
        delay(1000); // Wait a second
        digitalWrite(13 , LOW); //  Turn the LED off
        delay(1000); // Wait a second
  • Watch out for the indentation of the lines that highlight the code blocks. It is considered a good programming practice and we beg you to do it, because this makes easier the comprehension of the program.
  • Whenever you make a mistake (and believe us you will do it) the indenting is very helpful to visualize the program.
  • There are only two types of programmers, those who make mistakes and those who are about to make them.

There is only one thing left, to check whether there are some errors. Just click on the yellow icon that appears in the following picture:

IDE Verify Button

Verify button

If everything goes fine (if there aren’t any errors in red) we can compile and upload the program using the horizontal arrow.
On the other hand, ( and believe me, it will happen very often) you will have to verify and correct the possible errors. We will go back to this in the future.

IDE upload button

Upload button

The yellow arrow will upload our program to the Arduino board so that we can verify that the LED blinks with a one second delay between the on and off states.

  • Suggestion: if we modify the values in the delay, we will also modify the blinking rate.
  • Note: this example won’t work with any other pin of the Arduino Uno because only the pin13 has one LED attached.



In this chapter we have learnt several important things:

    • What the key concept of a program is, that is, a sequence of instructions that are executed in a fixed order.
    • There are two main functions in an Arduino program: setup() and loop().
    • To define a block of instructions we use the opening and closing braces, {}.
    • All instructions must end with a semicolon, ; (although there can be some exceptions…)
    • We can use comments in our programs using the double slash, //.
    • We have learnt some basic instructions in Arduino C++.



No Comments

Give a Reply