Arduino - Reference

Arduino

Guide

Tutorials

Reference

Hardware

Software

Forum

Playground

Exhibition

Hacking

FAQ

Buy

Language | Libraries | Comparison

# Arduino Reference

Arduino programs can be divided in three main parts: structure, values (variables and constants), and functions. The Arduino language is based on C/C++.

## Structure

An Arduino program run in two parts:

- void setup()
- void loop()

setup() is preparation, and loop() is execution. In the setup section, always at the top of your program, you would set pinMode, initialize serial communication, etc. The loop section is the code to be executed -- reading inputs, triggering outputs, etc.

- Variable Declaration
- Function Declaration

#### Control Structures

- if
- if...else
- for
- switch case
- while

#### Arithmetic Operators

- plus(addition)
- -(subtraction)
- *(multiplication)
- /(division)
- %(modulo)

#### Comparison Operators

- == (equal to)
- != (not equal to)
- < (less than)
- > (greater than)
- <= (less than or equal to)
- >= (greater than or equal to)

#### Boolean Operators

- && (and)
- || (or)
- ! (not)

#### Bitwise Operators

- & (bitwise and)
- | (bitwise or)
- ^ (bitwise xor)
- ~ (bitwise not)
- << (bitshift left)
- >> (bitshift right)

#### Further Syntax

- ; (semicolon)
- {} (curly braces)
- // (single line comment)
- /* */ (multi-line comment)
- #define
- #include?

## Variables

Variables are expressions that you can use in programs to store values, like e.g. sensor reading from an analog pin. They can have various types, which are described below.

#### Data Types

- char
- byte
- int
- unsigned int
- long
- unsigned long
- float
- double
- string
- array

#### Variable Scope

- variable scope
- static

#### Constants

Constants are labels for certain values which are preset in the Arduino compiler. You do not need to define or initialize constants. Arduino includes the following pre-defined constants.

- HIGH | LOW
- INPUT | OUTPUT

- IntegerConstants

#### Utilities

- cast (cast operator)
- sizeof() (sizeof operator)

## Reference

- keywords

## Functions

Digital I/O

- pinMode(pin, mode)
- digitalWrite(pin, value)
- int digitalRead(pin)

Analog I/O

- int analogRead(pin)
- analogWrite(pin, value) - PWM

Advanced I/O

- shiftOut(dataPin, clockPin, bitOrder, value)
- unsigned long pulseIn(pin, value)

Time

- unsigned long millis()
- delay(ms)
- delayMicroseconds(us)

Math

- min(x, y)
- max(x, y)
- abs(x)
- constrain(x, a, b)

Random Numbers

- randomSeed(seed)
- long random(max)
- long random(min, max)

External Interrupts

These functions allow you to trigger a function when the input to a pin changes value.

- attachInterrupt(interrupt, function, mode)
- detachInterrupt(interrupt)

Serial Communication

Used for communication between the Arduino board and a computer or other devices. This communication happens via the Arduino board's serial or USB connection and on digital pins 0 (RX) and 1 (TX). Thus, if you use these functions, you cannot also use pins 0 and 1 for digital i/o.

- Serial.begin(speed)
- int Serial.available()
- int Serial.read()
- Serial.flush()
- Serial.print(data)
- Serial.println(data)

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

Edit Page | Page History | Printable View | Recent Site Changes