If you have your pin configured as an INPUT, and are reading a switch, when the switch is in the open state the input pin will be "floating", resulting in unpredictable results. In order to assure a proper reading when the switch is open, a pull-up or pull-down resistor must be used. The purpose of this resistor is to pull the pin to a known state when the switch is open. A 10 K ohm resistor is usually chosen, as it is a low enough value to reliably prevent a floating input, and at the same time a high enough value to not draw too much current when the switch is closed. See the Digital Read Serial tutorial for more information.
If a pull-down resistor is used, the input pin will be LOW when the switch is open and HIGH when the switch is closed.
If a pull-up resistor is used, the input pin will be HIGH when the switch is open and LOW when the switch is closed.


Pins Configured as INPUT_PULLUP
The ATmega microcontroller on the Arduino has internal pull-up resistors (resistors that connect to power internally) that you can access. If you prefer to use these instead of external pull-up resistors, you can use the INPUT_PULLUP argument in pinMode().
See the Input Pullup Serial tutorial for an example of this in use.
Pins configured as inputs with either INPUT or INPUT_PULLUP can be damaged or destroyed if they are connected to voltages below ground (negative voltages) or above the positive power rail (5V or 3V).
Pins Configured as OUTPUT


Pins Configured as OUTPUT
Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that they can provide a substantial amount of current to other circuits. ATmega pins can source (provide current) or sink (absorb current) up to 40 mA (milliamps) of current to other devices/circuits. This makes them useful for powering LEDs because LEDs typically use less than 40 mA. Loads greater than 40 mA (e.g. motors) will require a transistor or other interface circuitry.
Pins configured as outputs can be damaged or destroyed if they are connected to either the ground or positive power rails.
Defining built-ins: LED_BUILTIN


Defining built-ins: LED_BUILTIN
Most Arduino boards have a pin connected to an on-board LED in series with a resistor. The constant LED_BUILTIN is the number of the pin to which the on-board LED is connected. Most boards have this LED connected to digital pin 13.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Variables
            >  Constants
            > Floatingpointconstants

Floating Point Constants
          [Constants]

Description
Similar to integer constants, floating point constants are used to make code more readable. Floating point constants are swapped at compile time for the value to which the expression evaluates.
Example Code


Example Code
float n = 0.005;  // 0.005 is a floating point constant
Notes and Warnings
Floating point constants can also be expressed in a variety of scientific notation. 'E' and 'e' are both accepted as valid exponent indicators.
floating-point constant
evaluates to:
also evaluates to:
10.0
10
2.34E5
2.34 * 10^5
234000
67e-12
67.0 * 10^-12
0.000000000067
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Digital io
            > Digitalread

digitalRead()
          [Digital I/O]

Description
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead(pin)
Parameters
pin: the Arduino pin number you want to read
Returns
HIGH or LOW
Example Code


pin: the Arduino pin number you want to read
Returns
HIGH or LOW
Example Code
Sets pin 13 to the same value as pin 7, declared as an input.
int ledPin = 13;  // LED connected to digital pin 13
int inPin = 7;    // pushbutton connected to digital pin 7
int val = 0;      // variable to store the read value
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
}
Notes and Warnings


}
Notes and Warnings
If the pin isn’t connected to anything, digitalRead() can return either HIGH or LOW (and this can change randomly).
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.
See also
EXAMPLE Description of the digital pins


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Digital io
            > Digitalwrite

digitalWrite()
          [Digital I/O]

Description
Write a HIGH or a LOW value to a digital pin.


Description
Write a HIGH or a LOW value to a digital pin.
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 the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. See the Digital Pins tutorial for more information.
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


Syntax
digitalWrite(pin, value)
Parameters
pin: the Arduino pin number.
value: HIGH or LOW.
Returns
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
}
Notes and Warnings


delay(1000);            // waits for a second
}
Notes and Warnings
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.
See also
EXAMPLE Description of the digital pins


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Digital io
            > Pinmode

pinMode()
          [Digital I/O]

Description
Configures the specified pin to behave either as an input or an output. See the Digital Pins page for details on the functionality of the pins.


As of Arduino 1.0.1, it is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pullups.
Syntax
pinMode(pin, mode)
Parameters
pin: the Arduino pin number to set the mode of.
mode: INPUT, OUTPUT, or INPUT_PULLUP. See the Digital Pins page for a more complete description of the functionality.
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


digitalWrite(13, LOW);  // sets the digital pin 13 off
  delay(1000);            // waits for a second
}
Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc.
See also
EXAMPLE Description of the digital pins


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Bits and bytes
            > Highbyte

highByte()
          [Bits and Bytes]

Description
Extracts the high-order (leftmost) byte of a word (or the second lowest byte of a larger data type).
Syntax
highByte(x)
Parameters
x: a value of any type
Returns
Data type: byte.


Parameters
x: a value of any type
Returns
Data type: byte.
See also
LANGUAGE word()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Bits and bytes
            > Bitread

bitRead()
          [Bits and Bytes]

Description
Reads a bit of a number.
Syntax
bitRead(x, n)
Parameters
x: the number from which to read.
n: which bit to read, starting at 0 for the least-significant (rightmost) bit.


Returns
The value of the bit (0 or 1).
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Bits and bytes
            > Bitwrite

bitWrite()
          [Bits and Bytes]

Description
Writes a bit of a numeric variable.
Syntax
bitWrite(x, n, b)
Parameters
x: the numeric variable to which to write.


Parameters
x: the numeric variable to which to write.
n: which bit of the number to write, starting at 0 for the least-significant (rightmost) bit.
b: the value to write to the bit (0 or 1).
Returns
Nothing
Example Code
Demonstrates the use of bitWrite by printing the value of a variable to the Serial Monitor before and after the use of bitWrite().
void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // wait for serial port to connect. Needed for native USB port only
  byte x = 0b10000000;  // the 0b prefix indicates a binary constant
  Serial.println(x, BIN); // 10000000
  bitWrite(x, 0, 1);  // write 1 to the least significant bit of x
  Serial.println(x, BIN); // 10000001


Serial.println(x, BIN); // 10000001
}
void loop() {}
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Bits and bytes
            > Lowbyte

lowByte()
          [Bits and Bytes]

Description
Extracts the low-order (rightmost) byte of a variable (e.g. a word).
Syntax
lowByte(x)
Parameters
x: a value of any type
Returns
Data type: byte.
See also
LANGUAGE word()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Bits and bytes
            > Bitset

bitSet()
          [Bits and Bytes]

