Skip to content

Commit a135169

Browse files
authored
Added ADC API doc + simple example (#6301)
* Added ADC API doc + simple example * Added attenuation input voltage range + conf.py added tabs extension * Update requirements.txt * Update adc.rst
1 parent b5f3d6c commit a135169

File tree

4 files changed

+230
-1
lines changed

4 files changed

+230
-1
lines changed

Diff for: docs/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
#
44
# matplotlib is currently required only by the script generate_chart.py
55
sphinx-copybutton==0.3.0
6+
sphinx-tabs==3.2.0

Diff for: docs/source/api/adc.rst

+208
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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.

Diff for: docs/source/conf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3131
# ones.
3232
extensions = [
33-
'sphinx_copybutton'
33+
'sphinx_copybutton',
34+
'sphinx_tabs.tabs'
3435
]
3536

3637
# Add any paths that contain templates here, relative to this directory.

Diff for: libraries/ESP32/examples/AnalogRead/AnalogRead.ino

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
void setup() {
2+
// initialize serial communication at 115200 bits per second:
3+
Serial.begin(115200);
4+
5+
//set the resolution to 12 bits (0-4096)
6+
analogReadResolution(12);
7+
}
8+
9+
void loop() {
10+
// read the analog / millivolts value for pin 2:
11+
int analogValue = analogRead(2);
12+
int analogVolts = analogReadMilliVolts(2);
13+
14+
// print out the values you read:
15+
Serial.printf("ADC analog value = %d\n",analogValue);
16+
Serial.printf("ADC millivolts value = %d\n",analogVolts);
17+
18+
delay(100); // delay in between reads for clear read from serial
19+
}

0 commit comments

Comments
 (0)