Arduino Language Reference

This reference contains the most important commands and structures in the Arduino programming language.

Program Structure

void setup() {
// Initialization, runs once when the program starts
}

void loop() {
// Main code, runs repeatedly
}

Comments

// Single-line comment

/*
Multi-line comment
*/

Functions

void myFunction() {
// Function code
}

// Function call
myFunction();

Input/Output

Pin Mode

pinMode(pinLED, OUTPUT); // Set pinLED as output

Digital

int value = digitalRead(pin); // Read
digitalWrite(pin, value); // Write

Analog (PWM)

int value = analogRead(pin); // Read
analogWrite(pin, value); // Write

Time Control

// Predefined variables
millis(), delay(), delayMicroseconds()

Communication

Serial (UART)

Serial.begin(9600); // Start communication
Serial.print("Hello"); // Send data
Serial.println(" World"); // Send data with new line
while (Serial.available()) { // Read data
char data = Serial.read();
}

I2C

#include <Wire.h>

void setup() {
Wire.begin(); // Start I2C communication
}

void loop() {
Wire.beginTransmission(8); // I2C device address
Wire.write("Hello"); // Send data
Wire.endTransmission(); // End transmission
}

SPI

#include <SPI.h>

void setup() {
SPI.begin(); // Start SPI communication
}

void loop() {
digitalWrite(SS, LOW); // Enable device
SPI.transfer(0x0A); // Send data
digitalWrite(SS, HIGH); // Disable device
}

Operators and Comparisons

Comparisons

// x equals y
x == y

// x not equals y
x != y

// x less than y
x < y

// x greater than y
x > y

// x less than or equal to y
x <= y

// x greater than or equal to y
x >= y

Arithmetic Operators

// Addition
a + b

// Subtraction
a - b

// Multiplication
a * b

// Division
a / b

// Modulus
a % b

Bitwise Operators

// Bitwise AND
a & b

// Bitwise OR
a | b

// Bitwise XOR
a ^ b

// Bitwise NOT
~a

// Left shift
a << b

// Right shift
a >> b

Compound Operators

// Increment
a++

// Decrement
a--

// Compound addition
a += b

// Compound subtraction
a -= b

// Compound multiplication
a *= b

// Compound division
a /= b

// Compound AND
a &= b

// Compound OR
a |= b

Logical Operators

// NOT
!a

// AND
a && b

// OR
a || b

Variables and Type Conversion

Data Types

// Integer
int var = 100;

// Unsigned integer
unsigned int var = 100;

// Long integer
long var = 100000L;

// Unsigned long integer
unsigned long var = 100000L;

// Floating point
float var = 1.117;

// Character
char var = 'A';

// Byte
byte var = B10010;

// Word
word var = 10000;

Type Conversion

// Convert to integer
int(variable);

// Convert to float
float(variable);

// Convert to character
char(variable);

// Convert to byte
byte(variable);

Arrays

Creating Arrays

// Create an array
int myArray[5];

// Initialize an array
int myArray[] = {2, 4, 8, 3, 6};

Working with Arrays

// Set value of an element
myArray[0] = 10;

// Get value of an element
x = myArray[4];

Strings

Strings as Character Arrays

char string1[15];
char string2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char string3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char string4[] = "text";

Strings as String Objects

String txtMsg = "Hello";
String txtMsg = String('a');
String txtMsg = String("Text");
String txtMsg = String("text1" + "text2");

Conditions

IF

if (variable < 10) {
// action A
} else if (variable >= 100) {
// action B
} else {
// action C
}

SWITCH

switch (variable) {
case 1:
// action A
break;
case 2:
// action B
break;
default:
// default case
}

Loops

FOR

for (int i = 0; i <= 100; i++) {
// action
}

WHILE

while (variable < 100) {
// action
variable++;
}

DO WHILE

do {
// action
variable++;
} while (variable < 100);

Math Functions

Basic

// Minimum
min(a, b);

// Maximum
max(a, b);

// Absolute value
abs(a);

// Constrain
constrain(x, a, b);

// Linear mapping
map(x, x1, x2, y1, y2);

Random Numbers

// Seed initialization
randomSeed(seed);

// Generate random number
random(a, b);

Libraries

#include <Wire.h> // I2C
#include <SPI.h> // SPI
#include <Servo.h> // Servo
#include <LiquidCrystal.h> // LCD
#include <WiFi.h> // WiFi
#include <Ethernet.h> // Ethernet
#include <SD.h> // SD Card