Arduino programming is primarily based on **C++**, but it has some characteristics and simplifications that make it distinct. Here's a detailed explanation to help you understand the relationship between Arduino and C++:
### 1. **Arduino Language and Environment**
- The **Arduino platform** provides a simplified programming environment that is designed to be beginner-friendly, especially for those who are new to electronics and programming.
- The code written for Arduino is often referred to as being written in the "Arduino language," but this is a bit of a misnomer. It's not a new language; rather, it's **C++ with additional libraries and abstractions**.
- The **Arduino Integrated Development Environment (IDE)** comes with a collection of libraries and built-in functions that make it easier to interact with hardware like sensors, motors, and LEDs without needing a deep understanding of the underlying microcontroller registers.
### 2. **C++ with Simplifications**
- While Arduino uses C++, it hides many of the complexities that you would normally deal with if you were programming directly in C++ for microcontrollers.
- For instance, in standard C++ programming for embedded systems, you often need to manage low-level details like memory management and hardware register manipulation. Arduino abstracts much of this away.
- In Arduino code, functions like `pinMode()`, `digitalWrite()`, and `analogRead()` are provided by the Arduino core library. These functions simplify the process of configuring and interacting with the microcontroller's pins.
### 3. **Arduino Program Structure**
- An Arduino program, also known as a **sketch**, usually contains two main functions:
- `setup()`: This function runs once when the microcontroller starts up. It's typically used for initializing settings, such as pin modes.
- `loop()`: This function runs repeatedly after `setup()` has finished. It's where the main logic of the program goes.
- Here's an example of a simple Arduino sketch:
```cpp
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as an output
}
void loop() {
digitalWrite(13, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
}
```
- This example shows how simple it is to blink an LED using Arduino, thanks to the abstractions provided by the Arduino libraries. Under the hood, though, these functions (`pinMode`, `digitalWrite`, etc.) are implemented in C++.
### 4. **Differences from Standard C++**
- While Arduino code is fundamentally C++, some standard C++ features (like classes, objects, and templates) are available, but other advanced features and parts of the C++ Standard Library may be limited or unavailable due to the constrained environment of the microcontroller.
- For example, many Arduino boards have limited memory and processing power, so you won't typically use features like dynamic memory allocation (`new` and `delete`) extensively.
- The **Arduino Core** libraries provide a higher-level API for hardware interaction, making it unnecessary to deal directly with the hardware registers and interrupts in many cases.
### 5. **Not Assembly or Machine Code (AC)**
- "AC" usually refers to **Alternating Current** in electrical contexts, but if you meant **Assembly Code** or **Machine Code**, then Arduino is not written in these languages. Assembly is a low-level programming language closely tied to the hardware architecture, while machine code is the binary instructions that a microcontroller executes.
- Arduino simplifies programming by abstracting away from Assembly and Machine Code, allowing developers to write in a higher-level language (C++).
### Summary
- **Arduino is based on C++,** with a simplified syntax and additional libraries tailored to make microcontroller programming more accessible.
- It provides a set of functions and abstractions that make it easy to perform complex tasks (like controlling hardware) without requiring in-depth knowledge of C++ or the microcontroller's architecture.
- By using the Arduino IDE and its libraries, you are essentially writing C++ code that gets compiled and uploaded to the microcontroller.
In short, **Arduino programming is C++** with a set of added libraries and simplifications to make it easier for beginners to interact with hardware.