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 Types | Range | Bits | Explanation |
---|---|---|---|
boolean | 0, 1 | 8bit | Used to determine "True" or "False" |
char | -128 to 127 | 8bit | Used for characters |
unsigned char | 0 to 255 | 8bit | 〃 |
int | -32,768 to 32,767 (Arduino Due: -2,147,483,648 to 2,147,483,647) | 16bit (32bit) | Used for integers |
unsigned int | 0 to 65,535 (Arduino Due: 0 to 4,294,967,295) | 16bit (32bit) | 〃 |
long | -2,147,483,648 to 2,147,483,647 | 32bit | 〃 |
unsigned long | 0 to 4,294,967,295 | 32bit | 〃 |
float | 3.4028235E+38 to -3.4028235E+38 | 32bit | Used for decimals |
Operators
Arithmetic Operators
Arithmetic Operators | Examples | Explanation |
---|---|---|
+ | a = b + c | Assign "b + c" into 'a' |
- | a = b - c | Assign "b - c" into 'a' |
* | a = b * c | Assign "b * c" into 'a' |
/ | a = b / c | Assign "b - c" into 'a' |
% | a = b % c | Assign "b % c" into 'a' %: remainder |
= | a = b | Assign 'b' into 'a' |
Compound Operators
Compound Operators | Examples | Explanation |
---|---|---|
+= | a += b | Assign "b + c" into 'a' (equivalent to the expression "a = b + c") |
-= | a -= b | Assign "b - c" into 'a' (equivalent to the expression "a = b - c") |
*= | a *= b | Assign "b * c" into 'a' (equivalent to the expression "a = b * c") |
/= | a /= b | Assign "b - c" into 'a' (equivalent to the expression "a = b / c") |
%= | a %= b | Assign "b % c" into 'a' (equivalent to the expression "a = b % c") |
Increment/Decrement Operators
Increment/Decrement Operators | Examples | Explanation |
---|---|---|
++ | a++ | returns the old value of 'a' and increment 'a' by one |
++a | increment 'a' by one and returns the new value of 'a' | |
-- | a-- | returns the old value of 'a' and decrement 'a' by one |
--a | decrement 'a' by one and returns the new value of 'a' |
Comparison Operators
Comparison Operators | Examples | Explanation |
---|---|---|
== | a == b | True if 'a' and 'b' are equal False if 'a' and 'b' are not equal |
!= | a != b | True if 'a' and 'b' are not equal False if 'a' and 'b' are equal |
< | a < b | True if 'a' is smaller than 'b' False if 'a' is equal or bigger than 'b' |
> | a > b | True if 'a' is bigger than 'b' False if 'a' is equal or smaller than 'b' |
<= | a <= b | True if 'a' is smaller than or equal to 'b' False if 'a' is bigger than 'b' |
>= | a >= b | True if 'a' is bigger than or equal to 'b' False if 'a' is smaller than 'b' |
Boolean Operators
Boolean Operators | Examples | Explanation |
---|---|---|
&& | (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
Functions | Parameters | Returns | Explanation |
---|---|---|---|
void pinMode(pin, mode) | pin: The digital pin number to set mode: INPUT, OUTPUT, or INPUT_PULLUP | None | Configures the pin to be input, output, or pullup |
void digitalWrite(pin, value) | pin: The digital pin number to set value: HIGH or LOW | None | Write HIGH or LOW to the digital pin |
int digitalRead(pin) | pin: The digital pin number to set | HIGH or LOW | Reads HIGH or LOW from the digital pin |
Arduino-Digital I/O Function
This article details the use of Arduino's Digital I/O functions. The Digital I/O functions allow the Arduino board to output and input digital signals. For o...
Analog I/O
Functions | Parameters | Returns | Explanation |
---|---|---|---|
void analogReference(type) | type: Reference voltage setting | None | Configures the reference voltage for analog input |
int analogRead(pin) | pin: The analog pin number to set | 10bits: 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) | None | Writes an analog value (PWM wave) to the digital pin |
Arduino-Analog I/O Function
This article details the use of Arduino's Analog I/O functions. Analog I/O functions allow the Arduino board to output or input analog signals. For other Ard...
Advanced I/O
Functions | Parameters | Returns | Explanation |
---|---|---|---|
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) | None | Generates a square wave |
void noTone(pin) | pin: The digital pin number to set | None | Stops 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 | None | Shift 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
Functions | Parameters | Returns | Explanation |
---|---|---|---|
void delay(unsigned long ms) | ms: Pause time(ms) | None | Pauses the program for the amount of time(ms) |
void delayMicroseconds(unsigned int us) | us: Pause time(us) | None | Pauses the program for the amount of time(us) |
unsigned long millis(void) | None | Number of milliseconds(ms) since the program started | Returns the number of milliseconds(ms) since running the program |
unsigned long micros(void) | None | Number of microseconds(us) since the program started | Returns the number of microseconds(us) since running the program |
Arduino-Time Function
This article details the use of the Arduino Time function. The Time function can be used to pause the program for the amount of time or to measure the time s...
Math
Functions | Parameters | Returns | Explanation |
---|---|---|---|
min(x, y) | x: The first number y: The second number | The smaller of the two numbers | Calculates the minimum of two numbers |
max(x, y) | x: The first number y: The second number | The bigger of the two numbers | Calculates the maximum of two numbers |
abs(x) | x: The number | The absolute value | Calculates 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 range | Returns 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 value | Re-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 by | Calculates the value of the number raised to the power |
double sqrt(x) | x: The number | The value of the square root | Calculates the square root of the number |
sq(x) | x: The number | The value of the square for the number | Calculates the square of the number |
Arduino-Math Function
This article details the use of the Arduino Math function. The Math function can be used to compare numbers, calculate absolute values, powers of magnitude, ...
Trigonometry
Functions | Parameters | Returns | Explanation |
---|---|---|---|
double cos(float rad) | rad: The angle in radians(rad) | The cos of the angle | Calculates the cosine of the angle |
double sin(float rad) | rad: The angle in radians(rad) | The sin of the angle | Calculates the sine of the angle |
double tan(float rad) | rad: The angle in radians(rad) | The tan of the angle | Calculates the tangent of the angle |
Arduino-Trigonometry Function
This article details the use of Arduino's Trigonometry function. The Trigonometry function can be used to calculate cos, sin, and tan. For other Arduino func...
Random Numbers
Functions | Parameters | Returns | Explanation |
---|---|---|---|
void randomSeed(unsigned int seed) | seed: The number to initialize the pseudo-random sequence | None | Initializes 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 value | Generates pseudo-random numbers |
Arduino-Random Numbers Function
This article details the use of the Arduino's Random Numbers function. The Random Numbers function can be used to create a random number sequence and obtain ...
Bits and Bytes
Functions | Parameters | Returns | Explanation |
---|---|---|---|
uint8_t lowByte(x) | x: The value of any type | The low-order Byte | Extracts the low-order Byte |
uint8_t highByte(x) | x: The value of any type | The high-order Byte | Extracts the high-order Byte |
boolean bitRead(x, n) | x: The value to read n: Which Bit to read | The value of the Bit | Reads 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) | None | Writes 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 | None | Sets 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 | None | Clears the Bit of the numeric variable for 0 |
bit(n) | n: The Bit whose value to compute | The value of the Bit set to 1 | Computes the value of the specified Bit set to 1 |
Arduino-Bits and Bytes Function
This article details the use of the Arduino's Bits and Bytes function. The Bits and Bytes function can be used to manipulate bits and bytes. For other Arduin...
Interrupts
Functions | Parameters | Returns | Explanation |
---|---|---|---|
interrupts() | None | None | Enable interrupts |
noInterrupts() | None | None | Disable interrupts |
External Interrupts
Functions | Parameters | Returns | Explanation |
---|---|---|---|
void attachInterrupt(digitalPinToInterrupt(pin), ISR, int mode) | digitalPinToInterrupt(pin): The number of the interrupt ISR: Function at interrupt mode: Interrupt timing | None | Interrupts by pin status |
void detachInterrupt(digitalPinToInterrupt(pin)) | digitalPinToInterrupt(pin): The number of the interrupt to disable | None | Turns off the given interrupt |
Communication/Serial
Functions | Parameters | Returns | Explanation |
---|---|---|---|
Serial.begin(unsigned long speed) | speed: Communication speed | None | Initializes serial communication |
end() | None | None | Ends serial communication |
int Serial.available() | None | The number of bytes available to read | Gets the number of bytes available for reading |
int Serial.read() | None | 1Byte serial data | Reads 1Byte serial data |
int Serial.peek() | None | 1Byte serial data | Reads 1Byte serial data without removing it from the buffer |
Serial.flush() | None | None | Waits 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 sent | Data transmission |
Serial.print(val, format) | val: The value to print format: Base or Significant Digits | Bytes of data to be sent | Send string (no line feed) |
Serial.println(val, format) | val: The value to print format: Base or Significant Digits | Bytes of data to be sent | Send string (with line feed) |
Arduino-Communication Function
This article details the use of the Arduino's Communication function. Arduino provides standard serial communication functions for easy serial communication....
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
Functions | Parameters | Returns | Explanation |
---|---|---|---|
byte Read(int address) | int address: Address | 1Byte data | Reads 1Byte data from the specified address in EEPROM |
void Write(int address, byte value) | int address: Address byte value: 1Byte data | None | Writes 1Byte data at the specified address in EEPROM |
int length(void) | None | EEPROM memory size | Gets the EEPROM memory size |
void Get(int address, T & data) | int address: Address T &data: Data over 2Bytes | None | Gets 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 | None | Writes 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 | None | Writes 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: Address | 1Byte data | Reads or Writes 1Byte data from the specified address in EEPROM, |
Arduino-EEPROM Library
This article details the use of the Arduino EEPROM library. The Arduino board is equipped with an EEPROM, a non-volatile memory that can retain data even aft...
Servo
Functions | Parameters | Returns | Explanation |
---|---|---|---|
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 value | Attach the Servo variable to the pin |
bool attached(void) | None | Attached: true Not attached: false | Check whether the Servo variable is attached to the pin |
void detach(void) | None | None | Detach the Servo variable from the pin |
void write(int angle) | int angle: Specified angle | None | Rotate servo motor to specified angle |
void writeMicroseconds(int uS) | int uS: Pulse width in usec | None | Rotate servo motor by specifying pulse width |
int read(void) | None | Servo motor angle | Reads the current servo motor angle |
Arduino-Servo Library
This article details the use of the Servo library in Arduino. The servo motor is operated by a program using "Servo", a library for servo motor control. For ...
Wire(I2C/TWI)
Functions | Parameters | Returns | Explanation |
---|---|---|---|
void begin(int address) | int address: Slave address | None | I2C 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 received | Requests data from other I2C devices |
void beginTransmission(int address) | int address: Address for I2C device | None | Starts 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 Result | Ends 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 sent | Transmit data to I2C slave device |
int available(void) | None | Number of readable bytes | Check the number of bytes available for reading |
int read(void) | None | Number of bytes received | Check the number of bytes received |
SPI
Functions | Parameters | Returns | Explanation |
---|---|---|---|
void begin(void) | None | None | Initializes the SPI bus |
void end(void) | None | None | Disables the SPI bus |
void beginTransaction(mySettings) | mySettings: SPI speed, bit order, data mode | None | Initializes the SPI bus (Using SPISettings) |
void endTransaction(void) | None | None | Ends SPI bus |
void setBitOrder(order) | order: LSBFIRST or MSBFIRST | None | Sets 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 | None | Sets SPI clock divider |
void setDataMode(mode) | mode: SPI_MODE0 SPI_MODE1 SPI_MODE2 SPI_MODE3 | None | Sets SPI transfer mode |
int transfer(value) | value: 1Byte data | Number of bytes received | Transmits and Receives the data with SPI devices |