Arduino programming syntax is based on C/C++, providing a structured yet accessible layout for controlling hardware. Every standard program—called a sketch—relies on a set of punctuation rules, structural blocks, and built-in functions. [1, 2, 3, 4]
1. Core Sketch Structure
Every Arduino sketch requires two foundational functions to run. If either is missing, the code will fail to compile. [1, 2, 3]
void setup(): This block runs exactly once when the Arduino powers up or resets. It is used to initialize variables, configure hardware pin modes, or start sensor communication. [1, 2]
void loop(): This block runs continuously in a cycle from top to bottom as long as the board has power. It handles the main logic, reading inputs, and responding with outputs. [1, 2]
cpp
void setup() {
// Put your setup code here, to run once:
}
void loop() {
// Put your main code here, to run repeatedly:
}
2. Basic Syntax Rules & Punctuation
Missing punctuation is the most common reason for compilation errors in the Arduino IDE. [1, 2]
Semicolon ;: Every single instruction or command line must end with a semicolon. It tells the microcontroller that a thought is finished.
Curly Braces { }: Used to define the start and end of blocks of code, such as functions, loops, or conditional rules.
Case Sensitivity: Arduino code differentiates between capital and lowercase letters. Writing digitalwrite() instead of digitalWrite() will cause an error.
Single-line Comment //: Anything typed after two slashes is ignored by the microcontroller, allowing you to leave helpful personal notes.
Multi-line Comment /* ... */: Safely blocks out multiple lines of text or sections of code for troubleshooting. [1, 2, 3, 4, 5]
3. Data Types and Variables
Variables act as containers to hold changing numbers or characters. You must declare what type of data a container holds before using it. [1, 2, 3, 4, 5]
int: Stores integers (whole numbers), such as 5 or -42.
float: Stores decimal numbers, such as 3.14 or 22.5.
boolean / bool: Holds only two potential states: true (HIGH) or false (LOW).
char: Stores single characters, wrapped in single quotes, like 'A'.
String: Stores strings of text wrapped in double quotes, like "Hello World". [1, 2, 3, 4, 5]
cpp
int ledPin = 13; // Stores the whole number 13
float voltage = 4.5; // Stores a decimal number
bool isPressed = true;// Stores a true/false condition
4. Essential Built-In Functions [1]
The Arduino Language Reference includes thousands of predefined functions, but a core handful handles most foundational tasks. [1, 2]
Pin Configuration & Control
pinMode(pin, mode): Configures a specific digital pin to behave as an INPUT (to read a sensor) or an OUTPUT (to power a component like an LED).
digitalWrite(pin, value): Sends a signal to a pin. Setting it to HIGH provides 5V (on); setting it to LOW drops it to 0V (off).
digitalRead(pin): Checks whether an input pin is actively receiving electrical power (HIGH or LOW). [1, 2, 3, 4, 5]
Timing
delay(ms): Halts all program activities for a specific number of milliseconds. Note that delay(1000) pauses the entire board for exactly 1 second. [1, 2]
5. Control Structures
Control structures direct the flow of your program based on real-time hardware criteria. [1]
Conditional Statements (if / else)
Executes a target chunk of code only if a specific condition proves true. [1, 2]
cpp
if (digitalRead(2) == HIGH) {
digitalWrite(13, HIGH); // Turns LED on if pin 2 receives a signal
} else {
digitalWrite(13, LOW); // Turns LED off otherwise
}
For Loops
Repeats a targeted block of code a set number of times using an incrementing counter variable. [1, 2]
cpp
// Flashes an LED 5 times in a row
for (int i = 0; i < 5; i++) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
Practical Example: The Classic Blink Sketch
This combined script utilizes structural syntax, pin modes, output control, and delays to continually blink an onboard indicator light. [1, 2]
cpp
// The setup function configures pin 13 as an output once
void setup() {
pinMode(13, OUTPUT);
}
// The loop function blinks the LED continuously
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
}
To dive deeper into advanced operations, review the Arduino Starting Guide or browse the Built-In Examples directly inside your local Arduino IDE application. [1, 2]