Arduino coding language is essentially **C++** with some simplifications and additional features specifically designed to work with **Arduino microcontrollers**. The Arduino environment uses a simplified version of C++ to make it more accessible to beginners, while still being powerful enough for more advanced users to create complex projects.
### Key Components of Arduino Coding Language:
1. **Arduino IDE (Integrated Development Environment):**
- The Arduino IDE is a user-friendly platform where you write, compile, and upload code to the Arduino board.
- It simplifies the coding process, providing features such as code highlighting and automatic library management.
2. **Sketches:**
- In Arduino terminology, a "sketch" is simply a program written in the Arduino language.
- Each sketch must have two main functions:
- `setup()` – This runs once when the board is powered on or reset, and is used to initialize settings (like pin modes, serial communication, etc.).
- `loop()` – This runs continuously after the `setup()` function, controlling the ongoing operation of the board.
Example:
```cpp
void setup() {
// Initialize a pin as an output
pinMode(13, OUTPUT);
}
void loop() {
// Turn the LED on
digitalWrite(13, HIGH);
delay(1000); // Wait for a second
// Turn the LED off
digitalWrite(13, LOW);
delay(1000); // Wait for a second
}
```
3. **C++ Basis:**
- Arduino sketches are written in a combination of C and C++. Although Arduino uses a simplified environment, it's still largely based on the C++ programming language, with access to standard structures like loops (`for`, `while`), conditionals (`if`, `else`), and data types (`int`, `float`, etc.).
4. **Simplified Functions:**
- **Pin Control Functions:** Arduino simplifies many common tasks like reading from or writing to digital and analog pins through functions such as `digitalWrite()`, `digitalRead()`, and `analogRead()`.
- **Delay Functions:** You can control the timing of your program using `delay()`, which pauses the program for a given number of milliseconds.
5. **Libraries:**
- The Arduino ecosystem includes many pre-built libraries for handling specific tasks, such as controlling motors, reading from sensors, communicating via serial or wireless protocols, and more.
- These libraries abstract complex hardware control, making it easier to add advanced features to your projects without writing everything from scratch.
Example of importing a library:
```cpp
#include <Servo.h>
Servo myServo; // create a servo object
void setup() {
myServo.attach(9); // attaches the servo on pin 9
}
void loop() {
myServo.write(90); // rotate servo to 90 degrees
}
```
### Differences from Standard C++:
1. **Predefined Functions:** Arduino provides predefined functions (like `digitalWrite()`, `analogRead()`, `millis()`) to simplify interacting with the hardware. This makes it easier to control microcontroller functions without needing detailed low-level programming knowledge.
2. **No `main()` function:** In typical C++ programs, the `main()` function is essential, but in Arduino, the IDE provides this for you behind the scenes. Instead, users write the `setup()` and `loop()` functions.
3. **Arduino-Specific Libraries:** While Arduino code is essentially C++, it comes with built-in libraries that directly control microcontroller-specific hardware like I2C, SPI, and UART.
### The Role of the Compiler and Bootloader:
- **Compiler:** When you write code in the Arduino IDE, it gets compiled into machine code that the microcontroller can understand. The IDE uses **AVR-GCC** (GNU C Compiler for AVR microcontrollers) for most Arduino boards.
- **Bootloader:** The Arduino board includes a bootloader, a small piece of software pre-loaded on the microcontroller that allows you to upload code directly via USB without the need for a hardware programmer.
### Example Project: Blinking an LED
This simple example demonstrates how to blink an LED on and off every second:
```cpp
void setup() {
pinMode(13, OUTPUT); // Pin 13 is connected to the onboard LED
}
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
}
```
In summary, Arduino coding language is a simplified version of C++ with some pre-built libraries and functions tailored for microcontroller projects. This makes it easy for beginners to start building electronic projects while still allowing advanced users to write powerful, low-level code when needed.