analogio
– Analog hardware support¶
The analogio
module contains classes to provide access to analog IO
typically implemented with digital-to-analog (DAC) and analog-to-digital
(ADC) converters.
All classes change hardware state and should be deinitialized when they
are no longer needed if the program continues after use. To do so, either
call deinit()
or use a context manager. See
Lifetime and ContextManagers for more info.
For example:
import analogio
from board import *
pin = analogio.AnalogIn(A0)
print(pin.value)
pin.deinit()
This example will initialize the the device, read
value
and then
deinit()
the hardware. The last step is optional
because CircuitPython will do it automatically after the program finishes.
-
class
analogio.
AnalogIn
(pin: microcontroller.Pin)¶ Read analog voltage levels
Usage:
import analogio from board import * adc = analogio.AnalogIn(A1) val = adc.value
Use the AnalogIn on the given pin. The reference voltage varies by platform so use
reference_voltage
to read the configured setting.- Parameters
pin (Pin) – the pin to read from
-
value
:int¶ The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only)
Even if the underlying analog to digital converter (ADC) is lower resolution, the value is 16-bit.
-
reference_voltage
:Optional[float]¶ The maximum voltage measurable (also known as the reference voltage) as a
float
in Volts.
-
__enter__
(self) → AnalogIn¶ No-op used by Context Managers.
-
__exit__
(self) → None¶ Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
-
class
analogio.
AnalogOut
(pin: microcontroller.Pin)¶ Output analog values (a specific voltage).
Example usage:
import analogio from microcontroller import pin dac = analogio.AnalogOut(pin.PA02) # output on pin PA02 dac.value = 32768 # makes PA02 1.65V
Use the AnalogOut on the given pin.
- Parameters
pin (Pin) – the pin to output to
-
value
:int¶ The value on the analog pin between 0 and 65535 inclusive (16-bit). (write-only)
Even if the underlying digital to analog converter (DAC) is lower resolution, the value is 16-bit.
-
__enter__
(self) → AnalogOut¶ No-op used by Context Managers.
-
__exit__
(self) → None¶ Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.