What coding language does Arduino use?

 The short answer is: Arduino uses a variant of C++.

But that's a bit like saying "a car uses an internal combustion engine" – it's technically correct but doesn't give you the full picture. Let's break it down in detail.



1. The Core Language: C++ with a Simplifying Wrapper

The code you write for Arduino is processed by a C++ compiler (avr-g++). However, the Arduino environment provides a simplified layer on top of standard C++.

  • It's C++: You can use core C++ features like functions, variables, classes, and libraries.

  • It's Simplified: The complex parts of C++ (like memory management with new/delete and the Standard Template Library - STL) are often avoided or unavailable on these small microcontrollers due to limited memory and processing power.

  • It has a Unique Structure: Arduino programs are structured around two main functions, setup() and loop(), which is not standard C++ but is provided by the Arduino core libraries.

2. What It's Often Called: "The Arduino Programming Language"

Because of this specific environment and structure, many people refer to it informally as "the Arduino programming language." While not technically a new language, it's a practical way to describe the combination of C++ and the Arduino-specific libraries and functions.


Key Characteristics of Arduino Code

Here’s what this "C++ variant" looks like in practice:

1. The Mandatory setup() and loop() Functions

Every Arduino sketch (program) must have these two functions. This is the most distinctive feature.

cpp
void setup() {
  // This code runs ONCE when the board powers up or resets.
  // Used for initialization, like setting pin modes.
  pinMode(13, OUTPUT);
}

void loop() {
  // This code runs repeatedly, over and over again, forever.
  // This is the main logic of your program.
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

2. Simplified, Pre-Written Functions (The Arduino API)

Arduino provides a rich set of easy-to-use functions for common tasks:

  • pinMode(pin, mode) - Sets a pin as INPUTOUTPUT, or INPUT_PULLUP.

  • digitalWrite(pin, value) - Writes a HIGH or LOW value to a digital pin.

  • digitalRead(pin) - Reads a HIGH or LOW value from a digital pin.

  • analogRead(pin) - Reads a value from an analog pin (e.g., from a potentiometer).

  • analogWrite(pin, value) - Writes a "simulated" analog value (PWM) to a pin.

  • delay(ms) - Pauses the program for a specified number of milliseconds.

  • Serial.begin(9600) - Starts communication with the Serial Monitor.

3. It's Mostly "Bare C++"

You use standard C/C++ syntax for everything else:

  • Variables: intfloatcharbytebool

  • Control Structures: ifelseforwhileswitch

  • Operators: =+-*/==!=&&||

  • Functions: You can create your own.

  • Libraries: You can #include both standard C++ libraries (e.g., #include <math.h>) and user-contributed Arduino libraries (e.g., #include <Servo.h>).


Example: Blink Sketch in "Arduino C++"

This is the "Hello, World!" of Arduino. Notice the mix of standard C++ and Arduino-specific functions.

cpp
// This is a preprocessor directive, standard in C/C++
#define LED_PIN 13

// A global variable, standard C++
int delayTime = 1000;

// The mandatory Arduino functions
void setup() {
  // Using an Arduino-specific function
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  // Using Arduino-specific functions
  digitalWrite(LED_PIN, HIGH); // Turn LED on
  delay(delayTime);            // Wait
  digitalWrite(LED_PIN, LOW);  // Turn LED off (standard C++ comment too)
  delay(delayTime);            // Wait

  // Using a standard C++ 'if' statement
  if (delayTime > 2000) {
    delayTime = 1000; // Reset the delay time
  }
}

Summary

QuestionAnswer
What language is an Arduino program?A simplified version of C++.
What compiler is used?A C++ compiler (avr-g++).
Is it a unique language?Not officially, but it's often called one because of its specific structure and API.
Can I use standard C++?Yes, for the most part, but you should avoid memory-intensive features.
What makes it "Arduino"?The mandatory setup()/loop() structure and the pre-built functions like digitalWrite() and delay().

So, when you are coding for Arduino, you are essentially writing C++ code designed to be beginner-friendly and optimized for microcontrollers. This is a huge part of why Arduino has been so successful—it lowers the barrier to entry for embedded systems programming.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How To Connect Stm32 To PC?

How do you set up ADC (Analog-to-Digital Converter) in STM32?