Loading...
Searching...
No Matches
Analog inputs

Reading A0-A5, the same functions on every pin. More...

Functions

 analog_averaging (samples)
 Averages several readings for every analogue result.
 analog_read (name)
 Reads an analog pin as a 12-bit data.
 analog_full_scale_millivolts (name)
 Returns the maximum input voltage an analog pin can measure.
 analog_millivolts (name)
 Reads an analog pin in millivolts.
 analog_volts (name)
 Reads an analog input pin in volts.
 analog_differential_millivolts (plus, minus)
 The voltage of one analog input pin minus another, in millivolts.

Variables

int ADC_FULL_SCALE_MILLIVOLTS = 3300
 Maximum input voltage of A0-A2 (RP2040 ADC) in millivolts.
int EXT_ADC_FULL_SCALE_MILLIVOLTS = 2048
 Maximum input voltage A3-A5 (MAX11635, LM4040 reference on AREF) in millivolts.

Detailed Description

Reading A0-A5, the same functions on every pin.

A0-A2 are read by the RP2040's ADC (0-3.3 V). A3-A5 are read by the on-board MAX11635 (0-2.048 V), which is set up the first time one of them is read. Keep every pin between 0 V and its full scale.

Function Documentation

◆ analog_averaging()

analog_averaging ( samples)

Averages several readings for every analogue result.

Steadier readings, but each takes longer. A3-A5 average in the external ADC itself; A0-A2 take the samples one after another.

The setting is 1 at start-up and applies to every analogue reading after the call, on all six pins and vbus_volts(); cpu_temperature() is not averaged. On a steady 1 V, going from 1 to 32 cut the spread of 100 readings from about 16 codes to 3 on A0, and from about 10 to 1 on A3.

Parameters
samples1 (off), 4, 8, 16 or 32; other values use the next lower one (values below 1 use 1).
Returns
The setting used.
Example
mcu.analog_averaging(16) # average 16 samples per reading from now on
print(mcu.analog_millivolts("A0")) # a steadier reading

◆ analog_read()

analog_read ( name)

Reads an analog pin as a 12-bit data.

Inputs A0-A2 are read by the RP2040's ADC, A3-A5 by the on-board MAX11635. Each step is 0.8 mV on A0-A2 and 0.5 mV on A3-A5. The maximum input voltage for A0-A2 is 3.3V, and for A3-A5 is 2.048V. An unconnected pin drifts. For a voltage, use analog_millivolts() or analog_volts().

Parameters
name"A0"-"A5".
Returns
0-4095. A0-A2: 0 = 0 V, 4095 = 3.3 V. A3-A5: 0 = 0 V, 4095 = 2.048 V.
Exceptions
ValueErrorNot an analogue pin.
OSErrorThe external ADC did not respond (A3-A5).

◆ analog_full_scale_millivolts()

analog_full_scale_millivolts ( name)

Returns the maximum input voltage an analog pin can measure.

Parameters
name"A0"-"A5".
Returns
3300 for A0-A2, 2048 for A3-A5.
Exceptions
ValueErrorNot an analogue pin.

◆ analog_millivolts()

analog_millivolts ( name)

Reads an analog pin in millivolts.

The voltage is calculated by multiplying the value of analog_read() by the value of analog_full_scale_millivolts() then divided by the maximum 12bit count. Inputs A3-A5 are as accurate as the board's LM4040 reference, within 0.5 %.

Parameters
name"A0"-"A5".
Returns
Voltage in mV (a float).
Exceptions
ValueErrorNot an analogue pin.
OSErrorThe external ADC did not respond (A3-A5).

◆ analog_volts()

analog_volts ( name)

Reads an analog input pin in volts.

Parameters
name"A0"-"A5".
Returns
Voltage in V (a float).
Exceptions
ValueErrorNot an analogue pin.
OSErrorThe external ADC did not respond (A3-A5).

◆ analog_differential_millivolts()

analog_differential_millivolts ( plus,
minus )

The voltage of one analog input pin minus another, in millivolts.

A3/A4 (either order) are a true differential inputs: the external ADC samples both pins at the same instant, so common noise is removed from the measurement. The differential voltage range is +/-1024 mV in 0.5 mV steps for A3/A4; however, each input pins maximum voltage is still 2.048V.

Any other combination of analog input pins will be measured in sequence, plus then minus. This may cause error in the measurement depending on how fast the signals are changing voltage. Take this into consideration when using this function.

Parameters
plus"A0"-"A5".
minus"A0"-"A5".
Returns
plus - minus in mV (a float).
Exceptions
ValueErrorNot an analogue pin.
OSErrorThe external ADC did not respond (A3-A5).
Example
print(mcu.analog_differential_millivolts("A3", "A4")) # A3 minus A4, sampled together