I am working on a project where I need to read NTC thermistors via analogRead() function and then send them to another ESP32 via ESPNow.
The problem is that analogRead() doesn’t read anything if I initialize WiFi via WiFi.mode(WIFI_STA) or WiFi.mode(WIFI_AP). I can’t understand why, and I have tried everything. Is there any known ESP32 problem with analogRead() and WiFi?
If I measure the thermistor before initializing WiFi, it works fine. But once I set WiFi to any mode, it stops working and reads 3.14V everytime, in any Pin.
I have already tried (and didn’t work):
- Disconnecting WiFi 3 seconds before taking the measures via WiFi.disconnect(true); WiFi.mode(WIFI_OFF);
- Restarting the ESP32 after every measure via ESP.restart()
- Using DeepSleep between measures
- I know it’s not a ESPNow library problem because it doesn’t work even not using any ESPNow function.
The strange part is that, if I restart the ESP32 via the reboot button, the first measure (just before initializing WiFi) works fine.
Can anybody please help me?
Thanks in advance 🙂
I’m reading from 10 of them:
#define THERMISTORPIN1 25 #define THERMISTORPIN2 26 #define THERMISTORPIN3 27 #define THERMISTORPIN4 13 #define THERMISTORPIN5 2 #define THERMISTORPIN6 39 #define THERMISTORPIN7 34 #define THERMISTORPIN8 15 #define THERMISTORPIN9 12 #define THERMISTORPIN10 4
I tried connecting the pins one by one, but, after WiFi, none of them worked…
Can you just try reading one GPIO first with analogRead and Wi-Fi?
For example just use GPIO 27 with your Wi-Fi sketch.
I tried what you said, and I got some results:
it works with PINs 36, 39, 34 and 35. I tried all the pins one by one and the rest of them doesn’t work once I inicializate WiFi. Do you know what can be causing that?
I need to read at least 10 Thermistors.
Thanks in advance,
I’m not sure why to be honest. I had the same problem with GPIO 4 before… I think it’s something about the WiFi library that doesn’t work with all pins. I don’t know for sure which pins work with analogRead() + Wi-Fi, so only testing will give you the accurate results.
I met hear from ESP web. He have note for ADC2 can not work between
Since the ADC2 is shared with the WIFI module, which has higher priority, reading operation of
adc2_get_raw() will fail between
esp_wifi_stop(). Use the return code to see whether the reading is successful
So is there any solution to this apart from disconnecting from wifi to read analogue values and then reconnecting to send data?
I have been chasing around this problem for a couple of days and have just come to the same conclusion with a simple test program. I have tried pins 4, 13 and 14 and they all exhibit the same behaviour. Are there any pins that work?
Hello David, this problem happens with ADC2 GPIOS, so you should be able to do analog reading and Wi-Fi on Arduino IDE using these pins:
- GPIO 36
- GPIO 39
- GPIO 34
- GPIO 35
Hi Rui, thanks for coming back on this so quickly. Yes I have tested GPIO 35 and it is fine. I also found that 32 and 33 are good too, the other pins on ADC1. So this solves the problem. Thanks so much!
Can we use any adcs2 pins for digital opertions whilst using wifi.begin or should we not use them at all?
You probably have moved on to other things by now, but to measure the analog voltage from several analog sources you might consider using an analog switch to select which analog source you want to read.
This would allow you to use a single ADC1 pin on the ESP32 to read the selected source, then select a new source with the analog switch.
Four GPIO lines configured as output would go to the S0, S1, S2, S3 pins of the analog switch and would select which of 16 analog sources would be routed to the ESP32’s analog input pin.
This is one analog switch:
The CD74HC4067 takes 5V to operate, would use four digital output GPIO pins to select the source to read, and could support reading up to 16 analog sources.
Hope this helps.