|
| 1 | +### |
| 2 | +ADC |
| 3 | +### |
| 4 | + |
| 5 | +About |
| 6 | +----- |
| 7 | + |
| 8 | +ADC (analog to digital converter) is a very common peripheral used to convert an analog signal such as voltage |
| 9 | +to a digital form so that it can be read and processed by a microcontroller. |
| 10 | + |
| 11 | +ADCs are very useful in control and monitoring applications since most sensors |
| 12 | +(e.g., temperature, pressure, force) produce analogue output voltages. |
| 13 | + |
| 14 | +.. note:: Each SoC or module has a different number of ADC's with a different number of channels and pins availible. Refer to datasheet of each board for more info. |
| 15 | + |
| 16 | +Arduino-ESP32 ADC API |
| 17 | +--------------------- |
| 18 | + |
| 19 | +ADC common API |
| 20 | +************** |
| 21 | + |
| 22 | +analogRead |
| 23 | +^^^^^^^^^^ |
| 24 | + |
| 25 | +This function is used to get the ADC raw value for a given pin/ADC channel. |
| 26 | + |
| 27 | +.. code-block:: arduino |
| 28 | +
|
| 29 | + uint16_t analogRead(uint8_t pin); |
| 30 | +
|
| 31 | +* ``pin`` GPIO pin to read analog value |
| 32 | + |
| 33 | +This function will return analog raw value. |
| 34 | + |
| 35 | +analogReadMillivolts |
| 36 | +^^^^^^^^^^^^^^^^^^^^ |
| 37 | + |
| 38 | +This function is used to get ADC value for a given pin/ADC channel in millivolts. |
| 39 | + |
| 40 | +.. code-block:: arduino |
| 41 | +
|
| 42 | + uint32_t analogReadMilliVolts(uint8_t pin); |
| 43 | +
|
| 44 | +* ``pin`` GPIO pin to read analog value |
| 45 | + |
| 46 | +This function will return analog value in millivolts. |
| 47 | + |
| 48 | +analogReadResolution |
| 49 | +^^^^^^^^^^^^^^^^^^^^ |
| 50 | + |
| 51 | +This function is used to set the resolution of ``analogRead`` return value. Default is 12 bits (range from 0 to 4096) |
| 52 | +for all chips except ESP32S3 where default is 13 bits (range from 0 to 8192). |
| 53 | +When different resolution is set, the values read will be shifted to match the given resolution. |
| 54 | + |
| 55 | +Range is 1 - 16 .The default value will be used, if this function is not used. |
| 56 | + |
| 57 | +.. note:: For the ESP32, the resolution is between 9 to12 and it will change the ADC hardware resolution. Else value will be shifted. |
| 58 | + |
| 59 | +.. code-block:: arduino |
| 60 | +
|
| 61 | + void analogReadResolution(uint8_t bits); |
| 62 | +
|
| 63 | +* ``bits`` sets analog read resolution |
| 64 | + |
| 65 | +analogSetClockDiv |
| 66 | +^^^^^^^^^^^^^^^^^ |
| 67 | + |
| 68 | +This function is used to set the divider for the ADC clock. |
| 69 | + |
| 70 | +Range is 1 - 255. Default value is 1. |
| 71 | + |
| 72 | +.. code-block:: arduino |
| 73 | +
|
| 74 | + void analogSetClockDiv(uint8_t clockDiv); |
| 75 | +
|
| 76 | +* ``clockDiv`` sets the divider for ADC clock. |
| 77 | + |
| 78 | +analogSetAttenuation |
| 79 | +^^^^^^^^^^^^^^^^^^^^ |
| 80 | + |
| 81 | +This function is used to set the attenuation for all channels. |
| 82 | + |
| 83 | +Input voltages can be attenuated before being input to the ADCs. |
| 84 | +There are 4 available attenuation options, the higher the attenuation is, the higher the measurable input voltage could be. |
| 85 | + |
| 86 | +The measurable input voltage differs for each chip, see table below for detailed information. |
| 87 | + |
| 88 | +.. tabs:: |
| 89 | + |
| 90 | + .. tab:: ESP32 |
| 91 | + |
| 92 | + ===================== =========================================== |
| 93 | + Attenuation Measurable input voltage range |
| 94 | + ===================== =========================================== |
| 95 | + ``ADC_ATTEN_DB_0`` 100 mV ~ 950 mV |
| 96 | + ``ADC_ATTEN_DB_2_5`` 100 mV ~ 1250 mV |
| 97 | + ``ADC_ATTEN_DB_6`` 150 mV ~ 1750 mV |
| 98 | + ``ADC_ATTEN_DB_11`` 150 mV ~ 2450 mV |
| 99 | + ===================== =========================================== |
| 100 | + |
| 101 | + .. tab:: ESP32-S2 |
| 102 | + |
| 103 | + ===================== =========================================== |
| 104 | + Attenuation Measurable input voltage range |
| 105 | + ===================== =========================================== |
| 106 | + ``ADC_ATTEN_DB_0`` 0 mV ~ 750 mV |
| 107 | + ``ADC_ATTEN_DB_2_5`` 0 mV ~ 1050 mV |
| 108 | + ``ADC_ATTEN_DB_6`` 0 mV ~ 1300 mV |
| 109 | + ``ADC_ATTEN_DB_11`` 0 mV ~ 2500 mV |
| 110 | + ===================== =========================================== |
| 111 | + |
| 112 | + .. tab:: ESP32-C3 |
| 113 | + |
| 114 | + ===================== =========================================== |
| 115 | + Attenuation Measurable input voltage range |
| 116 | + ===================== =========================================== |
| 117 | + ``ADC_ATTEN_DB_0`` 0 mV ~ 750 mV |
| 118 | + ``ADC_ATTEN_DB_2_5`` 0 mV ~ 1050 mV |
| 119 | + ``ADC_ATTEN_DB_6`` 0 mV ~ 1300 mV |
| 120 | + ``ADC_ATTEN_DB_11`` 0 mV ~ 2500 mV |
| 121 | + ===================== =========================================== |
| 122 | + |
| 123 | + .. tab:: ESP32-S3 |
| 124 | + |
| 125 | + ===================== =========================================== |
| 126 | + Attenuation Measurable input voltage range |
| 127 | + ===================== =========================================== |
| 128 | + ``ADC_ATTEN_DB_0`` 0 mV ~ 950 mV |
| 129 | + ``ADC_ATTEN_DB_2_5`` 0 mV ~ 1250 mV |
| 130 | + ``ADC_ATTEN_DB_6`` 0 mV ~ 1750 mV |
| 131 | + ``ADC_ATTEN_DB_11`` 0 mV ~ 3100 mV |
| 132 | + ===================== =========================================== |
| 133 | + |
| 134 | +.. code-block:: arduino |
| 135 | +
|
| 136 | + void analogSetAttenuation(adc_attenuation_t attenuation); |
| 137 | +
|
| 138 | +* ``attenuation`` sets the attenuation. |
| 139 | + |
| 140 | +analogSetPinAttenuation |
| 141 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 142 | + |
| 143 | +This function is used to set the attenuation for a specific pin/ADC channel. For more information refer to `analogSetAttenuation`_. |
| 144 | + |
| 145 | +.. code-block:: arduino |
| 146 | +
|
| 147 | + void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation); |
| 148 | +
|
| 149 | +* ``pin`` selects specific pin for attenuation settings. |
| 150 | +* ``attenuation`` sets the attenuation. |
| 151 | + |
| 152 | +adcAttachPin |
| 153 | +^^^^^^^^^^^^ |
| 154 | + |
| 155 | +This function is used to attach the pin to ADC (it will also clear any other analog mode that could be on) |
| 156 | + |
| 157 | +.. code-block:: arduino |
| 158 | +
|
| 159 | + bool adcAttachPin(uint8_t pin); |
| 160 | +
|
| 161 | +This function will return ``true`` if configuration is successful. Else returns ``false``. |
| 162 | + |
| 163 | +ADC API specific for ESP32 chip |
| 164 | +******************************* |
| 165 | + |
| 166 | +analogSetWidth |
| 167 | +^^^^^^^^^^^^^^ |
| 168 | + |
| 169 | +This function is used to set the hardware sample bits and read resolution. |
| 170 | +Default is 12bit (0 - 4095). |
| 171 | +Range is 9 - 12. |
| 172 | + |
| 173 | +.. code-block:: arduino |
| 174 | +
|
| 175 | + void analogSetWidth(uint8_t bits); |
| 176 | + |
| 177 | +analogSetVRefPin |
| 178 | +^^^^^^^^^^^^^^^^ |
| 179 | + |
| 180 | +This function is used to set pin to use for ADC calibration if the esp is not already calibrated (pins 25, 26 or 27). |
| 181 | + |
| 182 | +.. code-block:: arduino |
| 183 | +
|
| 184 | + void analogSetVRefPin(uint8_t pin); |
| 185 | +
|
| 186 | +* ``pin`` GPIO pin to set VRefPin for ADC calibration |
| 187 | + |
| 188 | +hallRead |
| 189 | +^^^^^^^^ |
| 190 | + |
| 191 | +This function is used to get the ADC value of the HALL sensor conneted to pins 36(SVP) and 39(SVN). |
| 192 | + |
| 193 | +.. code-block:: arduino |
| 194 | +
|
| 195 | + int hallRead(); |
| 196 | + |
| 197 | +This function will return the hall sensor value. |
| 198 | + |
| 199 | + |
| 200 | +Example Applications |
| 201 | +******************** |
| 202 | + |
| 203 | +Here is an example of how to use the ADC. |
| 204 | + |
| 205 | +.. literalinclude:: ../../../libraries/ESP32/examples/AnalogRead/AnalogRead.ino |
| 206 | + :language: arduino |
| 207 | + |
| 208 | +Or you can run Arduino example 01.Basics -> AnalogReadSerial. |
0 commit comments