The Arduino Programming Language: Which One Is It?

Wondering what programming language you should learn to program the Arduino? The short answer is C++, but if you want to know why it’s not obvious, and why there are quite a few different answers still hovering about…

Let’s Start at the End

At the heart of most Arduino boards lie microcontrollers made by Atmel, specifically from their AVR family. The only commands these chips actually understand are the AVR Instruction Set – a relatively small collection of Assembly commands. You can find them on the microcontroller datasheet.

ATmega328_InstructionSet
Part of the ATmega328 Instruction Set (taken from the datasheet)

If you’re really into it, you can learn this language and program the Arduino directly in it. However,  for most applications you’ll want to write your program in a “higher”, more advanced and comfortable language, such as C, C++, Python, Pascal, even Basic or Ada. That’s where Compilers step in: they are computer programs that take your higher-language code and convert it to AVR Instruction Set commands, so that the Arduino can understand and run them.

The Arduino Integrated Development Environment

The good folks who designed the Arduino software chose what is probably the most common solution in the industry today: the C++ programming language. Under the hood of the humble Arduino Integrated Development Environment (IDE) hides a compiler called WinAVR, which is in fact a version of the famous GCC compiler, used mainly for compiling programs in C and C++.

If you take a look at files deep in the Arduino installation directory on your computer – for example, on Windows, at …\hardware\arduino\avr\cores\arduino – you’ll find source code files with the postfix .cpp (i.e. C Plus Plus). Open them in a text editor and you’ll see they are indeed written in C++. Also, if you already know C++, you can write your Arduino code in it and see that it compiles just fine.

Part of the Arduino source code
Part of the Arduino source code. Yes, that’s C++

Given such overwhelming evidence, why do people still insist on other answers? Let’s find out.

Programming the Arduino in C

If – sorry, I mean AS 😉 – you follow this blog, you’ll see that even I will talk a lot about C programming in the context of the Arduino. While C is NOT a “subset of C++”, in most aspects it is very, very similar to one, and we’ll only rarely need the advanced features of C++, So for all practical purposes we can safely program the Arduino as if we were working with pure C.

A Language of Its Own

“Hang on now,” I hear you say, “if Arduino programs (‘Sketches’) are in C++, why do they have the .ino postfix? How come the obligatory main function is not in them? And, most important of all, why does the official Arduino website claim that ‘The Arduino language is based on C/C++’?” [emphasis mine]

First, microcontrollers are not PCs. for instance, they don’t have hard drives, and no file systems. So standard-library C++ functions to handle files will fail on them. Does that make the programming language different? No. It’s like saying that if I only know some Chinese, and not the whole Chinese language, what I do know is not really in Chinese. Of course it is, it’s just that I’m limited to a subset of the language.

On the other hand, the Arduino developers spent a lot of effort writing specialized code libraries to make it more beginner-friendly. Useful Arduino functions such as digitalWrite are definitely not part of standard C++, but again, they were written in C++, so it’s not a different language.

Another beginner-oriented modification is the use of the setup and loop functions instead of main. This is just a cosmetic change: behind the scenes, the compiler actually takes your code and incorporates it into a proper C++ skeleton code, with main and all. You can even write the main function yourself, although it will break some automatic functionality for that particular program.

Java, Processing and Wiring

While Arduino programs are written in C++, the Arduino IDE isn’t. It was written in Java, based in part on previous and related software called Processing and Wiring. These may be interesting on their own right, but they are not what you need to program the Arduino board.

The Arduino IDE
The Arduino IDE: For C++, not in C++

In Summary

If you wish to program the Arduino, you’ll need to know some C and/or C++.  There is no lack of books and websites to teach you these, but they are mostly focused on programming for PCs, which has a different character from programming for MCUs (Micro Controller Units) – so you will definitely want to check out Arduino-specific resources as well. Until I write more about it, here’s a good place to start.

4 thoughts on “The Arduino Programming Language: Which One Is It?”

  1. Thanks Igendel 🙂

    I’ve been learning C++ and always wondered why Arduino sketches have a different structure. This post helps heaps.

  2. Thanks for this post, just started getting into Arduino and was searching for just such a succinct explanation of the “Arduino Programming Language.”

    1. Well, the post could be much shorter, but I wanted to fit in all the misunderstandings and objections I’ve heard… Have fun with Arduino!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.