Why does the MQ2 sensor not read value when wifi code is present

So I'm using an MQ2 smoke sensor in a project and the project requires wifi also
When I don't include the wifi code the sensor read value
When I include the wifi code the sensor doesn't read and gives 0.
I'm using an esp32 dev kit v4 and the MQ2 smoke sensor is connected to pin 25
This is my code


#include <WiFi.h>
const char* ssid = "ssid";
const char* password =  "password";

int MQ2Sensor;
int MQ2;
int buzzer = 18;
int led = 16;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
  delay(10000);
}

void loop() {
  MQ2Sensor = analogRead(25);
  MQ2 = MQ2Sensor - 2000;
  Serial.print("Sensor Value: ");
  Serial.print(MQ2);
  if (MQ2 > 400)
  {
    digitalWrite(buzzer, HIGH);
    digitalWrite(led, HIGH);
  }
  else {
    digitalWrite(buzzer, LOW);
    digitalWrite(led, LOW);
  }
  Serial.println("");
  delay(2000); // wait 2s for next reading
}

What can I do to solve it?

Use one of the ESP32's A:D converter pins. See the ESP32 API

Hi, thank you very much now that I change the pin to pin 35 it is working.
Thank you for the link too as it helped me to know that i cant use any other adc2 pin when i have wifi

1 Like