Description
Sets (writes a 1 to) a bit of a numeric variable.
Syntax
bitSet(x, n)
Parameters
x: the numeric variable whose bit to set.


bitSet(x, n)
Parameters
x: the numeric variable whose bit to set.
n: which bit to set, starting at 0 for the least-significant (rightmost) bit.
Returns
Nothing
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Bits and bytes
            > Bitclear

bitClear()
          [Bits and Bytes]

Description
Clears (writes a 0 to) a bit of a numeric variable.
Syntax
bitClear(x, n)
Parameters
x: the numeric variable whose bit to clear.


bitClear(x, n)
Parameters
x: the numeric variable whose bit to clear.
n: which bit to clear, starting at 0 for the least-significant (rightmost) bit.
Returns
x: the value of the numeric variable after the bit at position n is cleared.
Example Code
Prints the output of bitClear(x,n) on two given integers. The binary representation of 6 is 0110, so when n=1, the second bit from the right is set to 0. After this we are left with 0100 in binary, so 4 is returned.
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  int x = 6;
  int n = 1;


}
  int x = 6;
  int n = 1;
  Serial.print(bitClear(x, n)); // print the output of bitClear(x,n)
}
void loop() {
}
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Bits and bytes
            > Bit

bit()
          [Bits and Bytes]

Description
Computes the value of the specified bit (bit 0 is 1, bit 1 is 2, bit 2 is 4, etc.).
Syntax
bit(n)
Parameters
n: the bit whose value to compute
Returns


Syntax
bit(n)
Parameters
n: the bit whose value to compute
Returns
The value of the bit.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Communication
            > Wire

Wire
          [Communication]

Description


> Wire

Wire
          [Communication]

Description
This library allows you to communicate with I2C/TWI devices. On the Arduino boards with the R3 layout (1.0 pinout), the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. The Arduino Due has two I2C/TWI interfaces SDA1 and SCL1 are near to the AREF pin and the additional one is on pins 20 and 21.
As a reference the table below shows where TWI pins are located on various Arduino boards.
Board
I2C/TWI pins
UNO, Ethernet
A4 (SDA), A5 (SCL)
Mega2560
20 (SDA), 21 (SCL)
Leonardo
20 (SDA), 21 (SCL), SDA1, SCL1


20 (SDA), 21 (SCL), SDA1, SCL1
As of Arduino 1.0, the library inherits from the Stream functions, making it consistent with other read/write libraries. Because of this, send() and receive() have been replaced with read() and write().
Recent versions of the Wire library can use timeouts to prevent a lockup in the face of certain problems on the bus, but this is not enabled by default (yet) in current versions. It is recommended to always enable these timeouts when using the Wire library. See the Wire.setWireTimeout function for more details.


Note: There are both 7 and 8-bit versions of I2C addresses. 7 bits identify the device, and the eighth bit determines if it’s being written to or read from. The Wire library uses 7 bit addresses throughout. If you have a datasheet or sample code that uses 8-bit address, you’ll want to drop the low bit (i.e. shift the value one bit to the right), yielding an address between 0 and 127. However the addresses from 0 to 7 are not used because are reserved so the first address that can be used is 8. Please note that a pull-up resistor is needed when connecting SDA/SCL pins. Please refer to the examples for more information. MEGA 2560 board has pull-up resistors on pins 20 and 21 onboard.


The Wire library implementation uses a 32 byte buffer, therefore any communication should be within this limit. Exceeding bytes in a single transmission will just be dropped.
To use this library:
#include <Wire.h>
Functions
begin()
end()
requestFrom()
beginTransmission()
endTransmission()
write()
available()
read()
setClock()
onReceive()
onRequest()
setWireTimeout()
clearWireTimeoutFlag()
getWireTimeoutFlag()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Communication
            > Spi

SPI
          [Communication]

Description
This library allows you to communicate with SPI devices, with the Arduino as the controller device. This library is bundled with every Arduino platform (avr, megaavr, mbed, samd, sam, arc32), so you do not need to install the library separately.
To use this library


To use this library
#include <SPI.h>
To read more about Arduino and SPI, you can visit the Arduino & Serial Peripheral Interface (SPI) guide.
Functions
SPISettings
begin()
beginTransaction()
endTransaction()
end()
setBitOrder()
setClockDivider()
setDataMode()
transfer()
usingInterrupt()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Communication
            > Stream

Stream
          [Communication]

Description
Stream is the base class for character and binary based streams. It is not called directly, but invoked whenever you use a function that relies on it.


Stream defines the reading functions in Arduino. When using any core functionality that uses a read() or similar method, you can safely assume it calls on the Stream class. For functions like print(), Stream inherits from the Print class.
Some of the libraries that rely on Stream include :
Serial
Wire
Ethernet
SD
Functions
available()
read()
flush()
find()
findUntil()
peek()
readBytes()
readBytesUntil()
readString()
readStringUntil()
parseInt()
parseFloat()
setTimeout()
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Communication
            > Serial

Serial
          [Communication]

Description
Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART), and some have several.
Board
USB CDC name
Serial pins
Serial1 pins
Serial2 pins
Serial3 pins


USB CDC name
Serial pins
Serial1 pins
Serial2 pins
Serial3 pins
Uno, Nano, Mini
0(RX), 1(TX)
Mega
0(RX), 1(TX)
19(RX), 18(TX)
17(RX), 16(TX)
15(RX), 14(TX)
Leonardo, Micro, Yún
Serial
0(RX), 1(TX)
Uno WiFi Rev.2
Connected to USB
0(RX), 1(TX)
Connected to NINA
MKR boards
Serial
13(RX), 14(TX)
Zero
SerialUSB (Native USB Port only)
Connected to Programming Port
0(RX), 1(TX)
Due
SerialUSB (Native USB Port only)
0(RX), 1(TX)
19(RX), 18(TX)


0(RX), 1(TX)
19(RX), 18(TX)
17(RX), 16(TX)
15(RX), 14(TX)
101
Serial
0(RX), 1(TX)
On Uno, Nano, Mini, and Mega, pins 0 and 1 are used for communication with the computer. Connecting anything to these pins can interfere with that communication, including causing failed uploads to the board.
You can use the Arduino environment’s built-in serial monitor to communicate with an Arduino board. Click the serial monitor button in the toolbar and select the same baud rate used in the call to begin().
Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board). Don’t connect these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your Arduino board.


