ESP32 的 GPIO 管脚是否可以在 WiFi 工作时使用?

Can you use all the ESP32's GPIO pins when the WiFi is working?

我有一个奇怪的现象,当我添加 WiFi 库和所有设置时,3 个传感器中有 2 个停止工作。当我删除 WiFi 代码时,它像以前一样工作。

我有一个 esp32 devkit v1 板并连接了 3 个传感器,它们是光敏电阻 (ky-018)、dht-11 和电容式土壤湿度传感器。

我试过换针;没有帮助。

代码如下:

#include "DHT.h"
#include <WiFi.h>

#define DHTPIN 14       // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11

const char* ssid = "Cgates_E031F1"; // ESP32 and ESP8266 uses 2.4GHZ wifi only
const char* password = "60E541C32F";

DHT dht(DHTPIN, DHTTYPE);
const byte lightPin = 13;
int lightReading;
int lightReadingpercent=0;
const int RELAY_PIN = 15;    // the Arduino pin, which connects to the IN pin of relay
const int AirValue = 4095;   //you need to replace this value with Value_1
const int WaterValue = 2200; //you need to replace this value with Value_2
const int SensorPin = 15;    // Soil moisture

int soilMoistureValue = 0;
int soilmoisturepercent=0;
const int Lightvalue = 0;
const int Darkvalue = 4095;
 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  // begin Wifi connect
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(2000);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  //end Wifi connect
 
  pinMode(RELAY_PIN, OUTPUT);//relay
  
  Serial.println(F("DHTxx test!")); //dht
  ; 
  dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:

  lightReading = analogRead(lightPin); //0-4095 12bit -- esp8266 10bit 0-1023 -- arduino 8bit 0-254
 
  Serial.print("Light reading = ");
  
  lightReadingpercent = map(lightReading, Darkvalue, Lightvalue,  0, 100 );
  Serial.print(lightReadingpercent);
  Serial.println(" %");
  Serial.println();
  
  delay(500);

  soilMoistureValue = analogRead(SensorPin);  //put Sensor insert into soil 

  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

  if (soilmoisturepercent > 100) {
    Serial.println("Soil moisture ");
    Serial.println("100 %");
    delay(500);
  } else if (soilmoisturepercent <0) {
    Serial.println("Soil moisture ");
    Serial.println("0 %");
    delay(500);
  } else if (soilmoisturepercent >=0 && soilmoisturepercent <= 100) {
    Serial.println("Soil moisture "); //go to next line
    Serial.print(soilmoisturepercent);
    Serial.println("%");
    delay(500); // soil end
  }
 
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F(" Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("C "));
  Serial.print(f);
  Serial.print(F("F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("C "));
  Serial.print(hif);
  Serial.println(F("F"));

  delay(500); //wait 0.5seconds
}

问题可能是,根据GitHub上的comment on this issue

ADC2 pins can not be used when WiFi is used. On the other hand, ADC1 pins can be used even when WiFi is enabled.

这可能是因为 WiFi 固件 运行 在具有 ADC2 外设的内核上使用了 ADC2。

虽然这并不能解释为什么 GPIO14 适合您,但是您仍然可以尝试仅使用使用 ADC1 的 GPIO 引脚,看看它是否适合您。

此外,WiFi 可以拉动相当大的电流;确保电源符合要求,以免电压下降。