The AIO API supports reading analog input pins that measure the analog voltage signal between 0 and a maximum voltage (usually 3.3 or 5 Volts), then do Analog-to-Digital Conversion (ADC) with a resolution of 10 or 12 bits on most boards, so that the result (pin value) is 0 to 1023 or 0 to 4095, inclusively.
On some boards access to AIO may be asynchronous. This API uses synchronous methods.
AIO functionality is exposed by an object that can be obtained by using the aio()
method of the Board
API. See also the Web IDL. The API object exposes the following method:
Method | Description |
---|---|
open() |
synchronous open |
Configures an AIO pin using data provided by the options
argument. It involves the following steps:
- If
options
is a string, create a dictionary 'init' and use the value ofoptions
to initialize theinit.pin
property. - Otherwise if
options
is a number, create a dictionary 'init' and use the value ofoptions
to initialize theinit.pin
property. - Otherwise if
options
is a dictionary, letinit
beoptions
. It may contain the followingAIO
properties, where at leastpin
MUST be specified:pin
for board pin name with the valid values defined by the board, or for the numeric index of the analog pin;precision
for the bit width of a sample (if the board supports setting the sampling rate).
- If any property of
init
is specified and has an invalid value on the given board, as defined by the board documentation, throwTypeError
. - Request the underlying platform to initialize AIO on
init.pin
(if defined) or otherwiseinit.channel
. - In case of failure, throw
InvalidAccessError
. - Let
aio
be theAIOPin
](#aiopin) object that represents the hardware pin identified byinit.pin
, as defined by the board documentation. - If
init.precision
is defined, request the board to set the precision and initialize theaio.precision
property with the value supported by the board. If there is an error, throwInvalidAccessError
. - Initialize the
aio.value
property withundefined
. - Return the
aio
object.
The AIO
object extends the Pin
object.
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
pin |
String or Number | no | undefined |
board name for the pin |
precision |
unsigned long | yes | undefined |
bit length of digital sample |
Method | Description |
---|---|
read() |
synchronous read |
close() |
close the pin |
The pin
property inherited from Pin
can take values defined by the board documentation, usually strings prefixed by "A"
, but it can also be specified as the numeric index of the analog pin, where pin 0 corresponding to the first analog pin and so forth.
The precision
property represents the bit length of the digital sample. It is usually 10 or 12 bits, depending on board.
Performs a synchronous read operation for the pin value. It returns the pin value representing the last sampling.
Called when the application is no longer interested in the pin. Until the next open() is called, invoking the read()
method throws InvalidAccessError
.
try {
var aio = require("board").aio(); // initialize AIO on the board
var a1 = aio.open("A1");
console.log(board.name + " AIO pin 1 value: " + a1.read());
a1.close();
var a4 = aio.open({pin: "A4", precision: 12 });
setTimeout(function() {
a4.close();
}, 10500);
setInterval(function() {
console.log("AIO pin 4 value: " + a4.read());
}, 1000);
} catch (err) {
console.log("AIO error: " + err.message);
};