|
| 1 | +ADC (analog to digital conversion) |
| 2 | +==================================== |
| 3 | + |
| 4 | +.. include:: ../refs/system.ref |
| 5 | +.. include:: ../refs/hardware.adc.ref |
| 6 | + |
| 7 | +On the ESP32 chip, ADC functionality is available on pins 32-39 (ADC channel 1) and |
| 8 | +pins 0, 2, 4, 12-15 and 25-27 (ADC channel 2). |
| 9 | + |
| 10 | +On the ESP32S3 chip, ADC functionality is available on pins 1-10 (ADC channel 1) and |
| 11 | +pins 11-14 and 17-20 (ADC block 2). |
| 12 | + |
| 13 | +ADC channel 2 is also used by WiFi and so attempting to read analog values from |
| 14 | +channel 2 pins when WiFi is active will raise an exception. |
| 15 | + |
| 16 | + |
| 17 | +Micropython Example: |
| 18 | + |
| 19 | + .. literalinclude:: ../../../examples/hardware/adc/adc_cores3_example.py |
| 20 | + :language: python |
| 21 | + :linenos: |
| 22 | + |
| 23 | +UIFLOW2 Example: |
| 24 | + |
| 25 | + |example.png| |
| 26 | + |
| 27 | +.. only:: builder_html |
| 28 | + |
| 29 | + |adc_cores3_example.m5f2| |
| 30 | + |
| 31 | + |
| 32 | +class ADC |
| 33 | +--------- |
| 34 | + |
| 35 | +.. class:: ADC(pin, *, atten) |
| 36 | + |
| 37 | + Return the ADC object for the specified pin. ESP32 does not support |
| 38 | + different timings for ADC sampling and so the ``sample_ns`` keyword argument |
| 39 | + is not supported. |
| 40 | + |
| 41 | + To read voltages above the reference voltage, apply input attenuation with |
| 42 | + the ``atten`` keyword argument. Valid values (and approximate linear |
| 43 | + measurement ranges) are: |
| 44 | + |
| 45 | + - ``ADC.ATTN_0DB``: No attenuation (100mV - 950mV) |
| 46 | + - ``ADC.ATTN_2_5DB``: 2.5dB attenuation (100mV - 1250mV) |
| 47 | + - ``ADC.ATTN_6DB``: 6dB attenuation (150mV - 1750mV) |
| 48 | + - ``ADC.ATTN_11DB``: 11dB attenuation (150mV - 2450mV) |
| 49 | + |
| 50 | + UIFLOW2: |
| 51 | + |
| 52 | + |init.png| |
| 53 | + |
| 54 | +.. Warning:: |
| 55 | + Note that the absolute maximum voltage rating for input pins is 3.6V. Going |
| 56 | + near to this boundary risks damage to the IC! |
| 57 | + |
| 58 | +Methods |
| 59 | +------- |
| 60 | +.. method:: ADC.read() |
| 61 | + |
| 62 | + This method returns the raw ADC value ranged according to the resolution of |
| 63 | + the block, e.g., 0-4095 for 12-bit resolution. |
| 64 | + |
| 65 | + UIFLOW2: |
| 66 | + |
| 67 | + |read.png| |
| 68 | + |
| 69 | +.. method:: ADC.read_u16() |
| 70 | + |
| 71 | + Take an analog reading and return an integer in the range 0-65535. |
| 72 | + The return value represents the raw reading taken by the ADC, scaled |
| 73 | + such that the minimum value is 0 and the maximum value is 65535. |
| 74 | + |
| 75 | + UIFLOW2: |
| 76 | + |
| 77 | + |read_u16.png| |
| 78 | + |
| 79 | +.. method:: ADC.read_uv() |
| 80 | + |
| 81 | + This method uses the known characteristics of the ADC and per-package eFuse |
| 82 | + values - set during manufacture - to return a calibrated input voltage |
| 83 | + (before attenuation) in microvolts. The returned value has only millivolt |
| 84 | + resolution (i.e., will always be a multiple of 1000 microvolts). |
| 85 | + |
| 86 | + The calibration is only valid across the linear range of the ADC. In |
| 87 | + particular, an input tied to ground will read as a value above 0 microvolts. |
| 88 | + Within the linear range, however, more accurate and consistent results will |
| 89 | + be obtained than using `read_u16()` and scaling the result with a constant. |
| 90 | + |
| 91 | + UIFLOW2: |
| 92 | + |
| 93 | + |read_uv.png| |
| 94 | + |
| 95 | +.. method:: ADC.atten(atten) |
| 96 | + |
| 97 | + Equivalent to ``ADC.init(atten=atten)``. |
| 98 | + |
| 99 | + UIFLOW2: |
| 100 | + |
| 101 | + |atten.png| |
| 102 | + |
| 103 | +.. method:: ADC.width(bits) |
| 104 | + |
| 105 | + Equivalent to ``ADC.block().init(bits=bits)``. |
| 106 | + |
| 107 | + For compatibility, the ``ADC`` object also provides constants matching the |
| 108 | + supported ADC resolutions: |
| 109 | + |
| 110 | + - ``ADC.WIDTH_9BIT`` = 9 |
| 111 | + - ``ADC.WIDTH_10BIT`` = 10 |
| 112 | + - ``ADC.WIDTH_11BIT`` = 11 |
| 113 | + - ``ADC.WIDTH_12BIT`` = 12 |
| 114 | + |
| 115 | + UIFLOW2: |
| 116 | + |
| 117 | + |width.png| |
0 commit comments