Arduino Language Reference: Functions and Libraries

This article introduces the Arduino language reference.

Please refer to the linked articles for detailed explanations and sample programs (sample sketches) for each standard function and standard library.

TOC

Data Types

Data TypesRangeBitsExplanation
boolean0, 18bitUsed to determine "True" or "False"
char-128 to 1278bitUsed for characters
unsigned char0 to 2558bit
int-32,768 to 32,767
(Arduino Due: -2,147,483,648 to 2,147,483,647)
16bit
(32bit)
Used for integers
unsigned int0 to 65,535
(Arduino Due: 0 to 4,294,967,295)
16bit
(32bit)
long-2,147,483,648 to 2,147,483,64732bit
unsigned long0 to 4,294,967,29532bit
float3.4028235E+38 to -3.4028235E+3832bitUsed for decimals

Operators

Arithmetic Operators

Arithmetic OperatorsExamplesExplanation
+a = b + cAssign "b + c" into 'a'
-a = b - cAssign "b - c" into 'a'
*a = b * cAssign "b * c" into 'a'
/a = b / cAssign "b - c" into 'a'
%a = b % cAssign "b % c" into 'a'
%: remainder
=a = bAssign 'b' into 'a'

Compound Operators

Compound OperatorsExamplesExplanation
+=a += bAssign "b + c" into 'a'
(equivalent to the expression "a = b + c")
-=a -= bAssign "b - c" into 'a'
(equivalent to the expression "a = b - c")
*=a *= bAssign "b * c" into 'a'
(equivalent to the expression "a = b * c")
/=a /= bAssign "b - c" into 'a'
(equivalent to the expression "a = b / c")
%=a %= bAssign "b % c" into 'a'
(equivalent to the expression "a = b % c")

Increment/Decrement Operators

Increment/Decrement OperatorsExamplesExplanation
++a++returns the old value of 'a' and increment 'a' by one
++aincrement 'a' by one and returns the new value of 'a'
--a--returns the old value of 'a' and decrement 'a' by one
--adecrement 'a' by one and returns the new value of 'a'

Comparison Operators

Comparison OperatorsExamplesExplanation
==a == bTrue if 'a' and 'b' are equal
False if 'a' and 'b' are not equal
!=a != bTrue if 'a' and 'b' are not equal
False if 'a' and 'b' are equal
<a < bTrue if 'a' is smaller than 'b'
False if 'a' is equal or bigger than 'b'
>a > bTrue if 'a' is bigger than 'b'
False if 'a' is equal or smaller than 'b'
<=a <= bTrue if 'a' is smaller than or equal to 'b'
False if 'a' is bigger than 'b'
>=a >= bTrue if 'a' is bigger than or equal to 'b'
False if 'a' is smaller than 'b'

Boolean Operators

Boolean OperatorsExamplesExplanation
&&(a >= 10) && (a < 100)True if "a >= 10" and "a < 100"
Otherwise, False
||(a == 10) || (a == 100)True if "a == 10" or "a == 100"
Otherwise, False
!!(a == 10)True if not "a == 10"
Otherwise, False

Control Structure

if

if(condition A) {
  //Executed if condition A is satisfied.
}
if(condition A) {
  //Executed if condition A is satisfied.
}
else {
  //Executed if condition A is not satisfied.
}
if(condition A) {
  //Executed if condition A is satisfied.
}
else if(condition B) {
  //Executed if condition A is not satisfied and condition B is satisfied.
}
else {
  //Executed if conditions A and B are not satisfied.
}

for

for(initialization; condition; increment) {
  //Repeat while condition is satisfied.
}

while

while(condition) {
  //Repeat while condition is satisfied.
}

do while

do {
  //Always execute once, then Repeatedly executed while condition is satisfied.
} while(condition);

switch case

switch(variable) {
  case constant1:
    //Execute if variable is 1.
    break;
  case constant2:
    //Execute if variable is 2.
    break;
  case constant3:
    //Execute if variable is 3.
    break;
  default:
    //Execute if all variables do not apply.
}

Standard Functions

The Arduino IDE and Arduino Web Editor provide Arduino standard functions.

The following link is to the reference page on the official Arduino Web site. This article summarizes the Arduino standard functions with reference to these information.

Digital I/O

FunctionsParametersReturnsExplanation
void pinMode(pin, mode)pin: The digital pin number to set
mode: INPUT, OUTPUT, or INPUT_PULLUP
NoneConfigures the pin to be input, output, or pullup
void digitalWrite(pin, value)pin: The digital pin number to set
value: HIGH or LOW
NoneWrite HIGH or LOW to the digital pin
int digitalRead(pin)pin: The digital pin number to setHIGH or LOWReads HIGH or LOW from the digital pin 

Analog I/O

FunctionsParametersReturnsExplanation
void analogReference(type)type: Reference voltage settingNoneConfigures the reference voltage for analog input
int analogRead(pin)pin: The analog pin number to set10bits: 0 to 1023
12bits: 0 to 4095
Reads the value from the analog pin
void analogWrite(pin, value)pin: The digital pin number to set
value: The duty cycle(0 to 255)
NoneWrites an analog value (PWM wave) to the digital pin