To use these extra serial ports to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the Mega’s USB-to-serial adaptor. To use them to communicate with an external TTL serial device, connect the TX pin to your device’s RX pin, the RX to your device’s TX pin, and the ground of your Mega to your device’s ground.
Functions
if(Serial)
available()
availableForWrite()
begin()
end()
find()
findUntil()
flush()
parseFloat()
parseInt()
peek()
print()
println()
read()
readBytes()
readBytesUntil()
readString()
readStringUntil()
setTimeout()
write()
serialEvent()
See also
EXAMPLE ReadASCIIString
EXAMPLE ASCII TAble
EXAMPLE Dimmer
EXAMPLE Graph
EXAMPLE Physical Pixel
EXAMPLE Serial Call Response
EXAMPLE Serial Call Response ASCII


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Usb
            > Keyboard

Keyboard
          [USB]

Description
The keyboard functions enable 32u4 or SAMD micro based boards to send keystrokes to an attached computer through their micro’s native USB port.
Note: Not every possible ASCII character, particularly the non-printing ones, can be sent with the Keyboard library.


The library supports the use of modifier keys. Modifier keys change the behavior of another key when pressed simultaneously. See here for additional information on supported keys and their use.
Notes and Warnings
These core libraries allow the 32u4 and SAMD based boards (Leonardo, Esplora, Zero, Due and MKR Family) to appear as a native Mouse and/or Keyboard to a connected computer.
A word of caution on using the Mouse and Keyboard libraries: if the Mouse or Keyboard library is constantly running, it will be difficult to program your board. Functions such as Mouse.move() and Keyboard.print() will move your cursor or send keystrokes to a connected computer and should only be called when you are ready to handle them. It is recommended to use a control system to turn this functionality on, like a physical switch or only responding to specific input you can control. Refer to the Mouse and Keyboard examples for some ways to handle this.


When using the Mouse or Keyboard library, it may be best to test your output first using Serial.print(). This way, you can be sure you know what values are being reported.
Functions
Keyboard.begin()
Keyboard.end()
Keyboard.press()
Keyboard.print()
Keyboard.println()
Keyboard.release()
Keyboard.releaseAll()
Keyboard.write()
See also
EXAMPLE KeyboardAndMouseControl: Demonstrates the Mouse and Keyboard commands in one program.
EXAMPLE KeyboardMessage: Sends a text string when a button is pressed.
EXAMPLE KeyboardLogout: Logs out the current user with key commands
EXAMPLE KeyboardSerial: Reads a byte from the serial port, and sends back a keystroke.
EXAMPLE KeyboardReprogram: opens a new window in the Arduino IDE and reprograms the board with a simple blink program


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Usb
            > Mouse

Mouse
          [USB]

Description
The mouse functions enable 32u4 or SAMD micro based boards to control cursor movement on a connected computer through their micro’s native USB port. When updating the cursor position, it is always relative to the cursor’s previous location.
Notes and Warnings


Notes and Warnings
These core libraries allow the 32u4 and SAMD based boards (Leonardo, Esplora, Zero, Due and MKR Family) to appear as a native Mouse and/or Keyboard to a connected computer.
A word of caution on using the Mouse and Keyboard libraries: if the Mouse or Keyboard library is constantly running, it will be difficult to program your board. Functions such as Mouse.move() and Keyboard.print() will move your cursor or send keystrokes to a connected computer and should only be called when you are ready to handle them. It is recommended to use a control system to turn this functionality on, like a physical switch or only responding to specific input you can control. Refer to the Mouse and Keyboard examples for some ways to handle this.
When using the Mouse or Keyboard library, it may be best to test your output first using Serial.print(). This way, you can be sure you know what values are being reported.
Functions


Functions
Mouse.begin()
Mouse.click()
Mouse.end()
Mouse.move()
Mouse.press()
Mouse.release()
Mouse.isPressed()
See also
EXAMPLE KeyboardAndMouseControl: Demonstrates the Mouse and Keyboard commands in one program.
EXAMPLE ButtonMouseControl: Control cursor movement with 5 pushbuttons.
EXAMPLE JoystickMouseControl: Controls a computer’s cursor movement with a Joystick when a button is pressed.


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  External interrupts
            > Detachinterrupt

detachInterrupt()
          [External Interrupts]

Description
Turns off the given interrupt.
Syntax
detachInterrupt(digitalPinToInterrupt(pin)) (recommended)
detachInterrupt(interrupt) (not recommended)


detachInterrupt(interrupt) (not recommended)
detachInterrupt(pin) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)
Parameters
interrupt: the number of the interrupt to disable (see attachInterrupt() for more details).
pin: the Arduino pin number of the interrupt to disable
Returns
Nothing
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  External interrupts
            > Attachinterrupt

attachInterrupt()
          [External Interrupts]

Description
Digital Pins With Interrupts


[External Interrupts]

Description
Digital Pins With Interrupts
The first parameter to attachInterrupt() is an interrupt number. Normally you should use digitalPinToInterrupt(pin) to translate the actual digital pin to the specific interrupt number. For example, if you connect to pin 3, use digitalPinToInterrupt(3) as the first parameter to attachInterrupt().
Board
Digital Pins Usable For Interrupts
Uno, Nano, Mini, other 328-based
2, 3
Uno WiFi Rev.2, Nano Every
all digital pins
Mega, Mega2560, MegaADK
2, 3, 18, 19, 20, 21 (pins 20 & 21 are not available to use for interrupts while they are used for I2C communication)
Micro, Leonardo, other 32u4-based
0, 1, 2, 3, 7
Zero


0, 1, 2, 3, 7
Zero
all digital pins, except 4
MKR Family boards
0, 1, 4, 5, 6, 7, 8, 9, A1, A2
Nano 33 IoT
2, 3, 9, 10, 11, 13, A1, A5, A7
Nano 33 BLE, Nano 33 BLE Sense
all pins
Due
all digital pins
101
all digital pins (Only pins 2, 5, 7, 8, 10, 11, 12, 13 work with CHANGE)
Notes and Warnings
Note


Notes and Warnings
Note
Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function. See the section on ISRs below for more information.
Using Interrupts
Interrupts are useful for making things happen automatically in microcontroller programs and can help solve timing problems. Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input.


If you wanted to ensure that a program always caught the pulses from a rotary encoder, so that it never misses a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the input.
About Interrupt Service Routines
ISRs are special kinds of functions that have some unique limitations most other functions do not have. An ISR cannot have any parameters, and they shouldn’t return anything.


Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be executed after the current one finishes in an order that depends on the priority they have. millis() relies on interrupts to count, so it will never increment inside an ISR. Since delay() requires interrupts to work, it will not work if called inside an ISR. micros() works initially but will start behaving erratically after 1-2 ms. delayMicroseconds() does not use any counter, so it will work as normal.
Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.
For more information on interrupts, see Nick Gammon’s notes.
Syntax
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended)


