0

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.

k_evan_a
  • 13
  • 2
  • Did you try to use a different pin? Try pin 36 which is [A0](https://github.com/espressif/arduino-esp32/blob/master/variants/esp32/pins_arduino.h#L25-L40) and is one of the four Input-only pins on ESP32. – hcheung Oct 31 '21 at 00:20
  • 1
    ADC Limitations: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#adc-limitations – Juraj Oct 31 '21 at 04:58

1 Answers1

0

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.

Christian
  • 187
  • 5
  • 1
    What's the source of this advice? You're claiming all ADC2 inputs are entirely unusable which is not what the [official documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#adc-limitations) says - depending on hardware, GPIOs 0, 2, 4 or 15 may be unusable, but the others are simply shared with WiFi. – Tarmo Oct 31 '21 at 08:57
  • It's from my notes, which are originally from the official documentation. Looks like I may have misunderstood something when I took the note so I need to have a second look. Thanks for pointing it out. I'll need to understand better what 'shared with WiFi' means as I'm not sure how they can be shared and still used to read analog voltages. – Christian Oct 31 '21 at 09:37
  • Tarmo is right, if you read https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#adc-limitations, the first sentence will tell you that. Basically all the strapping pins. – hcheung Oct 31 '21 at 09:40