Advanced I/O

FunctionsParametersReturnsExplanation
void tone(pin, unsigned int frequency, unsigned long duration)pin: The digital pin number to set
frequency: The frequency of the tone(Hz)
duration: The duration of the tone(ms)
NoneGenerates a square wave
void noTone(pin)pin: The digital pin number to setNoneStops the generation of a square wave
unsigned long pulseInLong(pin, value, unsigned long timeout)pin: The digital pin number to set
value: Type of pulse to read(HIGH or LOW)
timeout: Timeout time(us)
The length of the pulse(us)Reads the pulse
void shiftOut(dataPin, clockPin, bitOrder, value)dataPin: The digital pin number to output data
clockPin: The digital pin number to output clock
bitOrder: Which order to shift out the Bits(MSBFIRST or LSBFIRST)
value: 1Byte data to shift out
NoneShift out 1Byte data
byte incoming = shiftIn(dataPin, clockPin, bitOrder)dataPin: The digital pin number to input data
clockPin: The digital pin number to output clock
bitOrder: Which order to shift in the Bits(MSBFIRST or LSBFIRST)
value: 1Byte data to shift out
Input data(1Byte)Shifts in 1Byte data 1Bit at a time

Time

FunctionsParametersReturnsExplanation
void delay(unsigned long ms)ms: Pause time(ms)NonePauses the program for the amount of time(ms)
void delayMicroseconds(unsigned int us)us: Pause time(us)NonePauses the program for the amount of time(us)
unsigned long millis(void)NoneNumber of milliseconds(ms) since the program startedReturns the number of milliseconds(ms) since running the program
unsigned long micros(void)NoneNumber of microseconds(us) since the program startedReturns the number of microseconds(us) since running the program

Math

FunctionsParametersReturnsExplanation
min(x, y)x: The first number
y: The second number
The smaller of the two numbersCalculates the minimum of two numbers
max(x, y)x: The first number
y: The second number
The bigger of the two numbersCalculates the maximum of two numbers
abs(x)x: The numberThe absolute valueCalculates the absolute value of the number
constrain(x, a, b)x: The number to constrain
a: Minimum value of the range
b: Maximum value of the range
The value in the rangeReturns the number in the range
(Minimum or maximum value of the range if outside the range)
long map(long value, long fromLow, long fromHigh, long toLow, long toHigh)value: The number to map
fromLow: The lower value of the current range
fromHigh: The upper value of the current range
toLow: The lower value of the target range
toHigh: The upper value of the target range
The mapped valueRe-maps the number from one range to another
double pow(float base, float exponent)base: The base number
exponent: The multiplier number
The value of the power multiplied byCalculates the value of the number raised to the power
double sqrt(x)x: The numberThe value of the square rootCalculates the square root of the number
sq(x)x: The numberThe value of the square for the numberCalculates the square of the number

Trigonometry

FunctionsParametersReturnsExplanation
double cos(float rad)rad: The angle in radians(rad)The cos of the angleCalculates the cosine of the angle
double sin(float rad)rad: The angle in radians(rad)The sin of the angleCalculates the sine of the angle
double tan(float rad)rad: The angle in radians(rad)The tan of the angleCalculates the tangent of the angle

Random Numbers

FunctionsParametersReturnsExplanation
void randomSeed(unsigned int seed)seed:
The number to initialize the pseudo-random sequence
NoneInitializes the pseudo-random number generator
long random(long max)
long random(long min, long max)
min: The lower value of the random value
max: The upper value of the random value
The random valueGenerates pseudo-random numbers

Bits and Bytes

FunctionsParametersReturnsExplanation
uint8_t lowByte(x)x: The value of any typeThe low-order ByteExtracts the low-order Byte
uint8_t highByte(x)x: The value of any typeThe high-order ByteExtracts the high-order Byte
boolean bitRead(x, n)x: The value to read
n: Which Bit to read
The value of the BitReads the Bit of the number
void bitWrite(x, n, b)x: The numeric variable to write
n: The Bit of the number to write
b: The value to write(0 or 1)
NoneWrites the Bit of the numeric variable
void bitSet(x, n)x: The numeric variable whose Bit to set
n: The Bit to set for 1
NoneSets the Bit of the numeric variable for 1
void bitClear(x, n)x: The numeric variable whose Bit to clear
n: The Bit to set for 0
NoneClears the Bit of the numeric variable for 0
bit(n)n: The Bit whose value to computeThe value of the Bit set to 1Computes the value of the specified Bit set to 1

Interrupts

FunctionsParametersReturnsExplanation
interrupts()NoneNoneEnable interrupts
noInterrupts()NoneNoneDisable interrupts

External Interrupts

FunctionsParametersReturnsExplanation
void attachInterrupt(digitalPinToInterrupt(pin), ISR, int mode)digitalPinToInterrupt(pin): The number of the interrupt
ISR: Function at interrupt
mode: Interrupt timing
NoneInterrupts by pin status
void detachInterrupt(digitalPinToInterrupt(pin))digitalPinToInterrupt(pin): The number of the interrupt to disableNoneTurns off the given interrupt

