Arduino Code Reference

Complete reference for Arduino programming language, functions, and libraries.

Digital I/O

pinMode(), digitalRead(), digitalWrite()

Analog I/O

analogRead(), analogWrite(), analogReference()

Time Functions

millis(), micros(), delay(), delayMicroseconds()

Serial Communication

Serial.begin(), Serial.print(), Serial.read()

Advanced I/O

shiftOut(), pulseIn(), tone(), noTone()

WiFi & IoT

WiFi library, ESP8266, ESP32 functions

I2C & SPI

Wire library, SPI communication protocols

Libraries

Built-in and contributed libraries

Most Popular Functions

pinMode()

Digital I/O

Configures the specified pin to behave either as an input or an output.

Syntax

pinMode(pin, mode)

Parameters

  • pin: the Arduino pin number
  • mode: INPUT, OUTPUT, or INPUT_PULLUP

Example

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);  // sets the digital pin 13 as output
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turns the LED on
  delay(1000);                       // waits for 1 second
  digitalWrite(LED_BUILTIN, LOW);   // turns the LED off
  delay(1000);                       // waits for 1 second
}

digitalWrite()

Digital I/O

Write a HIGH or LOW value to a digital pin.

Syntax

digitalWrite(pin, value)

Parameters

  • pin: the Arduino pin number
  • value: HIGH or LOW

Example

int ledPin = 13;    // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT);  // sets the digital pin as output
}

void loop() {
  digitalWrite(ledPin, HIGH);  // sets the LED on
  delay(1000);                 // waits for a second
  digitalWrite(ledPin, LOW);   // sets the LED off
  delay(1000);                 // waits for a second
}

analogRead()

Analog I/O

Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter.

Syntax

analogRead(pin)

Parameters

  • pin: the analog input pin number

Example

int analogPin = A3;  // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;         // variable to store the value read

void setup() {
  Serial.begin(9600);          //  setup serial
}

void loop() {
  val = analogRead(analogPin); // read the input pin
  Serial.println(val);         // debug value
}