Search code examples
arduinowifiesp32gpioarduino-esp32

ESP32 WiFi initialization disrupting GPIO analog reading


I'm programming an ESP32 through Arduino IDE, and I'm having a strange issue with Arduino's WiFi library. When I connect to my WiFi network, it forces reading of a particular pin (Pin 2) to 4095. With the line of code connecting to WiFi commented out, I get a correct reading on a sensor I have connected to that pin, but with it included it's stuck at 4095. This also happens when the microcontroller is disconnected from the rest of the circuit. With the line commented out I get a white noise reading, but the 4095 reading with the line included. Here's the code:

//Libraries 
#include <WiFi.h>

//Wi-Fi Connection Parameters
const char* ssid     = "REMOVED";
const char* password = "REMOVED";

const int sensePin = 2;

//Initialize WiFi Server
WiFiServer server(80);

void setup() {
  pinMode(sensePin, INPUT);
  int senseOut = 0;

  Serial.begin(115200);

  // Connect to WiFi network
  WiFi.begin(ssid, password); //THIS LINE CAUSES 4095 READING
}

void loop() {
  TestSensor();
}

void TestSensor()
{
  for (int i = 0; i < 100; i++)
  {
  senseOut = analogRead(sensePin);
  Serial.println(senseOut);
  delay(100);
  }
}

Example of white noise reading:

1251
1263
1275
1254
1237
1200
1149
1095
1040
976
928
868
835
805
806
820
778
752
819
1002
1516
1675
1687
1693
1659
1674
1702
1713
1727

Any ideas what could be causing this? Thank you.


Solution

  • You need to use another analog input.

    These pins do not support analog input when WiFi is in use: 00, 02, 04, 12, 13, 14, 15, 25, 26,

    These ones do: 32, 33, 34, 35, 36, 39, Use one of these last 6 to connect your sensor.