Arduino programming language can be divided in three main parts: functions, values (variables and
constants), and structure.
SYNTAX
Syntax in programming refers to the set of rules that dictate how a program must be structured and written in a
specific programming language for it to be valid and understandable by a computer. It is analogous to the grammar
and punctuation rules of natural human languages.
The Arduino Integrated Development Environment (IDE)
The Arduino programming language is based on C/C++, but it is designed to be simpler and easier to learn. The
most intuitive way to think about programming is like building with LEGO blocks: certain rules must be followed
and different building blocks can be used to build bigger blocks. The Integrated Development Environment (IDE) is
the software used to write code for Arduino.
General Syntax
• Every line must either end with a semicolon ‘;’ unless it is a conditional, loop, or function
• Conditionals, loops, and functions are enclosed in curly brackets {}
• Comments start with a //
o Comments are text that the program ignores
o Used to label and explain code
Datatypes
Datatypes are the different kinds of data values that can be used, manipulated and stored using C++. Table 1
below shows the most basic and widely used datatypes.
Datatype What it stores (examples) Default value Notes
bool
A true value (1, TRUE, HIGH) or 0, FALSE, English Boolean values such as HIGH or LOW
(Boolean a false value (0, FALSE, LOW) LOW must be in all caps
)
An integer number (-5, 15, 1047,
int 0 Can be positive or negative
etc.)
A decimal number (-0.5, 123.77,
double 0 Can be positive or negative
etc.)
A decimal number (-0.5, 123.77,
float 0 Can be positive or negative
etc.)
A single character (‘c’, ‘A’, ‘5’, ‘?’,
char Indeterminate Must be enclosed in single quotes
etc.)
A sequence of characters
string (“Hello World!”, Empty (“”) Must be enclosed in double quotes
“10”, “157+5”, etc.)
Operators
Operators perform operations on variables and constants. The results of these operations are usually stored in a
variable. Table 2 shows the includes common operators in Arduino IDE.
Table 2: Common Arduino IDE Operators
Operator What it does Notes
= Assigns a value to a variable
+ Adds two or more values
- Subtracts two or more values
* Multiplies two or more values
/ Divides two or more values
++ Increment by 1 Usually used in loops
-- Decrement by 1 Usually used in loops
Usually used in
== Checks if two value are equal
conditionals
Usually used in
!= Checks if two value are not equal
conditionals
Usually used in
> or < Less than/Greater than comparison
conditionals
Usually used in
<= or >= Less than/greater than or equal to comparison
conditionals
Boolean AND or Boolean OR Used to cascade multiple Boolean Usually used in
&& or ||
operations conditionals
Constants and Variables
Constants and variables hold data according to their datatype. They need to be given a name so that they can be
referred to later. Constants hold data that will not change while a program is running. Constants usually contain
pin numbers or sensor threshold values. Variables contain data that WILL change while a program is running.
Variables usually contain sensor values and other values that need to have mathematical operations done on
them. Figure 1 is an example of how to create different constants and variables.
Figure 1: Constants & Variables
Conditional Statements
Conditional statements run code enclosed by their curly brackets when a condition is met (Figure 2).
Figure 2: Conditional statements
Loops
Loops run the code enclosed by their curly brackets a specific number of times or until a condition is met. While-
loops are used to perform a task until a condition is met For-loop. For-loops are used when you want something to
run a specific number of times. Although they seem complicated at first, the structure of most for-loops is the
same. In the parentheses, the first part sets a variable (usually ‘i’ for ‘index’) to a value used to begin a count, the
middle is the condition when the loop stops, and the third part is increments or decrement the counting variable
(Figure 3).
Figure 3: While and for-loops
Functions
Functions are predefined sections of code that can be called multiple times to perform a defined task. These are
typically used when a programmer needs to perform the same task multiple times.
The header portion is the function declaration. This tells the code what the function will return (return type), what
the name of the function is (function name), and any parameters [Link] return type can be any valid Arduino
C data type. If the function will not return any values, the return type should be void. Parameters are optional when
creating a function. Figure 4 shows the anatomy of a function.
Figure 4: Function Prototype
The header portion is the function declaration. This tells the code what the function will return (return type), what
the name of the function is (function name), and any parameters [Link] return type can be any valid Arduino
C data type. If the function will not return any values, the return type should be void. Parameters are optional when
creating a function.
For example, the following function is called addOne. This function takes in an integer, and returns that integer
value plus one (Figure 5).
Figure 5: addOne() Function
An example use of this function is below. After calling the function addOne(4), the value of the integer number
would be 5 (Figure 7).
Figure 6: Using the addOne() function
Commonly Used Arduino Functions
These functions are included with Arduino IDE to be used with the Arduino board (Table 3).
Table 3: Common Arduino IDE Functions
• Standard Arduino Functions: Specific functions provided by the Arduino library for interacting with
hardware (e.g., pinMode(), digitalWrite(), analogRead(), [Link]()).
Function What it does
pinMode(pin,mode) Sets a pin as an input or output
digitalWrite(pin, value) Sets a digital output pin to HIGH or LOW
digitalRead(pin) Reads a digital input pin as HIGH or LOW
analogWrite(pin, value) Sets an analog output pin to a value 0-1023
analogRead(pin) Reads an analog output pin as a value 0-1023
delay(milliseconds) Pauses the program for a certain amount of time
[Link](value) Begins the Serial Monitor with a baud rate of value
[Link](value) Prints the value (variable) to the Serial Monitor.