id
stringlengths 14
16
| text
stringlengths 10
1.45k
| source
stringlengths 46
118
|
---|---|---|
25fd27952faf-1
|
Returns
The value of the bit (0 or 1).
See also
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitread/index.html
|
ef43bb79e29e-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitwrite/index.html
|
ef43bb79e29e-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitwrite/index.html
|
ef43bb79e29e-2
|
Serial.println(x, BIN); // 10000001
}
void loop() {}
See also
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitwrite/index.html
|
d4e7743dd3eb-0
|
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()
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/lowbyte/index.html
|
5d83f80140d8-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitset/index.html
|
5d83f80140d8-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitset/index.html
|
22585d594b99-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitclear/index.html
|
22585d594b99-1
|
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;
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitclear/index.html
|
22585d594b99-2
|
}
int x = 6;
int n = 1;
Serial.print(bitClear(x, n)); // print the output of bitClear(x,n)
}
void loop() {
}
See also
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitclear/index.html
|
f1d8861c5f48-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bit/index.html
|
f1d8861c5f48-1
|
Syntax
bit(n)
Parameters
n: the bit whose value to compute
Returns
The value of the bit.
See also
|
https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bit/index.html
|
33fb1ef5d0b6-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/communication/wire/index.html
|
33fb1ef5d0b6-1
|
> 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
|
https://www.arduino.cc/reference/en/language/functions/communication/wire/index.html
|
33fb1ef5d0b6-2
|
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.
|
https://www.arduino.cc/reference/en/language/functions/communication/wire/index.html
|
33fb1ef5d0b6-3
|
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.
|
https://www.arduino.cc/reference/en/language/functions/communication/wire/index.html
|
33fb1ef5d0b6-4
|
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()
|
https://www.arduino.cc/reference/en/language/functions/communication/wire/index.html
|
9ef2b6915a32-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/communication/spi/index.html
|
9ef2b6915a32-1
|
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()
|
https://www.arduino.cc/reference/en/language/functions/communication/spi/index.html
|
2e4ee98c0ef7-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/communication/stream/index.html
|
2e4ee98c0ef7-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/communication/stream/index.html
|
72a9eede3adc-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/communication/serial/index.html
|
72a9eede3adc-1
|
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)
|
https://www.arduino.cc/reference/en/language/functions/communication/serial/index.html
|
72a9eede3adc-2
|
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.
|
https://www.arduino.cc/reference/en/language/functions/communication/serial/index.html
|
72a9eede3adc-3
|
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
|
https://www.arduino.cc/reference/en/language/functions/communication/serial/index.html
|
92cd973651fa-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/usb/keyboard/index.html
|
92cd973651fa-1
|
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.
|
https://www.arduino.cc/reference/en/language/functions/usb/keyboard/index.html
|
92cd973651fa-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/usb/keyboard/index.html
|
29221661fd07-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/usb/mouse/index.html
|
29221661fd07-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/usb/mouse/index.html
|
29221661fd07-2
|
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.
|
https://www.arduino.cc/reference/en/language/functions/usb/mouse/index.html
|
fa23c128c7e0-0
|
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)
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/detachinterrupt/index.html
|
fa23c128c7e0-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/detachinterrupt/index.html
|
3d997e3ef9a4-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-1
|
[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
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-3
|
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.
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-4
|
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.
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-5
|
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)
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-6
|
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.
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-7
|
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
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-8
|
}
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
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
3d997e3ef9a4-9
|
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
|
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/index.html
|
6f5f16ce7745-0
|
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.)
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/index.html
|
6f5f16ce7745-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/index.html
|
6f5f16ce7745-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/index.html
|
6f5f16ce7745-3
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/index.html
|
6f5f16ce7745-4
|
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.
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/index.html
|
6f5f16ce7745-5
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/index.html
|
28d6b039c179-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/index.html
|
28d6b039c179-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/index.html
|
28d6b039c179-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/index.html
|
28d6b039c179-3
|
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.
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/index.html
|
28d6b039c179-4
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/index.html
|
28d6b039c179-5
|
}
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/index.html
|
01715a331cbe-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/index.html
|
01715a331cbe-1
|
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().
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/index.html
|
01715a331cbe-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/index.html
|
01715a331cbe-3
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/index.html
|
01715a331cbe-4
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/index.html
|
01715a331cbe-5
|
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
|
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/index.html
|
cdfa0acff13f-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/index.html
|
cdfa0acff13f-1
|
> 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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/index.html
|
cdfa0acff13f-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/index.html
|
ba89feb7d9d1-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/index.html
|
ba89feb7d9d1-1
|
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.
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/index.html
|
ba89feb7d9d1-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/index.html
|
7c085beb12a0-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/index.html
|
7c085beb12a0-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/index.html
|
7c085beb12a0-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/index.html
|
c87f3ab69c0c-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/pulseinlong/index.html
|
c87f3ab69c0c-1
|
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.
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/pulseinlong/index.html
|
c87f3ab69c0c-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/pulseinlong/index.html
|
e24ff2fd03cd-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/index.html
|
e24ff2fd03cd-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/index.html
|
e24ff2fd03cd-2
|
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 //
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/index.html
|
e24ff2fd03cd-3
|
// 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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/index.html
|
e24ff2fd03cd-4
|
//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);
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/index.html
|
e24ff2fd03cd-5
|
// 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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/index.html
|
5fea173589c4-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/notone/index.html
|
5fea173589c4-1
|
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
|
https://www.arduino.cc/reference/en/language/functions/advanced-io/notone/index.html
|
392717f53d05-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/index.html
|
392717f53d05-1
|
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);
|
https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/index.html
|
392717f53d05-2
|
randNumber = random(300);
Serial.println(randNumber);
delay(50);
}
Notes and Warnings
If the seed is 0, randomSeed(seed) will have no effect.
See also
|
https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/index.html
|
a4559b02e4c2-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/index.html
|
a4559b02e4c2-1
|
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);
|
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/index.html
|
a4559b02e4c2-2
|
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
|
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/index.html
|
beca92cd261d-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/characters/ishexadecimaldigit/index.html
|
beca92cd261d-1
|
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()
|
https://www.arduino.cc/reference/en/language/functions/characters/ishexadecimaldigit/index.html
|
2ec0bf6bfb0a-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/characters/islowercase/index.html
|
2ec0bf6bfb0a-1
|
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()
|
https://www.arduino.cc/reference/en/language/functions/characters/islowercase/index.html
|
5806baee5ecf-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/characters/ispunct/index.html
|
5806baee5ecf-1
|
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()
|
https://www.arduino.cc/reference/en/language/functions/characters/ispunct/index.html
|
18e722e881df-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/characters/isuppercase/index.html
|
18e722e881df-1
|
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()
|
https://www.arduino.cc/reference/en/language/functions/characters/isuppercase/index.html
|
577515905707-0
|
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
|
https://www.arduino.cc/reference/en/language/functions/characters/isspace/index.html
|
577515905707-1
|
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()
|
https://www.arduino.cc/reference/en/language/functions/characters/isspace/index.html
|
087084293f59-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/characters/isalpha/index.html
|
087084293f59-1
|
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()
|
https://www.arduino.cc/reference/en/language/functions/characters/isalpha/index.html
|
bc0c002b415d-0
|
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.
|
https://www.arduino.cc/reference/en/language/functions/characters/isprintable/index.html
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.