-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Added ADC API doc + simple example #6301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
### | ||
ADC | ||
### | ||
|
||
About | ||
----- | ||
|
||
ADC (analog to digital converter) is a very common peripheral used to convert an analogue signal such as voltage | ||
to a digital form so that it can be read and processed by a microcontroller. | ||
|
||
ADCs are very useful in control and monitoring applications since most sensors | ||
(e.g., temperature sensor, pressure sensor, force sensor) produce analogue output voltages. | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. note:: Each board have ADC different number of ADC channels and pins availible. Refer to datasheet of each board for more info. | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Arduino-ESP32 ADC API | ||
--------------------- | ||
|
||
ADC common API | ||
************** | ||
|
||
analogRead | ||
^^^^^^^^^^ | ||
|
||
This function is used to get ADC value for pin. | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: arduino | ||
|
||
uint16_t analogRead(uint8_t pin); | ||
|
||
* ``pin`` GPIO pin to read analog value | ||
|
||
This function will return analog value. | ||
|
||
analogReadMillivolts | ||
^^^^^^^^^^^^^^^^^^^^ | ||
|
||
This function is used to get ADC value for pin in millivolts. | ||
|
||
.. code-block:: arduino | ||
|
||
uint32_t analogReadMilliVolts(uint8_t pin); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* ``pin`` GPIO pin to read analog value | ||
|
||
This function will return analog value in millivolts. | ||
|
||
analogReadResolution | ||
^^^^^^^^^^^^^^^^^^^^ | ||
|
||
This function is used to set the resolution of analogRead return value. Default is 12 bits (range from 0 to 4096) | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for all chips except ESP32S3 where default is 13 bits (range from 0 to 8192). | ||
When different resolution is set, the values read will be shifted to match the given resolution. | ||
|
||
Range is 1 - 16 .The default value will be used, if this function is not used. | ||
|
||
.. note:: For ESP32 resolution between 9 and 12 will change ADC hardware resolution. Else value will be shifted. | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: arduino | ||
|
||
void analogReadResolution(uint8_t bits); | ||
|
||
* ``bits`` sets analog read resolution | ||
|
||
analogSetClockDiv | ||
^^^^^^^^^^^^^^^^^ | ||
|
||
This function is used to set the divider for the ADC clock. | ||
|
||
Range is 1 - 255. Default value is 1. | ||
|
||
.. code-block:: arduino | ||
|
||
void analogSetClockDiv(uint8_t clockDiv); | ||
|
||
* ``clockDiv`` sets the divider for ADC clock. | ||
|
||
analogSetAttenuation | ||
^^^^^^^^^^^^^^^^^^^^ | ||
|
||
This function is used to set the attenuation for all channels. | ||
|
||
Default is 11db. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe worth mentioning what is the maximum input voltage for each attenuation setting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added :) |
||
.. code-block:: arduino | ||
|
||
void analogSetAttenuation(adc_attenuation_t attenuation); | ||
|
||
* ``attenuation`` sets the attenuation. | ||
|
||
* can be set to ADC_0db, ADC_2_5db, ADC_6db or ADC_11db. | ||
|
||
analogSetPinAttenuation | ||
^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
This function is used to set the attenuation for a specific pin. | ||
|
||
Default is 11db. | ||
|
||
.. code-block:: arduino | ||
|
||
void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation); | ||
|
||
* ``pin`` selects specific pin for attenuation settings. | ||
* ``attenuation`` sets the attenuation. | ||
|
||
* can be set to ADC_0db, ADC_2_5db, ADC_6db or ADC_11db. | ||
|
||
adcAttachPin | ||
^^^^^^^^^^^^ | ||
|
||
This function is used to attach pin to ADC (it will also clear any other analog mode that could be on) | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: arduino | ||
|
||
bool adcAttachPin(uint8_t pin); | ||
|
||
This function will return ``true`` if configuration is successful. Else returns ``false``. | ||
|
||
ADC API specific for ESP32 chip | ||
******************************* | ||
|
||
analogSetWidth | ||
^^^^^^^^^^^^^^ | ||
|
||
This function is used to set the hardware sample bits and read resolution. | ||
Default is 12bit (0 - 4095). | ||
Range is 9 - 12. | ||
|
||
.. code-block:: arduino | ||
|
||
void analogSetWidth(uint8_t bits); | ||
|
||
analogSetVRefPin | ||
^^^^^^^^^^^^^^^^ | ||
|
||
This function is used to set pin to use for ADC calibration if the esp is not already calibrated (pins 25, 26 or 27). | ||
|
||
.. code-block:: arduino | ||
|
||
void analogSetVRefPin(uint8_t pin); | ||
|
||
* ``pin`` GPIO pin to set VRefPin for ADC calibration | ||
|
||
hallRead | ||
^^^^^^^^ | ||
|
||
This function is used to get ADC value of HALL sensor conneted to pins 36(SVP) and 39(SVN). | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: arduino | ||
|
||
int hallRead(); | ||
|
||
This function will return hall sensors value. | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
Example Applications | ||
******************** | ||
|
||
Here is an example of how to use the ADC. | ||
|
||
.. literalinclude:: ../../../libraries/ESP32/examples/AnalogRead/AnalogRead.ino | ||
:language: arduino | ||
|
||
Or you can run Arduino example 01.Basics -> AnalogReadSerial. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
void setup() { | ||
// initialize serial communication at 115200 bits per second: | ||
Serial.begin(115200); | ||
|
||
//set the resolution to 12 bits (0-4096) | ||
analogReadResolution(12); | ||
} | ||
|
||
void loop() { | ||
// read the analog / millivolts value for pin 2: | ||
int analogValue = analogRead(2); | ||
int analogVolts = analogReadMilliVolts(2); | ||
|
||
// print out the values you read: | ||
Serial.printf("ADC analog value = %d\n",analogValue); | ||
Serial.printf("ADC millivolts value = %d\n",analogVolts); | ||
|
||
delay(100); // delay in between reads for clear read from serial | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.