Arduino programming language: What language is it based on and what are its fundamentals?

Last update: 19/08/2022
Arduino programming language: What language is it based on and what are its fundamentals?

Arduino it has become in one of electronics projects most successful in the worldIt was created primarily for students in the year 2005 in Italy, All of this was aimed at making electronics work easier, allowing anyone interested in the field to experience what it's like. creation of electronic projects for use in the real world.

Therefore, to use this platform it is not necessary to have advanced knowledge in electronics nor being a successful programmer, since the idea Arduino main It is to make all of this as accessible as possible, thus allowing thousands of people to create your own projects from home.

One of the keys to success here is knowing write the instructions for microcontroller in the programming languagewhich may not be so simple for many. That's why we're going to teach you a little more about what it is. The Arduino programming language and its fundamentalsTo do this, carefully follow each of the steps that we will show you below in the post.

What is the Arduino programming language and what is it used for in electronics?

What is the Arduino programming language and what is it used for in electronics?

In this case, it should be mentioned that the Arduino programming language is based on C++ even though the reference for this programming language of Arduino is in “Arduino”, nowadays you can also Use standard C++ commands in board programming.

This is how we're going to show you the most outstanding features of C:

  • It is considered the programming language general purpose that is associated to the UNIX operating system.
  • It's a mid-level languageIt has basic objectives such as numbers, characters, bits, memory addresses, among others.
  • Account with a great portability.
  • It is used for systems programming as it is text editors, interpreter builders, compilers, among others.

C++ he is considered like un programming language which was designed in the mid-80s by Bjarne Stroustrup, It was created with the intention of extending this C language with mechanisms that allow for the manipulation of objects. Accordingly, and from the perspective of object-oriented languages, C++ It is considered a hybrid language.

As time went on, generic programming features were added, all of which complemented the other two paradigms that were already supported. (Object-oriented programming and structured programming). That is why it is understood that C++ is a programming language multiparadigm programming and there is currently a standard known as ISO C++.

C# is a Microsoft's proprietary language which is responsible for mixing the basic features of C++ simplifying them Java-style and at the same time offering a framework. Finally, it must be mentioned that C# is part of the .NET platform

What other programming languages ​​does Arduino board programming use?

The team in charge of Arduino created a language suitable for programming this card, said language is found based on WiringThis is all about a frameworks, This is a kind of template, structure, or conceptual scheme based on technology that allows you to work in a much simpler way, and it is also... open source for microcontroller programming.

Let's look at it in more detail:

Wiring

It is written in the C and C++ programming languages, That's where the syntax of both becomes quite similar. The main advantage of using this framework is the great variety of microcontrollers it supports, since it is capable of support microcontrollers that are used by the Arduino board families.

Finally, just like the arduino board, Wiring has a IDE which is based on Processing which allows for the programming of microcontrollers. Therefore, you simply need to go to the official website of Wiring and download this development environment, which is available for Linux, Windows, and Mac operating systems.

Processing

In order to give it to him instructions and write the code on the microcontroller or development board An environment is necessary. These environments are known as “Integrated Development Environment” or as it is also known by its abbreviation IDE and which is defined as an integrated development environment.

In the case of Arduino IDEThe program, where the code is written, is developed using a programming language known as Processing. Therefore, Processing is a Java-based programming language and development environment, It is mostly used to carry out the development of digital design multimedia projects.

C/C++ language

As mentioned earlier, the language of Arduino based Wiring and which is also based on C/C++. This allows working on Arduino projects, but it requires some knowledge about them. the C language. It should be noted that not all of the functions of this the language They are used, but only if the types of structure and variables are used.

However, the advantage that C offers It is a mid-level language, which allows for the efficient management of all resources. microcontroller and it is also a compiled language that allows you to create a file so that it can later be loaded into the microcontroller.

Arduino IDE: Discover the development environment for Open Source hardware!

El Arduino IDE or as it is also known “Integrated development environment” It is a computer program that is composed of a set of programming toolsThis can be dedicated exclusively to a single programming language or it can be used by various types of languages.

Thus, the IDE It has become the programming environment that has been packaged as an application program, which means that it is a code editor, a compiler, a debugger, and a constructor graphical user interface (GUI). In the case of the plate Arduino The tools to load the compiled program into the hardware's flash memory are incorporated.