Syntax
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended)
attachInterrupt(interrupt, ISR, mode) (not recommended)
attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)
Parameters
interrupt: the number of the interrupt. Allowed data types: int.
pin: the Arduino pin number.
ISR: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.
mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:
LOW to trigger the interrupt whenever the pin is low,
CHANGE to trigger the interrupt whenever the pin changes value
RISING to trigger when the pin goes from low to high,
FALLING for when the pin goes from high to low.


FALLING for when the pin goes from high to low.
The Due, Zero and MKR1000 boards allow also:
HIGH to trigger the interrupt whenever the pin is high.
Returns
Nothing
Example Code
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
  digitalWrite(ledPin, state);
}
void blink() {
  state = !state;
}
Interrupt Numbers


}
void blink() {
  state = !state;
}
Interrupt Numbers
Normally you should use digitalPinToInterrupt(pin), rather than place an interrupt number directly into your sketch. The specific pins with interrupts and their mapping to interrupt number varies for each type of board. Direct use of interrupt numbers may seem simple, but it can cause compatibility trouble when your sketch runs on a different board.
However, older sketches often have direct interrupt numbers. Often number 0 (for digital pin 2) or number 1 (for digital pin 3) were used. The table below shows the available interrupt pins on various boards.
Note that in the table below, the interrupt numbers refer to the number to be passed to attachInterrupt(). For historical reasons, this numbering does not always correspond directly to the interrupt numbering on the ATmega chip (e.g. int.0 corresponds to INT4 on the ATmega2560 chip).
Board
int.0


Board
int.0
int.1
int.2
int.3
int.4
int.5
Uno, Ethernet
2
3
Mega2560
2
3
21
20
19
18
32u4 based (e.g Leonardo, Micro)
3
2
0
1
7
For Uno WiFiRev.2, Due, Zero, MKR Family and 101 boards the interrupt number = pin number.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Analog io
            > Analogreference

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:
Arduino AVR Boards (Uno, Mega, Leonardo, etc.)


Arduino AVR Boards (Uno, Mega, Leonardo, etc.)
DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
INTERNAL: a built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328P and 2.56 volts on the ATmega32U4 and ATmega8 (not available on the Arduino Mega)
INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)
EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.
Arduino SAMD Boards (Zero, etc.)
AR_DEFAULT: the default analog reference of 3.3V
AR_INTERNAL: a built-in 2.23V reference


AR_INTERNAL: a built-in 2.23V reference
AR_INTERNAL1V0: a built-in 1.0V reference
AR_INTERNAL1V65: a built-in 1.65V reference
AR_INTERNAL2V23: a built-in 2.23V reference
AR_EXTERNAL: the voltage applied to the AREF pin is used as the reference
Arduino megaAVR Boards (Uno WiFi Rev2)
DEFAULT: a built-in 0.55V reference
INTERNAL: a built-in 0.55V reference
VDD: Vdd of the ATmega4809. 5V on the Uno WiFi Rev2
INTERNAL0V55: a built-in 0.55V reference
INTERNAL1V1: a built-in 1.1V reference
INTERNAL1V5: a built-in 1.5V reference
INTERNAL2V5: a built-in 2.5V reference


INTERNAL2V5: a built-in 2.5V reference
INTERNAL4V3: a built-in 4.3V reference
EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference
Arduino SAM Boards (Due)
AR_DEFAULT: the default analog reference of 3.3V. This is the only supported option for the Due.
Arduino Mbed OS Nano Boards (Nano 33 BLE), Arduino Mbed OS Edge Boards (Edge Control)
AR_VDD: the default 3.3 V reference
AR_INTERNAL: built-in 0.6 V reference
AR_INTERNAL1V2: 1.2 V reference (internal 0.6 V reference with 2x gain)
AR_INTERNAL2V4: 2.4 V reference (internal 0.6 V reference with 4x gain)
Syntax
analogReference(type)
Parameters


Syntax
analogReference(type)
Parameters
type: which type of reference to use (see list of options in the description).
Returns
Nothing
Notes and Warnings
After changing the analog reference, the first few readings from analogRead() may not be accurate.
Don’t use anything less than 0V or more than 5V for external reference voltage on the AREF pin! If you’re using an external reference on the AREF pin, you must set the analog reference to EXTERNAL before calling analogRead(). Otherwise, you will short together the active reference voltage (internally generated) and the AREF pin, possibly damaging the microcontroller on your Arduino board.


Alternatively, you can connect the external reference voltage to the AREF pin through a 5K resistor, allowing you to switch between external and internal reference voltages. Note that the resistor will alter the voltage that gets used as the reference because there is an internal 32K resistor on the AREF pin. The two act as a voltage divider, so, for example, 2.5V applied through the resistor will yield 2.5 * 32 / (32 + 5) = ~2.2V at the AREF pin.
See also
EXAMPLE Description of the analog input pins


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Analog io
            > Analogwrite

analogWrite()
          [Analog I/O]

Description


analogWrite()
          [Analog I/O]

Description
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin.
Board
PWM Pins
PWM Frequency
Uno, Nano, Mini
3, 5, 6, 9, 10, 11
490 Hz (pins 5 and 6: 980 Hz)
Mega
2 - 13, 44 - 46
490 Hz (pins 4 and 13: 980 Hz)
Leonardo, Micro, Yún
3, 5, 6, 9, 10, 11, 13


3, 5, 6, 9, 10, 11, 13
490 Hz (pins 3 and 11: 980 Hz)
Uno WiFi Rev2, Nano Every
3, 5, 6, 9, 10
976 Hz
MKR boards *
0 - 8, 10, A3, A4
732 Hz
MKR1000 WiFi *
0 - 8, 10, 11, A3, A4
732 Hz
Zero *
3 - 13, A0, A1
732 Hz
Nano 33 IoT *
2, 3, 5, 6, 9 - 12, A2, A3, A5
732 Hz
Nano 33 BLE/BLE Sense
1 - 13, A0 - A7
500 Hz
Due **
2-13
1000 Hz
101


