To answer the question "What Language Does Arduino Use?", You have to look at the
system as a whole to understand "what language Arduino uses" because the
entire environment creates the "Arduino Language" which is built on top
of a C++ compiler.
Throughout this discussion you'll see mention of a specific C++
function that is provided by the Arduino environment. If you have ever
done any Arduino programming you will be familiar with it:
digitalWrite(pin, output_level);
This function encapsulates three ideas:
When you look at the function there is no mention of the chip PORT or
specific bit within a port that must be used to control a pin on any microcontroller. Instead you have
an abstraction i.e. the pin number. The pin number is simply a number
printed next to an output pin on the physical Arduino board.
This function allows libraries to be abstracted since they too do not
need to know anything about the underlying hardware when they use this
function.
Also included in the underlying code is the ability to use different boards. each Arduino board will use different physical pins and ports but the code within the Arduino environment will set up the digitalWrite function to operate correctly for each Arduino board type.
It means that digitalWrite will work across different:
So in fact digitalWrite becomes part of the Arduino programming language. This is
true of other functions that are used in the Arduino environment e.g. analogRead etc.
The combination of libraries, board manager and supporting code
effectively add to the C++ language making it an enhanced language.
The key to the system is the IDE which brings these elements together.
The Arduino IDE (integrated Development Environment) is the key to using any Arduino Board, allowing you to program any supported board using this one interface with a version of C++.
The Arduino IDE is so popular is that many aspects of programming and
using Arduino boards are hidden behind this simple interface. It is
virtually a single push button interface that:
What do you have to do to get the operations above?
Answer: Click One Button (after writing the C++ source files of course).
The IDE also gives you:
The version of compiler used under the hood is gcc (the open source Gnu C Compiler) and specifically avr-gcc which is the Atmel Avr version. You can download the avr-gcc and use it from the command line but the Arduino IDE allows for one click compilation with virtually no user setup.
While you will see a lot of discussions saying that a simplified
compiler is used, in fact, the compiler is a fully capable C++ compiler.
When people say it is simplified they actually mean it is enhanced for
ease of use.
So when you ask the question: What Language Does Arduino Use? the
actual answer is C++. Of course since C++ is an addition to standard C,
it also allows you to use old style C syntax (or a mixture of both).
This can be very useful as old style C uses less memory than C++ which can be important in memory constrained Arduinos.
Note: it is not simplifed C++ - you can use any C++ syntax you want
since the avr-gcc compiler is extremely capable. You may however not be
able to use all of C++ as some operations consume huge amounts of
memory.
The ability to use classes that encapsulate private variables and
then put this code into a library makes using libraries for different
peripherals such as humidity detectors ADCs, and more, extremely simple.
The key differences (or so-called simplifications of C++) between the Arduino C++ and standard C++ are:
The Arduino IDE language is formed from a combination of IDE and C++ functions.
When you use the stand-alone avr-gcc you have to include all the
header files that contain prototypes of standard functions that you use
in your code. In normal code writing this is just a pain, and
feels like pleasing the compiler to get it all to compile - it does not
feel like useful work and just makes you feel important as you
have the knowledge "Oh that function is defined in math.h".
The Arduino system scans your code and automatically includes the header files so you don't have to think about them.
One key difference in Arduino code is that the Arduino system always expects you to define the functions:
setup();
loop();
The first function is executed once while the second is executed endlessly.
This makes writing code a little simpler since you don't need to write the 'forever' loop code and makes all Arduino programs follow a standard template.
Multiple different Arduino Boards are supported in the Arduino IDE
meaning you can program any Arduino Board using the IDE. This gives you a
familiar interface without needing to change to a different programming
environment.
The Arduino IDE also includes a library manager that can import code
from Github etc. These libraries support all the different peripheral
hardware modules and chips making it easy to develop a system since
there is likely to be a library already created for the hardware you
want to use.
An example of hardware abstraction is most easily seen when you control an output pin on an Arduino board.
Typically you use the following code
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
This sets pin 10 as output and drives it to high voltage.
When you look at the board you will see pin 10 labelled so you know
physically where the pin is located. You can continue this process for
as many pins as available on your Arduino board.
This makes controlling an external device extremely simple.
If you ask a traditional microcontroller coder, "what is pin 10?" you will get
an answer such as "It is bit two of PORTB". In traditional use, for an 8
bit processor (e.g. Arduino UNO, Nano, Mega) port pins are arranged in
blocks of 8.
However the coder will have to look at an Arduino pin layout that
maps physical pins to the "abstract pin" and ascertain the PORT and BIT
be used whereas an Arduino user "Just looks at the board"!
Accessing the pin using traditional non-abstracted coding you would write:
DDRB | = _BV(2); // Set bit 1 of DDRB to output. pinMode operation.
PORTB |= _BV(2); // digitalWite(10,HIGH) operation.
Note_BV is a macro that returns in this case (1<<2) - the value
1 (left value) shifted left by two bit positions (right value).
In any case bit 2 of PORTB is set to high .i.e. the same as:
digitalWrite(10,HIGH);
The only difference is that as the software programmer you don't need
to know anything about ports or bit shifting when you use
digitalWrite(). So you can see that hardware abstraction makes software
programming easier as you don't have to think about how the underlying
hardware operates.
Hardware abstraction makes code writing easy, but to get that magic, more processing must be done so the code operates slower. However, Arduinos are really fast and you won't notice - fast operation is rarely need unless you are doing something really critical.
You can see that the Arduino coding language used is C++ (which
includes all of old-style C - useful for memory saving) but additionally
the
complete Arduino environment adds functions and that allow hardware
abstraction.
These new functions form part of the "Arduino Language".
It means that the same code can operate for different microcontrollers, different Arduino boards and libraries.
It is the reason that Arduino is so popular - it's just easier.
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.