Interrupts in Arduino: What are they, what are they used for, and how can we create one in our project?

Thus, it can be said that the Arduino IDE It's the tool used to work with Arduino, so it's necessary to understand how it works. In the case of Arduino programs These are composed of a single file with the extension “Info”, Although it's also possible to organize them into different files. In the case of the main file, it must always be located in a folder with the same name as the file.

Finally, it should be mentioned that prior to version 1.x The extension was used “pde”, However, when this version was updated, there were significant changes, which is why it should be taken into account if used. the old code. It should also be mentioned that The IDE is completely free and it's possible Download from the official Arduino website and as for your installation process It's quite simple, so you won't have any problems.

Arduino Programming Fundamentals: What are the most important things you should know?

Arduino programming It has several fundamental principles that are important to know and apply, as they will help you achieve better results when working on this platform. That's why we're going to teach you below. What are the most important fundamentals you should know about Arduino programming?

To do this, carefully follow each of the following aspects:

Syntax

In the case of programming languages It is required to have a syntax, it is the same one that will allow communication with the compilerThe compiler's mission is It will read the code and translate it into machine code. so that he understands microcontroller. Therefore, it can be said that the compiler needs all the information it receives to be perfect, as this is the only way it can... be able to read and translate the code.

Comments

The comments are simply notes that the programmer leaves traces within the code, This helps you understand a large part of that. code. It's important that in this case, the goal isn't to create an extensive report about the code itself, but rather a descriptive overview that helps to understand how it works. It's worth mentioning that this is a fundamental part when... A program for Arduino is being written.

However, it is important to keep in mind that The comments are not compiled, it does not translate to machine codeTherefore, every time the compiler encounters a comment, it skips that line.

Currently, there are two ways to write a comment, which are as follows:

// This is a comment. Int myvariable = 0;

