Arduino Programming Language: Debunking Myths and Understanding the Facts
Uncover the facts about Arduino's programming language. Explore how C++ powers Arduino, debunk common myths, and learn how it simplifies coding for beginners while offering advanced functionality.
There’s a lot of confusion surrounding the programming language used in Arduino or ESP32 projects, when using Arduino IDE. Terms like "Arduino language" are often thrown around, leading many to believe that Arduino operates on its own proprietary or unique programming language. This misunderstanding arises because Arduino abstracts and simplifies traditional coding, hiding much of the complexity typically associated with languages like C or C++.
As a result, myths have emerged — such as the idea that learning Arduino requires mastering an entirely new language, or that Arduino uses the C programming language. In this post, we’ll clarify these misconceptions and explore what "Arduino language" really means.
What Language Does Arduino Use? #
Fact: Arduino Utilizes C++ as Its Primary Programming Language
The programming foundation of Arduino is based on C++, not a proprietary language or pure C. But take note of "based on", as there are key nuances to how C++ is implemented in the Arduino environment that make it distinct from standard C++ programming.
How Arduino Differs from Standard C++ #
While the code is compiled as C++, the Arduino environment introduces a few differences to make the programming experience simpler and more beginner-friendly:
- Automatic Inclusion of Arduino Libraries:
The Arduino.h
file, which contains core Arduino functions, is automatically included in every sketch.
This library provides access to essential functions and constants, such as digitalWrite()
, pinMode()
, and delay()
, which simplify interaction with hardware components. This automatic inclusion removes the need to manually include these libraries in every program.
- Simplified Program Structure:
Instead of the main() function typically required in a standard C++ program, Arduino sketches rely on two predefined functions:
setup()
– Runs once at the start to initialize settings.loop()
– Continuously executes to run the program logic.
This structure abstracts the complexity of defining and managing a main()
function, making it easier for beginners to focus on writing functional code. Behind the scenes, however, the Arduino framework includes a hidden main()
function that orchestrates the execution of setup()
and loop()
.
- Limited Availability of Standard C++ Libraries
The Arduino environment does not include many standard C++ libraries due to the memory and hardware constraints of microcontrollers. For example, libraries like <iostream>
(used for input/output with std::cout
and std::cin
) and <thread>
(used for multithreading) are unavailable. Instead, Arduino provides hardware-specific alternatives like the Serial library for communication.
Despite these limitations, much of the C standard library remains accessible, including <math.h>
for mathematical functions (e.g., sin()
, cos()
, sqrt()
) and <stdlib.h>
for general-purpose utilities (e.g., rand()
, atoi()
, memory management).
C++ and C: Understanding the Relationship #
Many people think Arduino programming is based only on the C language. This confusion happens because Arduino code, especially simple programs written by beginners, looks a lot like C due to its straightforward and procedural style.
However, Arduino is actually built on C++, a more advanced language that includes everything from C and adds powerful features like object-oriented programming. Understanding how C and C++ are connected helps explain how Arduino Language is actually based on C++.
C++ is a superset of C - This means C++ includes all the features of C and adds more functionality, such as object-oriented programming.
C is a subset of C++. - Code written in C can generally be compiled by a C++ compiler unless it uses certain C-only features.
How This Relationship Impacts Programming on Arduino #
In Arduino, you can use both C and C++ code without any problems. This means you can write simple C-style code with things like functions and structs or use more advanced C++ features like classes and objects. Arduino works well for all kinds of programmers — beginners can stick to easy, C-style code, while more experienced users can take advantage of C++ for creating more powerful and organized programs.
Since Arduino is built on C++, it supports both types of code. For example:
- You can write simple C-style programs with structs and functions, and they’ll work perfectly.
- You can also use C++ features like classes and objects to make your code more reusable and efficient.
A Blend of C and C++ Code Styles #
The compatibility between C and C++ is not just theoretical; it’s a defining feature of Arduino’s programming environment. Both styles are commonly used and even mixed within the same program. This blend is also evident in Arduino’s core libraries:
C style Programming: Arduino functions like digitalWrite()
and analogRead()
reflect a procedural, C-style approach. These loose functions are simple, hardware-focused, and straightforward to use.
C++ style Programming: Features like the Serial
object (an instance of the HardwareSerial
class) showcase Arduino’s use of object-oriented programming, taking use of the flexibility of C++.
Here’s an example of how both styles coexist in an Arduino sketch:
#include <Servo.h> // C++ style library
Servo myServo; // Create an object from the Servo class (C++)
void setup() {
myServo.attach(9); // C++ style function call
pinMode(13, OUTPUT); // C style function call
}
void loop() {
digitalWrite(13, HIGH); // C style function
delay(1000); // C style function
myServo.write(90); // C++ style object method
delay(1000);
}
Arduino Code and Standard C++ Compilers #
Code written in Arduino can be compiled using standard C++ compilers. The Arduino IDE processes sketches and converts them into valid C++ code, ensuring compatibility with standard C++ rules and syntax.
How the Arduino IDE Simplifies Development #
The Arduino IDE streamlines the development process by handling many tasks automatically:
- It generates a
main()
function and organizes the code structure. - It includes necessary libraries like
Arduino.h
for accessing hardware-related functions. - It manages low-level hardware initialization, such as setting up pins for input/output.
Using Standard C++ Without the Arduino IDE #
If one wanted to bypass the Arduino IDE, it’s entirely possible to write pure C++ code directly for Arduino-compatible microcontrollers. However, this approach requires handling tasks that the Arduino IDE automates. For example, you’d need to manually include the necessary libraries, define a main()
function, and set up hardware configurations like pin modes and clock settings.
Recap and Key Points #
Arduino is Not a Standalone Language: Arduino uses C++ as its primary programming language but simplifies it for ease of use.
The Arduino "Language": The so-called "Arduino language" is not a new language but a simplified framework built on C++, using tailored libraries and features.
Arduino’s Unique Code Structure: Arduino sketches are more like a framework built on C++ rather than standard C++ programs. They use a simplified structure with predefined functions like setup()
and loop()
, which abstract away the complexity of a typical C++ program structure. While Arduino leverages C++, its distinct approach prevents it from being directly referred to as standard C++.
Conclusion #
Arduino bridges the gap between simplicity and complexity, making it a powerful platform for both beginners and advanced users. While it is based on C++, Arduino’s tailored framework simplifies programming by abstracting many of the complexities associated with standard C++. The so-called “Arduino language” is not a new language but rather a streamlined way to interact with hardware using C++ as its foundation.
Arduino's use of C++ makes it both accessible and versatile. It simplifies programming for beginners, offering an easy way to start coding with minimal complexity. At the same time, the choice of C++ allows advanced users to take full advantage of its powerful features for creating sophisticated and modular projects.