4

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)); }

Coder9390
  • 512
  • 1
  • 7
  • 24

1 Answers1

14

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.

jsotola
  • 1,524
  • 2
  • 12
  • 20
Majenko
  • 105,761
  • 5
  • 80
  • 138