Everything that is located to the right of the double bar (//) is considered a comment and it turns gray.

In this case, the Arduino IDE will change the color to indicate that it is a comment:

/* This is a comment */ Int my variable = 0;

There is another way to place a comment and placing /* to open and */ to close. Therefore, everything between these two opening and closing marks is considered a comment. Since it is a The comment will be changed to a gray color.

Semicolon

En In the C++ programming language, the semicolon (;) is like a paragraph break.This means that this symbol indicates that what is being said is that a sequence has ended and from that moment on something new has been said that is unrelated to the previous one at the syntax level.

Let's see:

// This is a comment. Int variable = 0:

Beginning semicolon supplied in the previous line, the compiler He interprets everything written as a new sentencewhich will have nothing to do with the previous one. Therefore, at this point it is already possible to perform a test to see how the compiler behaves.

To do this, you must access a new program in the Arduino IDE and copy the following code; you do not need to connect the board to the computer:

//No semicolon int variable1 = 0 void setup() { // Insert your code here; } void loop() { // Put your main code here, so it runs repeatedly; }

Next, you must click on the "Verify" button; this is the first shortcut to the editor:

Sketch_may29a:4: error: expected ',' or ';' before 'void' Void setup() { Exit status 1 Expected ',' or ';' before 'void'

As you can see, it appears here the first mistake. To resolve this, it's important to analyze it. If you look at the message area, you can see that the error appears in English, but it's very descriptive. “Expected” “,” “Or” “, “before void”, This means that a comma or a period and as before the word void.

Let's see:

Sketch_may29a:4: error: expected ',' or ';' before 'void' Void setup() { Exit status 1 Expected ',' or ';' before 'void'

Finally, in the editor will see a red strip which would indicate the line where the error is located and where it is referenced in the console. The next step will be Change the code and place a semicolon at the end of line 2 so that it compiles. As you can see, the compiler is not only demanding, but also helps to correct errors.

Variables

Variables They are one of the most important concepts in what is Arduino language programming. These consist of a programming tool that It allows you to store and retrieve information. of the programs.

Type of data

It's not just about having a name the variablebut to be able to indicate the type of data to be storedTherefore, data types are responsible for indicating the information to be stored, specifying each one of data types.

Torrent files: What are they, what are they used for, and how can we download them?

This will make the data retrieval and acquisition process much simpler; it's not the same to search for something unknown as it is to have prior specifications of what you're looking for. It should also be mentioned that each variable will only be able to store a specified data type when that variable is declared.

Showtimes:

In the case of functions, some of them require parameters such as the digitalWrite functionwhere it is important to be able to know two things, such as el state (HIGH or LOW) and the PIN number.

You are surely wondering How do I know what parameters the function accepts? In this case, the best course of action is to consult the language reference; this is like a guide or user manual. Virtually all languages ​​have their own reference. Upon accessing “DigitalWrite” You will notice that the site is in English.

This being its biggest drawback, but you will be able to see the following:

Showtimes:

The references section contains everything you need to know about how to use the function:

  • Syntax: Indicate how the function is written and how many parameters it accepts.
  • Parameters: Here, each parameter is explained and what it means.
  • Returns: If a value is returned, the function will tell you the data type. The digitalWrite function It does not return any value, therefore nothing is placed.

Arduino setup() and loop() functions

Finally, we find what is Arduino's setup and loop functionsBoth are considered the most important functions of a program and essential when... Learn Arduino.

setup() function

It's an Arduino program, where there are things that only need to be done once, such as the following:

  • Start la serial communication with the computer via the USB port.
  • Show un Welcome message on an LCD screen.
  • Configure the welcome pins on an LCD screen.
  • Start un counter to zero.
  • Rotate a servomotor to a linear position default.

You should keep in mind that these are types of tasks that are performed only once. Therefore, they can be described as... the initial conditions of the program, These types of tasks are the ones that go in setup() function. To better understand all of this, we will look at this function in more detail here.

In this case the setup() function It has a special color in the Arduino IDEjust as happens with others fundamentals of language. This color indicates that the Arduino IDE can identify the function as a special type that is used for the control of the code structure. Like any function, it has an opening parenthesis and a closing parenthesis.

But unlike most functions, in this case setup() does not need any arguments to operate:

Void setup() { }

As you can see on the left side of the word setup is the word void, which indicates that this The function will not return any value or data. Therefore, said This function requires no arguments and returns no value. But it does do something.

In this case, its job is to execute all the code within it; this is delimited with curly braces as follows:

void setup() { //Modes for pins 5 and 8 pinMode(5, INPUT); pinMode(8, INPUT); //Welcome message on the LCD LCD.write("Welcome to Arduino Pizzeria"); //Default position of the servo motor Servo.move(90) ; }

Thus, the setup function It's the first thing that runs when the Arduino programOnce it is executed the setup functionThe program will execute each line one by one. The most important thing about setup is that it only runs once, exactly as described above. Therefore, this is very important. The code between those two curly braces will only be executed once..

loop() function

As setup is a Arduino programbut this time it runs after the setup function.

The meaning of loop is repetition, and it is similar to that of setup:

Void loop () } {

It does not require any kind of argument even though it has opening and closing parentheses. In this case it does not return any value, so it has the word void to its leftThis is how we will now show you how the key opening and closing works. ({}).

The same thing happens here as in the setup function; when the loop function is executed, it runs line by line, as can be seen in the following code:

void loop() { Read pin 5 Int pinCinco = digitalRead(5); //Pin 8 in high state Digitalwrite(8, HIGT); //Servomotor to position 180° Servo.mode(180); }

It can be said that the big difference that exists between the setup and loop function It's that when you are executing the code of the loop functionThe program returns to the beginning and repeats everything. It enters an endless loop, repeating the entire process. Now it's important to consider the speed at which the function executes, as this is primarily determined by each... microcontroller.

In the case of la Arduino UNO boardthe microcontroller used is the ATmega328P and has a speed of 20MHz (megahertz). All of this is equivalent to a a total of 20.000.000 instructions per second, But this does not mean that it is 20 million lines of codeIt is important to remember that the code you write is translated into machine code.

Each line of code that you write can translate to multiple translationsBut even so, if you have at least 1.000 instructions would take 0,00005 seconds. Therefore, the speed is incredible. In the case of the Arduino program, every time it reaches the last line of code... the loop functionThis will return to the beginning to run everything again, so it does not wait for any external input to do so.

Ebooks of IPAP
Ebooks IPAP

🔥JOIN🔥 THE NEW IP@P COMMUNITY! SIGN UP HERE!

Themes

Author: Félix Albornoz

I have over 20 years of experience working in the technology sector, helping companies and users develop and train in this field. I'm always learning new things.

Related