| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| en:edu:zmitac:macro:iot:labesp32 [2020/03/12 09:55] – [Laboratory scenarios] pczekalski | en:edu:zmitac:macro:iot:labesp32 [2020/03/12 11:08] (current) – [Laboratory scenarios] pczekalski |
|---|
| |
| ** Task 7 ** - reading analog data\\ | ** Task 7 ** - reading analog data\\ |
| In this task, you will read analogue data via integrated ADCs of the ESP32. ESP has 15 channels exposed (18 total) of the up to 12-bit resolution ADCs. Reading of the raw data (12-bit resolution is the default, 8 samples per measure as default) is easy. Just execute ''analogRead(GPIO)''.\\ | In this task, you will read analogue data via integrated ADCs.\\ |
| | Hardware part includes use of the "Joystick" sensor, composed of two potentiometers (one per axis) and a button (we do not use it here). |
| | __Please note correct pinout. Connect **GND** to board's GND, **5V** to 3.3V (**NOT 5V!**) even if on-board print states 5V.!__. Analogue resistance (voltage divider) goes through VRx and VRy.\\ |
| | |
| | ESP32 has 15 channels exposed (18 total) of the up to 12-bit resolution ADCs. Reading of the raw data (12-bit resolution is the default, 8 samples per measure as default) is easy using ''analogRead()'' function. \\ |
| | Technically, under the hood on the hardware level, there are two ADCs (ADC1 and ADC2). ADC 1 uses GPIOs 32 through 39. ADC2 GPIOs 0,2,4, 12-15 and 25-27. Note, ADC2 is used for WiFi purposes so you cannot use it when WiFi communication is enabled (so cannot you use GPIOs). Other limitations include use of GPIOs 0, 2, 4 an 15 in ESP WROVER KIT as mentioned above (those are already bound to other hardware). \\ |
| | Just execute ''analogRead(GPIO)''.\\ |
| A number of useful functions is here (not limited to): | A number of useful functions is here (not limited to): |
| * ''analogReadResolution(res)'' - where ''res'' is a value between 9 and 12 (default 12). For 9-bit resolution, you get 0..511 values, for 12-bit resolution, it is 0..4095 respectively. | * ''analogReadResolution(res)'' - where ''res'' is a value between 9 and 12 (default 12). For 9-bit resolution, you get 0..511 values, for 12-bit resolution, it is 0..4095 respectively. |
| * ''analogSetCycles(ccl)'' - where ''ccl'' is number of cycles per ADC sample. Default is 8, valid number is between 1 and 255. | * ''analogSetCycles(ccl)'' - where ''ccl'' is number of cycles per ADC sample. Default is 8, valid number is between 1 and 255. |
| | * ''analogSetClockDiv(divider)'' - sets base clock divider for the ADC. That has an impact on the speed of conversion. |
| * ''analogSetAttenuation(a)'' and ''analogSetPinAttenuation(GPIO,a)'' - sets input attenuation (for all channels or for selected channel). Default is ''ADC_11db''. This paremeter reflects dynamic scalling of the input value: | * ''analogSetAttenuation(a)'' and ''analogSetPinAttenuation(GPIO,a)'' - sets input attenuation (for all channels or for selected channel). Default is ''ADC_11db''. This paremeter reflects dynamic scalling of the input value: |
| * ''ADC_0db'' - no attenuation (1V on input = 1088 reading on ADC) | * ''ADC_0db'' - no attenuation (1V on input = 1088 reading on ADC), so full scale is 0..1.1V |
| * ''ADC_2_5db'' - 1.34 (1V on input = 2086 reading on ADC) | * ''ADC_2_5db'' - 1.34 (1V on input = 2086 reading on ADC), so full scale is 0..1.5V |
| * ''ADC_6db'' - 1.5 (1V on input = 2975 reading on ADC) | * ''ADC_6db'' - 1.5 (1V on input = 2975 reading on ADC), so full scale is 0..2.2V |
| * ''ADC_11db'' - 3.6 (1V on input = 3959 reading on ADC) - **warning - some people report that using it bricks ESP board!** | * ''ADC_11db'' - 3.6 (1V on input = 3959 reading on ADC), so full scale is 0..3.9V |
| | |
| | <note important>Do not execute consequent way ''analogRead()''. As technically all channels use same two registers (ADC1 and ADC2) you need to give it some time to sample (i.e. ''delay(100)'' between consecutive reads on different channels).</note> |
| | |
| | During your exercise, you may start from informing ESP to start sampling on particular GPIOs, usually in the ''setup()'' function: |
| | <code c> |
| | ... |
| | #define VRxPIN 25 |
| | #define VRyPIN 26 |
| | ... |
| | void setup() |
| | { |
| | ... |
| | adcAttachPin(VRxPIN); |
| | adcAttachPin(VRyPIN); |
| | // here you can also change clock divider, number of cycles per sample and so on. |
| | ... |
| | } |
| | </code> |
| ==== Resources ==== | ==== Resources ==== |
| - [[https://www.espressif.com/en/products/hardware/esp-wrover-kit/resources|ESP32 WROVER kit starting page]] | - [[https://www.espressif.com/en/products/hardware/esp-wrover-kit/resources|ESP32 WROVER kit starting page]] |