The Arduino Language in 10 Points

Programming on Arduino is definitely not the same thing as programming on a standard computer. So, what exactly is the Arduino language?

It seems like you’re programming in C/C++, but soon you realize that it’s not exactly C/C++. You don’t really know what you can do, and what you can’t do.

In this post I’ll show you all the secrets behind the Arduino language, so you’ll know exactly what to expect from it, and how you can use it more efficiently!

1. What is the Arduino language?

The Arduino language is a subset of C/C++, where you can also use assembly for ultra-low level code.

When saying “programming on Arduino”, in fact you don’t program the Arduino board itself, but the microcontroller inside the board. For example, the Arduino Uno has a AtMega328p microcontroller.

And usually, when writing code for microcontrollers, performance matters a lot. That’s why you need to have strong and fast programming languages. C and C++ are among the most powerful languages you can find. Those are quite fast and stable, and thus are a good choice for microcontrollers.

The compiler used to transform code into object files is avr-g++. Then, a program called avrdude is used to upload the program into the Arduino microcontroller.

2. C++ limitations

So, if the Arduino language is a subset of C++, then does that mean that you can use all the C++ functionalities?

Unfortunately, the answer is no – at least with the Arduino core. You can get most of the following functionalities by using an external library.

Here are some of the main C++ features you can’t use with Arduino:

  • STL library. Forget about using std::vector for lists, std::string, or std::map. You’ll have to define your arrays yourself and manage their space. Anyway, the Arduino memory is far more smaller than your computer memory, so managing memory is a real matter here. You’ll want to make your arrays as small as possible. For strings though, you can use the built-in Arduino String type.
  • Operators new and delete. Those are not implemented in avr-g++, so you can’t use them.
  • Exceptions. You’ll have to deal with error codes in returned values!

3. Can you use object oriented programming with the Arduino language?

Even if a lot of C++ features are not available, you can still use classes with the Arduino language! But remember, you won’t be able to create a class instance using the “new” keyword.

If you’re already an OOP adept, then you won’t have to forget everything you know.

// Just a random code example to show you can 
// use classes with the Arduino language

class MyMotor {
  private:
  // Private attributes
  int some_attribute;

  public:
  // Constructor
  MyMotor() {
    some_attribute = 4;
  }

  void init() {
    // init motor
  }

  void start() {
    // start rotating motor
  }

  void stop() {
    // stop rotating motor
  }
};

MyMotor motor;

void setup() {
  motor.init();
}

void loop() {
  motor.start();
  delay(500);
  motor.stop();
  delay(500);
}

4. The Arduino.h library

Before your code is sent to the compiler, the Arduino environment will automatically add the “Arduino.h” library at the top of your code, if you haven’t done it yourself.

#include <Arduino.h>

This library contains all the Arduino functions (digitalWrite, analogRead, etc), and sub-libraries (Serial, Wire, …). The entire code with all included libraries is open source and available on GitHub.

5. Setup and loop function

Usually, a C/C++ program starts its execution from the main() function. As you can see on an Arduino program, there is no such thing as a main() function.

In fact, you have 2 mandatory functions that you need to implement: setup() and loop().

The setup() function will be called only once as soon as the program starts its execution.

After that, the loop() function will be called indefinitely. As soon as the loop() is finished, it will be called again and again.

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

// No main() function here!

6. Variable initialization with the Arduino language

In C/C++, if you declare a variable without initializing it, then you don’t know what’s its first value. It could be anything.

With the Arduino language, as soon as you declare a variable, Arduino will initialize it to zero for you. Of course, this will depend on the type of variable you have.

For integers, the value will be 0. For float number, 0.0. And for pointers, you just get a NULL pointer.

int i;
float f;

void setup() {
  Serial.begin(9600);
  Serial.println(i); // --> will print 0
  Serial.println(f); // --> will print 0.00
}

void loop() {}

7. Automatic prototype generation

In C/C++, prototypes are generally created in a .h file that you include in your programs, while your code implementation will be written in .c or .cpp files.

For Arduino, well you can do the same. But in case you don’t declare a prototype for a function, Arduino will automatically do it for you before passing the code to the compiler.

