The site for those who love tinkering with Arduino, Raspberry Pi & Co.

Understanding our first Arduino sketch

by

Let's understand the code of our first Arduino project

I hope everything went well with your first Arduino experiment, and that the led flashed without any particular problems.

But let's review the code of our project:

/ *
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
* /
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup () {                
  // initialize the digital pin as an output.
  pinMode (led, OUTPUT);
}

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

For those who already know some programming language, most likely they will have already guessed how our sketch works: Arduino programming is really very simple, but not everyone may have had programming experience and if you are one of them I will try to make you understand the code. as smooth as possible.

Comments

The first thing we can identify in our code are comments.

The comments are part of the program that is not useful to the machine and that are written by programmers to maybe write down something to remember in the future, we can notice them if we chew a little English in fact they do not resemble commands to be sent to a machine.

Technically we can recognize them because they are included between the symbols / * and * /, therefore everything that is included between these symbols is ignored by Arduino when it performs the sketch.

Despite their uselessness in terms of commands for the form, comments are very important when your code is reused several times over time and perhaps modified, allowing us to have a quick picture of what is happening in our code.

In addition to being useful for themselves, the comments are very useful when the code is created collaboratively between a group of people, or when the program is released under an open source license, whoever will use our program will immediately understand what we have done.

There is another form for comments in our sketch, but in this new case the comment is a single line: these comments start with a // (double slash) and continue to the end of the line.

For example in the line of code:

digitalWrite (led, HIGH); // turn the LED on (HIGH is the voltage level)

the message "turn the LED on (HIGH is the voltage level)" is a comment.

Removed all the comments the code becomes much leaner and probably less difficult for beginners:

int led = 13;

void setup () {                
  pinMode (led, OUTPUT);     
}

void loop () {
  digitalWrite (led, HIGH);
  delay (1000);
  digitalWrite (led, LOW);
  delay (1000);
}

Functions

After this first simplification we can recognize two main blocks in our program, these two blocks are the setup () and loop () functions and will always be present in every sketch we will create for Arduino.

A function (also known in jargon as a procedure or sub-routine) is a piece of code identified by a name that can be called anywhere in the sketch.

The setup () and loop () functions, however, as we have already said, are predefined and indispensable in every sketch and contain the basic commands for the operation of our projects.

The setup () function is called once when the sketch begins its execution. This function is ideal for performing initial configuration functions (setup means organization) such as setting the pin mode that we will use or initializing any libraries (topic that we will see later).

The loop () function, on the other hand, is called continuously and cyclically (loop means ring): once finished it will be called up and this until the board is turned off.

If we press the reset button, or momentarily remove the power supply, the setup function () will still be executed the first time on restart and then the loop () function will start continuously, guaranteeing correct configuration of the board at each start.

In an Arduino sketch you must always declare both functions even if, in extreme cases, you don't need them.

If we see the definition of the setup () function of the example:

void setup () {                
  // initialize the digital pin as an output.
  pinMode (led, OUTPUT);     
}

We can see that the first line defines the information of the function, such as its name: "setup". The text before and after its name defines the type of the value the function will return and the parameters that must be sent (these parameters will be explained later).
The code between {and} is called the body of the function which is what the function does.
You can call a function if it has already been defined, for example the line "pinMode (led, OUTPUT);" calls the pinMode function, passing two parameters: led and OUTPUT. These parameters are used by the pinMode function to decide which pin and configuration to set.

Variables

Variables are a place to store data. They have a name, a type and a value.

For example, before the setup and loop functions in the line:

int led = 13;

we have declared a variable named "led" which has an integer type (ie a number without a decimal part) and we assign it an initial value equal to 13, a value that we will then use in our sketch to indicate to Arduino which port we have connected the led to .

Each time the name "led" appears in the code, the value of the variable will be retrieved.
You could also choose not to create a variable containing the pin number but to indicate the value 13 in the program from time to time, using a variable however we have the advantage of being able to easily move the LED to another pin: just change the value 13 with our desired pin when we assign the pin number to the variable.

pinMode (), digitalWrite (), delay ()

pinMode (led, OUTPUT);

The pinMode () function configures an arduino pin as an input or an output. To use it we must pass as a parameter the number of the pin we want to configure and the constant INPUT or OUTPUT.

When configured as an input the pin can detect the status of a sensor such as a button. As an output, the pin can drive an actuator such as a LED in our example.

This function is generally used in setup ().

The digitalWrite () function sets an output value on a pin. For example the line of code:

digitalWrite (led, HIGH);

sets the "led" pin (pin 13) to HIGH (5 volts). Setting it to LOW the pin is connected to ground (it assumes the value 0 volts). We thus obtain the on or off function of the led.

The delay () function makes the Arduino "wait" for a certain time set in milliseconds before executing the next line of code. There are 1000 milliseconds in a second so the line of code:

delay (1000);

creates a one second delay.

Let's put it all together

Now that we know the meaning of our code in detail, we can put it all back together:

  1. Let's create the variable with the pin number
  2. We configure the pin as an output
  3. We apply 5V to the output (lit)
  4. Let's wait a second
  5. We apply 0V to the output (off)
  6. Let's wait a second
  7. Let's go back to point 3

Easy right?


Arduino starter Kit
Arduino Starter Kit Ufficiale Per iniziare alla grande con Arduino

Ti è rimasta una domanda o un dubbio?

Iscriviti e chiedi nella community

Leave a Reply

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