Skip to content

Analog Input

An IO class that represents an analog input source

Creates a new Analog object instance.

Analog(options)

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"

If the constructor requires a resource that is already in use — whether by a script or the native host — an Error exception is thrown.

Includes properties of the IO Class Pattern. Specific to this class:

Always returns "number". The string value set by the constructor options or by the script at any time to change how it reads data.

Indicates the number of bits of resolution provided in values returned by the instance, e.g 10.

Returns analog data from the IO instance.

read()

An integer from 0 to a maximum value based on the resolution of the analog input.

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;

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)

Analog input