I have an ESP32, the current program running on it uses WiFi but, if I use WiFi and Analog read at the same time, Analog read does not work.
Why does this happen and what's the way around it?
Hardware - ESP32, Pin 25 for the ADC
Code-
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.begin("You really", "Want to know"); // If this line is commented then it works
}
void loop() {
Serial.println(analogRead(25));
}
-
1What do you mean with "analogRead does not work"? What happens when you try to read an analog value from a pin? Please describe your hardware setup and show some code that demonstrates the problem.StarCat– StarCat06/26/2021 05:54:14Commented Jun 26, 2021 at 5:54
-
1When WiFi is being used the values from analogRead stay at 0 whether connected to 3.3 or 100 voltsCoder9390– Coder939006/26/2021 10:36:19Commented Jun 26, 2021 at 10:36
-
1That is not normal behaviour so it's probably caused by your specific setup. Please provide some more information. What about your code and hardware setup? Are you using the correct pin(s). If you have connected an analog port to 100 volts you have probably killed your ESP32.StarCat– StarCat06/26/2021 10:47:10Commented Jun 26, 2021 at 10:47
-
1l'll update the question with the code, "100 Volts" was supposed to sarcastic as it shows only 0 connected to any voltageCoder9390– Coder939006/26/2021 13:10:57Commented Jun 26, 2021 at 13:10
-
1This code is a simplified version, but the problem exists here tooCoder9390– Coder939006/26/2021 13:22:53Commented Jun 26, 2021 at 13:22
1 Answer 1
The ESP32 has two ADCs. One of them, ADC2, is actively used by the WiFi.
From the IDF documentation:
Since the ADC2 module is also used by the Wi-Fi, only one of them could get the preemption when using together, which means the adc2_get_raw() may get blocked until Wi-Fi stops, and vice versa.
That means you can't use the ADC on any of the ADC2 channels while WiFi is on: GPIO0, GPIO2, GPIO4, GPIO12, GPIO13, GPIO14, GPIO15, GPIO25, GPIO26 and GPIO27.
But you can use ADC1, which uses pins GPIO32, GPIO33, GPIO34, GPIO35, GPIO36, GPIO37, GPIO38 and GPIO39.