Syntax
Arduino syntax is based on C++ and follows a set of structural rules that the compiler uses to understand your program. These rules cover how to write comments, group blocks of code, include libraries, define constants, and terminate statements. Mastering the basic syntax is the first step to writing correct and readable Arduino sketches.
// (single line comment)
Single line comments begin with // and extend to the end of the line. The compiler ignores everything after // on that line. Use comments to explain your code, document pin connections, or temporarily disable lines during debugging.
void setup() {
// Initialize serial communication at 9600 baud
Serial.begin(9600);
// Serial.println("Debug message"); // disabled for now
}
void loop() {
// Toggle LED every second
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
In this example, comments explain what each section of code does. The commented-out Serial.println() line is ignored by the compiler, which is useful for temporarily removing code without deleting it. Use comments to explain why code exists, not what it does โ the code itself should show the what.
/* */ (block comment)
Block comments start with /* and end with */. They can span multiple lines and are used for longer explanations, licensing information, or disabling larger blocks of code.
/*
Blink Example
Turns the built-in LED on for one second, then off for one second.
This example code is in the public domain.
*/
void setup() {
pinMode(LED_BUILTIN, OUTPUT); /* inline block comment */
}
void loop() {
/* Block comments can
span multiple lines */
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
Block comments can appear on a single line or stretch across many lines. Unlike single line comments, they can also be used inline within a line of code. Be careful not to nest block comments โ /* /* */ */ will cause a compiler error.
(curly braces)
Curly braces define the beginning and end of a block of code. They group multiple statements into a single unit for functions, loops, conditionals, and other control structures. Every opening brace { must have a matching closing brace }.
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // inside setup block
}
void loop() {
// Inside loop block
for (int i = 0; i < 3; i++) { // inner block for loop
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
}
delay(1000);
}
In this example, there are three levels of brace nesting: the setup() function body, the loop() function body, and the for loop body. Proper indentation makes it easier to see which braces match. Most Arduino IDEs provide auto-indentation and brace matching to help keep your code organized.
; (semicolon)
The semicolon terminates a statement in Arduino. Every complete instruction must end with a semicolon. Forgetting a semicolon is one of the most common compiler errors.
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // statement ends with ;
Serial.begin(9600);
int x = 42;
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
// No semicolon needed after closing braces
}
Each line in this example is a complete statement terminated by a semicolon. Note that semicolons are not used after #define, #include, or closing braces }. If you see an error like expected ';' before '...', check the line indicated (or the line before it) for a missing semicolon.
#define
The #define directive creates a macro โ a text substitution that the preprocessor performs before compilation. It is commonly used to define constant values, pin numbers, or short code snippets. By convention, macro names are written in UPPERCASE.
#define LED_PIN 13
#define DELAY_TIME 500
#define SENSOR_MIN 0
#define SENSOR_MAX 1023
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(DELAY_TIME);
digitalWrite(LED_PIN, LOW);
delay(DELAY_TIME);
}
Here, #define LED_PIN 13 causes the preprocessor to replace every occurrence of LED_PIN with 13 before the code is compiled. Unlike const, #define does not use any RAM โ the substitution happens at the text level. However, #define does not follow scope rules and provides no type checking, so const is preferred for typed constants in modern Arduino code.
#include
The #include directive inserts the contents of a header file into your sketch. It is used to include libraries that provide additional functionality, such as controlling a display, reading an SD card, or communicating over I2C.
#include <LiquidCrystal.h> // standard library
#include <SD.h> // SD card library
#include "MyLocalHeader.h" // local file in sketch folder
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("Hello, World!");
}
void loop() {
// nothing to do here
}
Angle brackets <> are used for libraries installed in the system library folder. Quotes "" are used for header files located in your sketch directory. Most Arduino libraries are included with #include <LibraryName.h> at the top of your sketch, before any other code.