Analog Input
An IO class that represents an analog input source
Constructor
Section titled “Constructor”Analog
Section titled “Analog”Creates a new Analog object instance.
Analog(options)Parameters
Section titled “Parameters”options
An object of properties used to construct the class.
pin- a pin specifier indicating the analog input pin.
resolution(optional) - The requested number of bits of resolution of the input. Default resolution is host-dependent.
format(optional) - a string that indicates the type of data used by the read method. Defaults to, and only accepts,"number"
Exceptions
Section titled “Exceptions”If the constructor requires a resource that is already in use — whether by a script or the native host — an Error exception is thrown.
Instance Properties
Section titled “Instance Properties”Includes properties of the IO Class Pattern. Specific to this class:
format
Section titled “format”Always returns "number". The string value set by the constructor options or by the script at any time to change how it reads data.
resolution
Section titled “resolution”Indicates the number of bits of resolution provided in values returned by the instance, e.g 10.
Instance Methods
Section titled “Instance Methods”Returns analog data from the IO instance.
read()Return value
Section titled “Return value”An integer from 0 to a maximum value based on the resolution of the analog input.
Examples
Section titled “Examples”The class can be imported from the embedded namespace or found on the host device global object:
import Analog from "embedded:io/analog";const Analog = device.io.Analog;Continually poll data from input
Section titled “Continually poll data from input”This example instantiates an analog input and read from it every 100 milliseconds using System.setInterval. It logs out the precise value based on the resolution.
const analogInput = new device.io.Analog({ pin: 0 // change the pin number for your development board})
System.setInterval(() => { const rawValue = analogInput.read(); const value = rawValue / ((1 << analogInput.resolution) - 1); console.log(`read: ${value.toPrecision(4)}`)}, 100)