Communication/Serial

FunctionsParametersReturnsExplanation
Serial.begin(unsigned long speed)speed: Communication speedNoneInitializes serial communication
end()NoneNoneEnds serial communication
int Serial.available()NoneThe number of bytes available to readGets the number of bytes available for reading
int Serial.read()None1Byte serial dataReads 1Byte serial data
int Serial.peek()None1Byte serial dataReads 1Byte serial data without removing it from the buffer
Serial.flush()NoneNoneWaits for the transmission of the serial data to complete
Serial.write(data)
Serial.write(buf, len)
data: The value to send
buf: The array to send
len: The number of bytes
Bytes of data to be sentData transmission
Serial.print(val, format)val: The value to print
format: Base or Significant Digits
Bytes of data to be sentSend string (no line feed)
Serial.println(val, format)val: The value to print
format: Base or Significant Digits
Bytes of data to be sentSend string (with line feed)

Standard Libraries

The Arduino IDE and Arduino Web Editor provide Arduino standard libraries.

The following link is to the reference page on the official Arduino Web site. This article summarizes the Arduino standard libraries with reference to these information.

EEPROM

FunctionsParametersReturnsExplanation
byte Read(int address)int address: Address1Byte dataReads 1Byte data from the specified address in EEPROM
void Write(int address, byte value)int address: Address
byte value: 1Byte data
NoneWrites 1Byte data at the specified address in EEPROM
int length(void)NoneEEPROM memory sizeGets the EEPROM memory size
void Get(int address, T & data)int address: Address
T &data: Data over 2Bytes
NoneGets 2 or more bytes of data from the specified address in EEPROM
void Put(int address, const T & data)int address: Address
T &data: Data over 2Bytes
NoneWrites 2 or more bytes of data at the specified address in EEPROM
void Update(int address, byte value)int address: Address
byte value: 1Byte data
NoneWrites 1Byte data at the specified address in EEPROM; if the original data and the write data are the same, the data is not written.
byte EEPROM[int address]int address: Address1Byte dataReads or Writes 1Byte data from the specified address in EEPROM,

Servo

FunctionsParametersReturnsExplanation
attach(int pin, int min, int max)int pin: The pin number to attach
int min: Pulse width(usec) at angle 0°, Defaults to 544
int max: Pulse width(usec) at angle 180°, Defaults to 2400
Index valueAttach the Servo variable to the pin
bool attached(void)NoneAttached: true
Not attached: false
Check whether the Servo variable is attached to the pin
void detach(void)NoneNoneDetach the Servo variable from the pin
void write(int angle)int angle: Specified angleNoneRotate servo motor to specified angle
void writeMicroseconds(int uS)int uS: Pulse width in usecNoneRotate servo motor by specifying pulse width
int read(void)NoneServo motor angleReads the current servo motor angle

Wire(I2C/TWI)

FunctionsParametersReturnsExplanation
void begin(int address)int address: Slave addressNoneI2C initialization
requestFrom(int address, int quantity, int stop)int address: Address for I2C device
int quantity: The number of bytes to request
int stop: "True(1)" will send a stop message after the request
"False(0)" will continually send a restart after the request, keeping the connection active
Number of bytes receivedRequests data from other I2C devices
void beginTransmission(int address)int address: Address for I2C deviceNoneStarts transmitting data to I2C slave
endTransmission(int stop)int stop: "True(1)" will send a stop message
"False(0)" will send a restart, keeping the connection active
Data Transmission ResultEnds of data transmission with I2C slave device
write(value)
write(string)
write(data, length)
value: 1Byte data
string: String data
data: Array data
length: The number of bytes to transmit
Number of bytes sentTransmit data to I2C slave device
int available(void)NoneNumber of readable bytesCheck the number of bytes available for reading
int read(void)NoneNumber of bytes receivedCheck the number of bytes received

SPI

FunctionsParametersReturnsExplanation
void begin(void)NoneNoneInitializes the SPI bus
void end(void)NoneNoneDisables the SPI bus
void beginTransaction(mySettings)mySettings: SPI speed, bit order, data modeNoneInitializes the SPI bus
(Using SPISettings)
void endTransaction(void)NoneNoneEnds SPI bus
void setBitOrder(order)order: LSBFIRST or MSBFIRSTNoneSets SPI bit order
void setClockDivider(divider)divider:
SPI_CLOCK_DIV2
SPI_CLOCK_DIV4
SPI_CLOCK_DIV8
SPI_CLOCK_DIV16
SPI_CLOCK_DIV32
SPI_CLOCK_DIV64
SPI_CLOCK_DIV128
NoneSets SPI clock divider
void setDataMode(mode)mode:
SPI_MODE0
SPI_MODE1
SPI_MODE2
SPI_MODE3
NoneSets SPI transfer mode
int transfer(value)value: 1Byte dataNumber of bytes receivedTransmits and Receives the data with SPI devices
Let's share this post !
TOC