Class Adc

extends Object
ADC unit for reading the voltage on GPIO Pins.

Class summary


constructor pin/any --max-voltage/float= --allow-restricted/bool=
Initializes an Adc unit for the pin.

Constructors

constructor pin/any --max-voltage/float=null --allow-restricted/bool=false
Initializes an Adc unit for the pin.
The pin is a GPIO number. The ADC reserves the pin and releases it again when the ADC is closed.
Use max-voltage to indicate max voltage expected to measure. This helps to tune the attenuation of the underlying ADC unit. If no max-voltage is provided, the ADC uses the maximum voltage range of the pin.
ESP32 and variants.
On most ESP32 variants, there are two ADCs. Typically, ADC1 should be preferred as ADC2 has lots of restrictions. It can't be used when WiFi is active, and some of the pins are strapping pins.
By default, ADC2 is disabled, and users need to pass in the allow-restricted flag to allow its use.
Passing a Pin is deprecated; provide the integer GPIO number instead. The Pin form will be removed in a future release.

Statics

channel channel/int --max-voltage/float=null -> Adc
Initializes an Adc unit on a dedicated analog channel rather than a GPIO pin.
Some chips expose the ADC as fixed analog inputs addressed by channel number instead of as a function on a GPIO pad, so there is no pin (it is null). The get/close operations are the same; only the construction differs.
EC618
Channel 0 -> AIO3 and channel 1 -> AIO4 (the board's "ADC0"/"ADC1" pins). The converter core measures 0..1.2 V; max-voltage selects the smallest range (up to 3.8 V) that covers it, for the best resolution. The higher ranges read above 1.2 V by switching in a resistor divider *inside the chip*; get compensates for it and returns the true voltage at the pin, so callers see the actual input regardless of range. Supported maxima: 1.2, 1.4, 1.6, 1.9, 2.4, 2.7, 3.2, 3.8 V; null uses the widest (3.8 V).

Methods

close -> none
Closes the ADC unit and releases the associated resources.

get --samples/any=64 -> float
Measures the voltage on the pin.

get --raw/bool -> int
Measures the voltage on the pin and returns the obtained raw value.
On the ESP32 the ADC readings are 12 bits, so the value will be in the range 0-4095.
The returned value is not scaled to the voltage range of the pin.
The value is not using the calibration data of the chip.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

Fields

pin / any