500 Hz
Due **
2-13
1000 Hz
101
3, 5, 6, 9
pins 3 and 9: 490 Hz, pins 5 and 6: 980 Hz
* In addition to PWM capabilities on the pins noted above, the MKR, Nano 33 IoT, and Zero boards have true analog output when using analogWrite() on the DAC0 (A0) pin.
** In addition to PWM capabilities on the pins noted above, the Due has true analog output when using analogWrite() on pins DAC0 and DAC1.
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.


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.
int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 3;   // potentiometer connected to analog pin 3
int val = 0;         // variable to store the read value
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


}
Notes and Warnings
The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g. 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.
See also
LANGUAGE analogWriteResolution()
DEFINITION PWM
EXAMPLE Blink


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Analog io
            > Analogread

analogRead()
          [Analog I/O]

Description


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.
The input range can be changed using analogReference(), while the resolution can be changed (only for Zero, Due and MKR boards) using analogReadResolution().


On ATmega based boards (UNO, Nano, Mini, Mega), it takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.
Board
Operating voltage
Usable pins
Max resolution
Uno
5 Volts
A0 to A5
10 bits
Mini, Nano
5 Volts
A0 to A7
10 bits
Mega, Mega2560, MegaADK
5 Volts
A0 to A14
10 bits
Micro
5 Volts
A0 to A11*
10 bits
Leonardo
5 Volts
A0 to A11*
10 bits
Zero
3.3 Volts
A0 to A5
12 bits**
Due
3.3 Volts
A0 to A11
12 bits**
MKR Family boards
3.3 Volts


12 bits**
MKR Family boards
3.3 Volts
A0 to A6
12 bits**
*A0 through A5 are labelled on the board, A6 through A11 are respectively available on pins 4, 6, 8, 9, 10, and 12
**The default analogRead() resolution for these boards is 10 bits, for compatibility. You need to use analogReadResolution() to change it to 12 bits.
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


Returns
The analog reading on the pin. Although it is limited to the resolution of the analog to digital converter (0-1023 for 10 bits or 0-4095 for 12 bits). Data type: int.
Example Code
The code reads the voltage on analogPin and displays it.
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
}
Notes and Warnings


Serial.println(val);          // debug value
}
Notes and Warnings
If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).
See also
LANGUAGE analogReadResolution()
EXAMPLE Description of the analog input pins


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Advanced io
            > Tone

tone()
          [Advanced I/O]

Description


> Tone

tone()
          [Advanced I/O]

Description
Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones.
Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.
Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).
It is not possible to generate tones lower than 31Hz. For technical details, see Brett Hagman’s notes.
Syntax
tone(pin, frequency)
tone(pin, frequency, duration)
Parameters


Syntax
tone(pin, frequency)
tone(pin, frequency, duration)
Parameters
pin: the Arduino pin on which to generate the tone.
frequency: the frequency of the tone in hertz. Allowed data types: unsigned int.
duration: the duration of the tone in milliseconds (optional). Allowed data types: unsigned long.
Returns
Nothing
Notes and Warnings
If you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.
See also
LANGUAGE analogWrite()
EXAMPLE Tone Melody
EXAMPLE Pitch Follower
EXAMPLE Tone Keyboard
EXAMPLE Tone Multiple
EXAMPLE PWM


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Advanced io
            > Pulsein

pulseIn()
          [Advanced I/O]

Description


pulseIn()
          [Advanced I/O]

Description
Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout.
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.
Note
if the optional timeout is used code will execute faster.
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
The example prints the time duration of a pulse on pin 7.
int pin = 7;
unsigned long duration;
void setup() {
  Serial.begin(9600);
  pinMode(pin, INPUT);
}
void loop() {
  duration = pulseIn(pin, HIGH);
  Serial.println(duration);
}
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Advanced io
            > Shiftin

shiftIn()
          [Advanced I/O]

Description


shiftIn()
          [Advanced I/O]

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.
If you’re interfacing with a device that’s clocked by rising edges, you’ll need to make sure that the clock pin is low before the first call to shiftIn(), e.g. with a call to digitalWrite(clockPin, LOW).
Note: this is a software implementation; Arduino also provides an SPI library that uses the hardware implementation, which is faster but only works on specific pins.
Syntax
byte incoming = shiftIn(dataPin, clockPin, bitOrder)
Parameters


Syntax
byte incoming = shiftIn(dataPin, clockPin, bitOrder)
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
The value read. Data type: byte.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Advanced io
            > Pulseinlong

pulseInLong()
          [Advanced I/O]

Description
pulseInLong() is an alternative to pulseIn() which is better at handling long pulse and interrupt affected scenarios.


Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseInLong() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout.
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.


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
The example prints the time duration of a pulse on pin 7.
int pin = 7;
unsigned long duration;
void setup() {
  Serial.begin(9600);
  pinMode(pin, INPUT);
}
void loop() {
  duration = pulseInLong(pin, HIGH);
  Serial.println(duration);
}
Notes and Warnings
This function relies on micros() so cannot be used in noInterrupts() context.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Advanced io
            > Shiftout

shiftOut()
          [Advanced I/O]

Description


shiftOut()
          [Advanced I/O]

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.
Note- if you’re interfacing with a device that’s clocked by rising edges, you’ll need to make sure that the clock pin is low before the call to shiftOut(), e.g. with a call to digitalWrite(clockPin, LOW).
This is a software implementation; see also the SPI library, which provides a hardware implementation that is faster but works only on specific pins.
Syntax
shiftOut(dataPin, clockPin, bitOrder, value)
Parameters


Syntax
shiftOut(dataPin, clockPin, bitOrder, value)
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                                               //


//  Version : 1.0                                               //
//  Notes   : Code for using a 74HC595 Shift Register           //
//          : to count from 0 to 255                            //
//****************************************************************
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
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


//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);
  }
}
Notes and Warnings
The dataPin and clockPin must already be configured as outputs by a call to pinMode().
shiftOut is currently written to output 1 byte (8 bits) so it requires a two step operation to output values larger than 255.
// Do this for MSBFIRST serial
int data = 500;
// shift out highbyte
shiftOut(dataPin, clock, MSBFIRST, (data >> 8));
// shift out lowbyte
shiftOut(dataPin, clock, MSBFIRST, data);


// shift out lowbyte
shiftOut(dataPin, clock, MSBFIRST, data);
// Or do this for LSBFIRST serial
data = 500;
// shift out lowbyte
shiftOut(dataPin, clock, LSBFIRST, data);
// shift out highbyte
shiftOut(dataPin, clock, LSBFIRST, (data >> 8));
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Advanced io
            > Notone

noTone()
          [Advanced I/O]