For beginners, if you write a function A, and another function B after the function A, then there is no way the function A can reach function B. You will get a compilation error if you call function B inside function A. Unless you create a function prototype for both functions that you include at the beginning of your file (or in a separate .h file). That’s what you are normally supposed to do when writing C/C++ code.

Arduino will correct this mistake for you. But be careful, the prototype generation is not 100% reliable depending on how you write your code, so… You’d better start learning about function prototypes anyway.

void function_A() {
  function_B(); // compilation error in C++, no error with Arduino!
}

void function_B() {
  
}

void setup() {
  function_A();
}

void loop() {}

8. Boolean, byte, and other specific Arduino types

When programming with the Arduino language, you can use data types such as “boolean” or “byte”. Those types are absolutely not included in the C++ standard.

“boolean” is a simple alias for “bool” in C++, and even the Arduino documentation recommends that you use bool instead of boolean.

A “byte” is an 8-bit unsigned number. You can store any number from 0 to 255. In C++ you would use the uint8_t type, which you can also use in Arduino.

The “String” data type is also Arduino-specific, with its own rules.

Well, what you need to remember from this point is to always check when you’re using a data type you’ve never used before. Is this data type only Arduino-compatible or is it also C++ compatible? If you want to write code that can be executed elsewhere in plain C++, you’d better choose appropriate data types from start. But if you’re just beginning, or just want to make a program that will only run on Arduino, feel free to use the built-in types.

9. Folder organization

With Arduino, you don’t create a program, you create a “sketch”. The main file containing the setup() and loop() function has an .ino extension. All other files can have .c, .cpp, .h extensions.

To open a “sketch” into the Arduino IDE, know that you’ll have to create a folder named exactly as the .ino file, and put all your files inside this folder. For example, if your main file is called ArduinoTest.ino, then it should be located into an “ArduinoTest” folder.

Libraries that you install should be placed on the “library” folder of your global Arduino folder (usually created in your home repository, after you’ve installed the Arduino IDE).

Those rules are not specific to the Arduino language itself, but it’s quite important to know them as they will impact the way you organize your files when writing code.

10. Use alternative IDEs to write code with the Arduino language

You might want to know: are there other IDEs than the Arduino IDE to develop programs using the Arduino language? Because, to be honest, the Arduino IDE is far from perfect. And if you have to spend all your days developing on the Arduino IDE, you’ll quickly become crazy.

What you need to understand, is that the Arduino environment is different from the Arduino compiler (avr-g++), which is different from the Arduino uploader (avrdude), which… is different from the Arduino IDE.

The IDE is mainly here to allow you to write code. Then it will use the environment and other software tools to compile and upload the code to your board.

So, good news here: you can use many other IDEs, providing that they have some support for Arduino, of course.

For example, there is an available Arduino plugin for Visual Studio Code, which allows you to benefits from the VS Code strengths, and verify/compile/upload your code.

It’s not only about the language

The difference between developing an Arduino program or a desktop program does not end with the Arduino language.

When programming on a microcontroller, you are more close to the hardware, and the way to think is completely different.

That’s why you won’t often see a job description involving web development AND video game development AND microcontroller programming experience, because those fields are completely different.

With Arduino, you have limited resources that you need to take into account. Less RAM, lower computing frequency, no multi-threading, etc.

You also have access to many hardware stuff, for example PWMs and hardware timers. Mastering those concepts will demand much more than knowing how to program. You’ll have to read some datasheets and increase your knowledge in several areas.

Understanding what is – and what is not – the Arduino language is a first great step on your learning journey. Now you’re ready to avoid most common beginners’ mistakes, and to dive into more advanced concepts!

2 thoughts on “The Arduino Language in 10 Points”

    • Hi Rex,

      OOP brings more re-usability for the components you create in your code. Performance wise, the code is not better than just using plain functions.
      Using OOP in Arduino is also more of a personal preference. It’s just about using one coding paradigm instead of another. Choose what best suits you!

      Check out Arduino OOP for more details!

Leave a Comment