Ardunio Language Reference
Ardunio Language Reference
[Digital I/O]
Description
Reads the value from a specified digital pin, either HIGH or LOW .
Syntax
digitalRead(pin)
Parameters
Returns
Example Code
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
The analog input pins can be used as digital pins, referred to as A0,
A1, etc. The exception is the Arduino Nano, Pro Mini, and Mini’s A6
and A7 pins, which can only be used as analog inputs.
digitalWrite()
[Digital I/O]
Description
If the pin has been configured as an OUTPUT with pinMode() , its
voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V
boards) for HIGH , 0V (ground) for LOW .
If you do not set the pinMode() to OUTPUT , and connect an LED to a
pin, when calling digitalWrite(HIGH) , the LED may appear dim. Without
explicitly setting pinMode() , digitalWrite() will have enabled the internal
pull-up resistor, which acts like a large current-limiting resistor.
Syntax
digitalWrite(pin, value)
Parameters
Nothing
Example Code
The code makes the digital pin 13 an OUTPUT and toggles it by
alternating between HIGH and LOW at one second pace.
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
The analog input pins can be used as digital pins, referred to as A0,
A1, etc. The exception is the Arduino Nano, Pro Mini, and Mini’s A6
and A7 pins, which can only be used as analog inputs.
pinMode()
[Digital I/O]
Description
pinMode(pin, mode)
Parameters
Returns
Nothing
Example Code
The code makes the digital pin 13 OUTPUT and Toggles
it HIGH and LOW
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
The analog input pins can be used as digital pins, referred to as A0,
A1, etc.
analogRead()
[Analog I/O]
Description
Reads the value from the specified analog pin. Arduino boards
contain a multichannel, 10-bit analog to digital converter. This means
that it will map input voltages between 0 and the operating
voltage(5V or 3.3V) into integer values between 0 and 1023. On an
Arduino UNO, for example, this yields a resolution between readings
of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit. See the
table below for the usable pins, operating voltage and maximum
resolution for some Arduino boards.
Syntax
analogRead(pin)
Parameters
pin : the name of the analog input pin to read from (A0 to A5 on most
boards, A0 to A6 on MKR boards, A0 to A7 on the Mini and Nano, A0
to A15 on the Mega).
Returns
Example Code
void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
analogReference()
[Analog I/O]
Description
Configures the reference voltage used for analog input (i.e. the value
used as the top of the input range). The options are:
Syntax
analogReference(type)
Parameters
Returns
Nothing
analogWrite()
[Analog I/O]
Description
(19)
0 - 8, 10,
MKR1000
11, A3 (18), 732 Hz
WiFi *
A4 (19)
3 - 13, A0
Zero * (14), A1 732 Hz
(15)
You do not need to call pinMode() to set the pin as an output before
calling analogWrite() . The analogWrite function has nothing to do with
the analog pins or the analogRead function.
Syntax
analogWrite(pin, value)
Parameters
pin : the Arduino pin to write to. Allowed data types: int .
value : the duty cycle: between 0 (always off) and 255 (always on).
Allowed data types: int .
Returns
Nothing
Example Code
Sets the output to the LED proportional to the value read from the
potentiometer.
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite
values from 0 to 255
}
analogReadResolution()
[Zero, Due & MKR Family]
Description
Sets the size (in bits) of the value returned by analogRead() . It
defaults to 10 bits (returns values between 0-1023) for backward
compatibility with AVR based boards.
The Due, Zero and MKR Family boards have 12-bit ADC capabilities
that can be accessed by changing the resolution to 12. This will
return values from analogRead() between 0 and 4095.
Syntax
analogReadResolution(bits)
Parameters
Returns
Nothing
Example Code
void setup() {
// open a serial connection
Serial.begin(9600);
}
void loop() {
// read the input on A0 at default resolution (10 bits)
// and send it out the serial connection
analogReadResolution(10);
Serial.print("ADC 10-bit (default) : ");
Serial.print(analogRead(A0));
For example: using the Due with analogReadResolution(16) will give you
an approximated 16-bit number with the first 12 bits containing the
real ADC reading and the last 4 bits padded with zeros.
analogWriteResolution()
[Zero, Due & MKR Family]
Description
By setting the write resolution to 12, you can use analogWrite() with
values between 0 and 4095 to exploit the full DAC resolution or to set
the PWM signal without rolling over.
By setting the write resolution to 10, you can use analogWrite() with
values between 0 and 1023 to exploit the full DAC resolution
Syntax
analogWriteResolution(bits)
Parameters
Returns
Nothing
Example Code
Explain Code
void setup() {
// open a serial connection
Serial.begin(9600);
// make our digital pin an output
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
// read the input on A0 and map it to a PWM pin
// with an attached LED
int sensorVal = analogRead(A0);
Serial.print("Analog Read) : ");
Serial.print(sensorVal);
delay(5);
}
Notes and Warnings
noTone()
[Advanced I/O]
Description
Syntax
noTone(pin)
Parameters
Returns
Nothing
Notes and Warnings
pulseIn()
[Advanced I/O]
Description
The timing of this function has been determined empirically and will
probably show errors in longer pulses. Works on pulses from 10
microseconds to 3 minutes in length.
Syntax
pulseIn(pin, value)
pulseIn(pin, value, timeout)
Parameters
pin : the number of the Arduino pin on which you want to read the
pulse. Allowed data types: int .
value : type of pulse to read: either HIGH or LOW . Allowed data
types: int .
timeout (optional): the number of microseconds to wait for the pulse
to start; default is one second. Allowed data types: unsigned long .
Returns
The length of the pulse (in microseconds) or 0 if no pulse started
before the timeout. Data type: unsigned long .
Example Code
int pin = 7;
unsigned long duration;
void setup() {
Serial.begin(9600);
pinMode(pin, INPUT);
}
void loop() {
duration = pulseIn(pin, HIGH);
Serial.println(duration);
}
pulseInLong()
[Advanced I/O]
Description
The timing of this function has been determined empirically and will
probably show errors in shorter pulses. Works on pulses from 10
microseconds to 3 minutes in length. This routine can be used only if
interrupts are activated. Furthermore the highest resolution is
obtained with large intervals.
Syntax
pulseInLong(pin, value)
pulseInLong(pin, value, timeout)
Parameters
pin : the number of the Arduino pin on which you want to read the
pulse. Allowed data types: int .
value : type of pulse to read: either HIGH or LOW . Allowed data
types: int .
timeout (optional): the number of microseconds to wait for the pulse
to start; default is one second. Allowed data types: unsigned long .
Returns
Example Code
int pin = 7;
unsigned long duration;
void setup() {
Serial.begin(9600);
pinMode(pin, INPUT);
}
void loop() {
duration = pulseInLong(pin, HIGH);
Serial.println(duration);
}
Description
Shifts in a byte of data one bit at a time. Starts from either the most
(i.e. the leftmost) or least (rightmost) significant bit. For each bit,
the clock pin is pulled high, the next bit is read from the data line,
and then the clock pin is taken low.
Syntax
Parameters
dataPin : the pin on which to input each bit. Allowed data types: int .
clockPin : the pin to toggle to signal a read from dataPin.
bitOrder : which order to shift in the bits;
either MSBFIRST or LSBFIRST. (Most Significant Bit First, or, Least
Significant Bit First).
Returns
Description
Shifts out a byte of data one bit at a time. Starts from either the
most (i.e. the leftmost) or least (rightmost) significant bit. Each bit is
written in turn to a data pin, after which a clock pin is pulsed (taken
high, then low) to indicate that the bit is available.
Syntax
Parameters
dataPin : the pin on which to output each bit. Allowed data types: int .
clockPin : the pin to toggle once the dataPin has been set to the
correct value. Allowed data types: int .
bitOrder : which order to shift out the bits; either MSBFIRST or
LSBFIRST. (Most Significant Bit First, or, Least Significant Bit First).
value : the data to shift out. Allowed data types: byte .
Returns
Nothing
Example Code
For accompanying circuit, see the tutorial on controlling a 74HC595
shift register .
//**************************************************************//
// Name : shiftOutCode, Hello World //
// Author : Carlyn Maw,Tom Igoe //
// Date : 25 Oct, 2006 //
// Version : 1.0 //
// Notes : Code for using a 74HC595 Shift Register //
// : to count from 0 to 255 //
//****************************************************************
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
//count up routine
for (int j = 0; j < 256; j++) {
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, j);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, HIGH);
delay(1000);
}
}
tone()
[Advanced I/O]
Description
Use of the tone() function will interfere with PWM output on pins 3
and 11 (on boards other than the Mega).
Syntax
tone(pin, frequency)
tone(pin, frequency, duration)
Parameters
Returns
Nothing