Description
Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.
Syntax
noTone(pin)
Parameters
pin: the Arduino pin on which to stop generating the tone
Returns
Nothing


Parameters
pin: the Arduino pin on which to stop generating the tone
Returns
Nothing
Notes and Warnings
If you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Random numbers
            > Randomseed

randomSeed()
          [Random Numbers]

Description
randomSeed() initializes the pseudo-random number generator, causing it to start at an arbitrary point in its random sequence. This sequence, while very long, and random, is always the same.


If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.
Conversely, it can occasionally be useful to use pseudo-random sequences that repeat exactly. This can be accomplished by calling randomSeed() with a fixed number, before starting the random sequence.
Syntax
randomSeed(seed)
Parameters
seed: non-zero number to initialize the pseudo-random sequence. Allowed data types: unsigned long.
Returns
Nothing
Example Code
The code generates a pseudo-random number and sends the generated number to the serial port.
long randNumber;
void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
}
void loop() {
  randNumber = random(300);
  Serial.println(randNumber);


randNumber = random(300);
  Serial.println(randNumber);
  delay(50);
}
Notes and Warnings
If the seed is 0, randomSeed(seed) will have no effect.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Random numbers
            > Random

random()
          [Random Numbers]

Description
The random function generates pseudo-random numbers.
Syntax
random(max)
random(min, max)
Parameters
min: lower bound of the random value, inclusive (optional).
max: upper bound of the random value, exclusive.
Returns


max: upper bound of the random value, exclusive.
Returns
A random number between min and max-1. Data type: long.
Example Code
The code generates random numbers and displays them.
long randNumber;
void setup() {
  Serial.begin(9600);
  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
}
void loop() {
  // print a random number from 0 to 299
  randNumber = random(300);
  Serial.println(randNumber);
  // print a random number from 10 to 19
  randNumber = random(10, 20);
  Serial.println(randNumber);
  delay(50);


Serial.println(randNumber);
  delay(50);
}
Notes and Warnings
If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.
Conversely, it can occasionally be useful to use pseudo-random sequences that repeat exactly. This can be accomplished by calling randomSeed() with a fixed number, before starting the random sequence.
The max parameter should be chosen according to the data type of the variable in which the value is stored. In any case, the absolute maximum is bound to the long nature of the value generated (32 bit - 2,147,483,647). Setting max to a higher value won’t generate an error during compilation, but during sketch execution the numbers generated will not be as expected.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Ishexadecimaldigit

isHexadecimalDigit()
          [Characters]

