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.
Available on these boards
- 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.- Paramètres:
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 :float¶
The maximum voltage measurable (also known as the reference voltage) as a
float
in Volts.
- __exit__() 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 board import * dac = analogio.AnalogOut(A2) # output on pin A2 dac.value = 32768 # makes A2 1.65V
Use the AnalogOut on the given pin.
- Paramètres:
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.
- __exit__() None ¶
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.