Interesting ESP32 issue: Can't use analogRead in ESP_WiFiManager Library
Posted: Tue Oct 20, 2020 11:45 pm
See already solved issue Not able to read analog port when using the autoconnect example in ESP_WiFiManager Library
The root cause of this issue is as follows
1. ESP32 has 2 ADCs, named ADC1 and ADC2
2. ESP32 ADCs functions
- ADC1 controls ADC function for pins GPIO32-GPIO39
- ADC2 controls ADC function for pins GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27
3.. ESP32 WiFi uses ADC2 for WiFi functions
- in order to use ADC2 for other functions, we have to acquire complicated firmware locks and very difficult to do
- It's not advisable to use ADC2 with WiFi
See in file adc_common.c
Please see the test results in Not able to read analog port when using the autoconnect example
The root cause of this issue is as follows
1. ESP32 has 2 ADCs, named ADC1 and ADC2
2. ESP32 ADCs functions
- ADC1 controls ADC function for pins GPIO32-GPIO39
- ADC2 controls ADC function for pins GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27
3.. ESP32 WiFi uses ADC2 for WiFi functions
- in order to use ADC2 for other functions, we have to acquire complicated firmware locks and very difficult to do
- It's not advisable to use ADC2 with WiFi
See in file adc_common.c
So it's suggested to use ADC1, and pins GPIO32-GPIO39In ADC2, there're two locks used for different cases:
1. lock shared with app and Wi-Fi:
ESP32:
When Wi-Fi using the ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed.
ESP32S2:
The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.
2. lock shared between tasks:
when several tasks sharing the ADC2, we want to guarantee
all the requests will be handled.
Since conversions are short (about 31us), app returns the lock very soon,
we use a spinlock to stand there waiting to do conversions one by one.
adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
Please see the test results in Not able to read analog port when using the autoconnect example