Description
Analyse if a char is an hexadecimal digit (A-F, 0-9). Returns true if thisChar contains an hexadecimal digit.
Syntax
isHexadecimalDigit(thisChar)
Parameters
thisChar: variable. Allowed data types: char.


Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is an hexadecimal digit.
Example Code
if (isHexadecimalDigit(myChar)) { // tests if myChar is an hexadecimal digit
  Serial.println("The character is an hexadecimal digit");
}
else {
  Serial.println("The character is not an hexadecimal digit");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Islowercase

isLowerCase()
          [Characters]

Description
Analyse if a char is lower case (that is a letter in lower case). Returns true if thisChar contains a letter in lower case.
Syntax
isLowerCase(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns


Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is lower case.
Example Code
if (isLowerCase(myChar)) {  // tests if myChar is a lower case letter
  Serial.println("The character is lower case");
}
else {
  Serial.println("The character is not lower case");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Ispunct

isPunct()
          [Characters]

Description
Analyse if a char is punctuation (that is a comma, a semicolon, an exclamation mark and so on). Returns true if thisChar is punctuation.
Syntax
isPunct(thisChar)
Parameters


Syntax
isPunct(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is a punctuation.
Example Code
if (isPunct(myChar)) {  // tests if myChar is a punctuation character
  Serial.println("The character is a punctuation");
}
else {
  Serial.println("The character is not a punctuation");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Isuppercase

isUpperCase()
          [Characters]

Description
Analyse if a char is upper case (that is, a letter in upper case). Returns true if thisChar is upper case.
Syntax
isUpperCase(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns


Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is upper case.
Example Code
if (isUpperCase(myChar)) {  // tests if myChar is an upper case letter
  Serial.println("The character is upper case");
}
else {
  Serial.println("The character is not upper case");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Isspace

isSpace()
          [Characters]

Description
Analyse if a char is a white-space character. Returns true if the argument is a space, form feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v').
Syntax
isSpace(thisChar)
Parameters


Syntax
isSpace(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is a white-space character.
Example Code
if (isSpace(myChar)) {  // tests if myChar is a white-space character
  Serial.println("The character is white-space");
}
else {
  Serial.println("The character is not white-space");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Isalpha

isAlpha()
          [Characters]

Description
Analyse if a char is alpha (that is a letter). Returns true if thisChar contains a letter.
Syntax
isAlpha(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is alpha.


Returns
true: if thisChar is alpha.
Example Code
if (isAlpha(myChar)) {  // tests if myChar is a letter
  Serial.println("The character is a letter");
}
else {
  Serial.println("The character is not a letter");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Isprintable

isPrintable()
          [Characters]

Description
Analyse if a char is printable (that is any character that produces an output, even a blank space). Returns true if thisChar is printable.
Syntax
isPrintable(thisChar)
Parameters
thisChar: variable. Allowed data types: char.


Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is printable.
Example Code
if (isPrintable(myChar)) {  // tests if myChar is printable char
  Serial.println("The character is printable");
}
else {
  Serial.println("The character is not printable");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Iscontrol

isControl()
          [Characters]

Description
Analyse if a char is a control character. Returns true if thisChar is a control character.
Syntax
isControl(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is a control character.


Returns
true: if thisChar is a control character.
Example Code
if (isControl(myChar)) {  // tests if myChar is a control character
  Serial.println("The character is a control character");
}
else {
  Serial.println("The character is not a control character");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Isgraph

isGraph()
          [Characters]

Description
Analyse if a char is printable with some content (space is printable but has no content). Returns true if thisChar is printable.
Syntax
isGraph(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns


Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is printable.
Example Code
if (isGraph(myChar)) {  // tests if myChar is a printable character but not a blank space.
  Serial.println("The character is printable");
}
else {
  Serial.println("The character is not printable");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Iswhitespace

isWhitespace()
          [Characters]

Description
Analyse if a char is a space character. Returns true if the argument is a space or horizontal tab ('\t').
Syntax
isWhitespace(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns


Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is a space character.
Example Code
if (isWhitespace(myChar)) { // tests if myChar is a space character
  Serial.println("The character is a space or tab");
}
else {
  Serial.println("The character is not a space or tab");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Isascii

isAscii()
          [Characters]

Description
Analyse if a char is Ascii. Returns true if thisChar contains an Ascii character.
Syntax
isAscii(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is Ascii.


Returns
true: if thisChar is Ascii.
Example Code
if (isAscii(myChar)) {  // tests if myChar is an Ascii character
  Serial.println("The character is Ascii");
}
else {
  Serial.println("The character is not Ascii");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Isalphanumeric

isAlphaNumeric()
          [Characters]

Description
Analyse if a char is alphanumeric (that is a letter or a numbers). Returns true if thisChar contains either a number or a letter.
Syntax
isAlphaNumeric(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns


Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is alphanumeric.
Example Code
if (isAlphaNumeric(myChar)) { // tests if myChar isa letter or a number
  Serial.println("The character is alphanumeric");
}
else {
  Serial.println("The character is not alphanumeric");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Characters
            > Isdigit

isDigit()
          [Characters]

Description
Analyse if a char is a digit (that is a number). Returns true if thisChar is a number.
Syntax
isDigit(thisChar)
Parameters
thisChar: variable. Allowed data types: char.
Returns


Parameters
thisChar: variable. Allowed data types: char.
Returns
true: if thisChar is a number.
Example Code
if (isDigit(myChar)) {  // tests if myChar is a digit
  Serial.println("The character is a number");
}
else {
  Serial.println("The character is not a number");
}
See also
LANGUAGE char
LANGUAGE if (conditional operators)
LANGUAGE while (conditional operators)
LANGUAGE read()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Zero due mkr family
            > Analogwriteresolution

analogWriteResolution()
          [Zero, Due & MKR Family]

Description
analogWriteResolution() is an extension of the Analog API for the Arduino Due.


Description
analogWriteResolution() is an extension of the Analog API for the Arduino Due.
analogWriteResolution() sets the resolution of the analogWrite() function. It defaults to 8 bits (values between 0-255) for backward compatibility with AVR based boards.
The Due has the following hardware capabilities:
12 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution.
2 pins with 12-bit DAC (Digital-to-Analog Converter)
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.
The Zero has the following hardware capabilities:
10 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution.


1 pin with 10-bit DAC (Digital-to-Analog Converter).
By setting the write resolution to 10, you can use analogWrite() with values between 0 and 1023 to exploit the full DAC resolution
The MKR Family of boards has the following hardware capabilities:
4 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed from 8 (default) to 12-bit resolution.
1 pin with 10-bit DAC (Digital-to-Analog Converter)
By setting the write resolution to 12 bits, you can use analogWrite() with values between 0 and 4095 for PWM signals; set 10 bit on the DAC pin to exploit the full DAC resolution of 1024 values.
Syntax
analogWriteResolution(bits)
Parameters


Syntax
analogWriteResolution(bits)
Parameters
bits: determines the resolution (in bits) of the values used in the analogWrite() function. The value can range from 1 to 32. If you choose a resolution higher or lower than your board’s hardware capabilities, the value used in analogWrite() will be either truncated if it’s too high or padded with zeros if it’s too low. See the note below for details.
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);


// with an attached LED
  int sensorVal = analogRead(A0);
  Serial.print("Analog Read) : ");
  Serial.print(sensorVal);
  // the default PWM resolution
  analogWriteResolution(8);
  analogWrite(11, map(sensorVal, 0, 1023, 0, 255));
  Serial.print(" , 8-bit PWM value : ");
  Serial.print(map(sensorVal, 0, 1023, 0, 255));
  // change the PWM resolution to 12 bits
  // the full 12 bit resolution is only supported
  // on the Due
  analogWriteResolution(12);
  analogWrite(12, map(sensorVal, 0, 1023, 0, 4095));
  Serial.print(" , 12-bit PWM value : ");


Serial.print(" , 12-bit PWM value : ");
  Serial.print(map(sensorVal, 0, 1023, 0, 4095));
  // change the PWM resolution to 4 bits
  analogWriteResolution(4);
  analogWrite(13, map(sensorVal, 0, 1023, 0, 15));
  Serial.print(", 4-bit PWM value : ");
  Serial.println(map(sensorVal, 0, 1023, 0, 15));
  delay(5);
}
Notes and Warnings
If you set the analogWriteResolution() value to a value higher than your board’s capabilities, the Arduino will discard the extra bits. For example: using the Due with analogWriteResolution(16) on a 12-bit DAC pin, only the first 12 bits of the values passed to analogWrite() will be used and the last 4 bits will be discarded.


If you set the analogWriteResolution() value to a value lower than your board’s capabilities, the missing bits will be padded with zeros to fill the hardware required size. For example: using the Due with analogWriteResolution(8) on a 12-bit DAC pin, the Arduino will add 4 zero bits to the 8-bit value used in analogWrite() to obtain the 12 bits required.
See also
LANGUAGE analogWrite()
LANGUAGE analogRead()
LANGUAGE map()
EXAMPLE Description of the analog input pins


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Zero due mkr family
            > Analogreadresolution

analogReadResolution()
          [Zero, Due & MKR Family]

Description
analogReadResolution() is an extension of the Analog API for the Zero, Due, MKR family, Nano 33 (BLE and IoT) and Portenta.


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 Zero, Due, MKR family and Nano 33 (BLE and IoT) 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.
The Portenta H7 has a 16 bit ADC, which will allow values between 0 and 65535.
Syntax
analogReadResolution(bits)
Parameters
bits: determines the resolution (in bits) of the value returned by the analogRead() function. You can set this between 1 and 32. You can set resolutions higher than the supported 12 or 16 bits, but values returned by analogRead() will suffer approximation. See the note below for details.
Returns
Nothing


Returns
Nothing
Example Code
The code shows how to use ADC with different resolutions.
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));
  // change the resolution to 12 bits and read A0
  analogReadResolution(12);
  Serial.print(", 12-bit : ");
  Serial.print(analogRead(A0));
  // change the resolution to 16 bits and read A0
  analogReadResolution(16);
  Serial.print(", 16-bit : ");
  Serial.print(analogRead(A0));


Serial.print(", 16-bit : ");
  Serial.print(analogRead(A0));
  // change the resolution to 8 bits and read A0
  analogReadResolution(8);
  Serial.print(", 8-bit : ");
  Serial.println(analogRead(A0));
  // a little delay to not hog Serial Monitor
  delay(100);
}
Notes and Warnings
If you set the analogReadResolution() value to a value higher than your board’s capabilities, the Arduino will only report back at its highest resolution, padding the extra bits with zeros.
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.


If you set the analogReadResolution() value to a value lower than your board’s capabilities, the extra least significant bits read from the ADC will be discarded.
Using a 16 bit resolution (or any resolution higher than actual hardware capabilities) allows you to write sketches that automatically handle devices with a higher resolution ADC when these become available on future boards without changing a line of code.
See also
EXAMPLE Description of the analog input pins
LANGUAGE analogRead()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Time
            > Millis

millis()
          [Time]

Description
Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
Syntax
time = millis()
Parameters
None
Returns


Syntax
time = millis()
Parameters
None
Returns
Number of milliseconds passed since the program started. Data type: unsigned long.
Example Code
This example code prints on the serial port the number of milliseconds passed since the Arduino board started running the code itself.
unsigned long myTime;
void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.print("Time: ");
  myTime = millis();
  Serial.println(myTime); // prints time since program started
  delay(1000);          // wait a second so as not to send massive amounts of data
}
Notes and Warnings
Please note that the return value for millis() is of type unsigned long, logic errors may occur if a programmer tries to do arithmetic with smaller data types such as int. Even signed long may encounter errors as its maximum value is half that of its unsigned counterpart.


Reconfiguration of the microcontroller’s timers may result in inaccurate millis() readings. The "Arduino AVR Boards" and "Arduino megaAVR Boards" cores use Timer0 to generate millis(). The "Arduino ARM (32-bits) Boards" and "Arduino SAMD (32-bits ARM Cortex-M0+) Boards" cores use the SysTick timer.
See also
EXAMPLE Blink Without Delay


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Time
            > Delay

delay()
          [Time]

Description
Pauses the program for the amount of time (in milliseconds) specified as parameter. (There are 1000 milliseconds in a second.)
Syntax
delay(ms)
Parameters
ms: the number of milliseconds to pause. Allowed data types: unsigned long.
Returns
Nothing


Returns
Nothing
Example Code
The code pauses the program for one second before toggling the output pin.
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
}
Notes and Warnings


delay(1000);                // waits for a second
}
Notes and Warnings
While it is easy to create a blinking LED with the delay() function and many sketches use short delays for such tasks as switch debouncing, the use of delay() in a sketch has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt. For alternative approaches to controlling timing see the Blink Without Delay sketch, which loops, polling the millis() function until enough time has elapsed. More knowledgeable programmers usually avoid the use of delay() for timing of events longer than 10’s of milliseconds unless the Arduino sketch is very simple.


Certain things do go on while the delay() function is controlling the Atmega chip, however, because the delay function does not disable interrupts. Serial communication that appears at the RX pin is recorded, PWM (analogWrite) values and pin states are maintained, and interrupts will work as they should.
See also
EXAMPLE Blink Without Delay


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Time
            > Delaymicroseconds

delayMicroseconds()
          [Time]

Description
Pauses the program for the amount of time (in microseconds) specified by the parameter. There are a thousand microseconds in a millisecond and a million microseconds in a second.


Currently, the largest value that will produce an accurate delay is 16383; larger values can produce an extremely short delay. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead.
Syntax
delayMicroseconds(us)
Parameters
us: the number of microseconds to pause. Allowed data types: unsigned int.
Returns
Nothing
Example Code
The code configures pin number 8 to work as an output pin. It sends a train of pulses of approximately 100 microseconds period. The approximation is due to execution of the other instructions in the code.
int outPin = 8;               // digital pin 8
void setup() {
  pinMode(outPin, OUTPUT);    // sets the digital pin as output
}
void loop() {
  digitalWrite(outPin, HIGH); // sets the pin on
  delayMicroseconds(50);      // pauses for 50 microseconds


delayMicroseconds(50);      // pauses for 50 microseconds
  digitalWrite(outPin, LOW);  // sets the pin off
  delayMicroseconds(50);      // pauses for 50 microseconds
}
Notes and Warnings
This function works very accurately in the range 3 microseconds and up to 16383. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times. Larger delay times may actually delay for an extremely brief time.
As of Arduino 0018, delayMicroseconds() no longer disables interrupts.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Time
            > Micros

micros()
          [Time]

Description


> Micros

micros()
          [Time]

Description
Returns the number of microseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 70 minutes. On the boards from the Arduino Portenta family this function has a resolution of one microsecond on all cores. On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution of eight microseconds.
Syntax
time = micros()
Parameters
None
Returns
Returns the number of microseconds since the Arduino board began running the current program. Data type: unsigned long.
Example Code
The code returns the number of microseconds since the Arduino board began.
unsigned long time;
void setup() {


unsigned long time;
void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.print("Time: ");
  time = micros();
  Serial.println(time); //prints time since program started
  delay(1000);          // wait a second so as not to send massive amounts of data
}
Notes and Warnings
There are 1,000 microseconds in a millisecond and 1,000,000 microseconds in a second.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Interrupts
            > Nointerrupts

noInterrupts()
          [Interrupts]

Description


noInterrupts()
          [Interrupts]

Description
Disables interrupts (you can re-enable them with interrupts()). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.
Syntax
noInterrupts()
Parameters
None
Returns
Nothing
Example Code
The code shows how to enable interrupts.
void setup() {}
void loop() {
  noInterrupts();
  // critical, time-sensitive code here
  interrupts();
  // other code here
}
Notes and Warnings
Note that disabling interrupts on the Arduino boards with native USB capabilities (e.g., Leonardo) will make the board
not appear in the Port menu, since this disables its USB capability.
See also


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Interrupts
            > Interrupts

interrupts()
          [Interrupts]

Description


interrupts()
          [Interrupts]

Description
Re-enables interrupts (after they’ve been disabled by noInterrupts(). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.
Syntax
interrupts()
Parameters
None
Returns
Nothing
Example Code
The code enables Interrupts.
void setup() {}
void loop() {
  noInterrupts();
  // critical, time-sensitive code here
  interrupts();
  // other code here
}
See also
LANGUAGE attachInterrupts()
LANGUAGE detachInterrupts()


Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.

Last Revision: Searching...
Last Build: 2023/06/30
Edit This Page
Reference
            >  Language
            >  Functions
            >  Math
            > Sq

sq()
          [Math]

Description
Calculates the square of a number: the number multiplied by itself.
Syntax
sq(x)
Parameters
x: the number. Allowed data types: any data type.
Returns
The square of the number. Data type: double.
Notes and Warnings