MQ-2 problem

Hello everyone
I’m having a strange problem.I am using MQ-2 with ESP32-CAM.I can see the value for MQ-2 sensor when I using sensor without Bylnk.But i cannot see the sensor value with bylnk, it always displays zero in serial monitor.I tried to use the “MQUnifiedsensor” library, also i tried using all the ESP32-CAM pins, And various codes, but the problem remained. The MQ-2 has a problem communicating with Blynk. Please Help me.

Obviously this is all guesswork, because you’ve provided no code, but my guess is that you’ve fallen into the trap of choosing an analog pin which is connected to ADC2…

Pete.

Thank you for your response
Sorry I didn’t post any code. But I would like to point that I use ESP32-CAM and have tried all its pins (Because I saw earlier responses to this problem with ESP32 and that it was solved using pin 36).

#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#define BLYNK_TEMPLATE_ID "TMPL2f4fc-df5"
#define BLYNK_TEMPLATE_NAME "cam"
#define BLYNK_AUTH_TOKEN "uhjBMBQNuQg-dvZkG8WLKjzAqavqBLkZ"

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

 
char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "";  // Enter your wifi name
char pass[] = "";  // Enter your wifi password
int smoke = 2;
int data = 0;
int sensorThres = 2000;


BlynkTimer timer;

void sendSensor(){
 
 int data = analogRead(smoke);
 Blynk.virtualWrite(V0, data);
  Serial.print("smoke pin : ");
  Serial.println(data);


  if(data > 2000)     // Change the Trashold value
  {
    
    Blynk.logEvent("gas_alert","Gas Leakage Detected");
  }
}

void setup(){
  pinMode(smoke, INPUT);
   Serial.begin(115200);
   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  Blynk.begin(auth, ssid, pass);
  
  timer.setInterval(2500L, sendSensor);
}

void loop(){
 Blynk.run();
  timer.run();
}

@5ab4410af9ca10 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Sure. it’s done

Exactly as I thought, you’re using an analog pin which is connected to ADC2, which can’t be used at the same time as WiFi.

If it’s the AI-Thinker ESP32-CAM board that you’re using then none of the ADC1 GPIOs are broken-out to physical pins, so you may have made a poor choice of board.

Pete.

1 Like

Yes, Unfortunately i’m using the AI-Thinker ESP32-CAM board. And it seems that this problem is with BLE as well (i tried this).
I want to ask. The ESP32-CAM is a dual-core microcontroller. So can the MQ-2 sensor work independently with other sensors connected to Blynk in the same code ? (It is not necessary for the MQ-2 to be connected to Blynk in my project). If this is not possible, can this problem be solved using a module or something. Thank you Pete

I don’t understand what you tried, but Blynk IoT doesn’t support Bluetooth.

The bottom line is that the ESP32 architecture doesn’t allow WiFi and ADC2 analog pins to be used at the same time. You can’t get around that issue, because the hardware doesn’t allow it.

You either need to use a module that does have ADC1 pins broken-out to physical pins or use a separate MCU for taking the MQ-2 readings.

Pete.

So i can fix this problem by making a serial communication between ESP32-cam and Arduino uno or NodeMCU which read the MQ-2 value and send it to the ESP32.

I guess that’s potentially possible, but it’s very messy and wouldn’t be the way I’d go.
If you used a NodeMCU the just send the data direct to Blynk.

Pete.

“but it’s very messy and wouldn’t be the way I’d go”
So what is the easiest way to solve the problem. Please provide the name of the piece. Sorry for the many questions

Connect the MQ-2 to a NodeMCU and connect the NodeMCU to Blynk directly via WiFi.

Pete.

Thank you Pete for all of your answers

Hi Pete
It seems that Blynk with ESP32-cam has a problem with all the sensors (I even tried a variable resistance for that).
But I want to let you know that there is a good thing, I was able to operate the DHT11 sensor through Blynk. I faced the same problem that I faced with the MQ-2 sensor, but I managed to solve it (with the DHT11) by disconnecting the wires from the sensor and then reconnecting them after uploading the code. However, this approach didn’t work with the MQ-2 sensor, even though the DHT11 sensor operates somewhat similarly to the MQ-2 (both using thermistors).
Could you please explain this? Additionally, is there a solution based on what I did with the DHT11 that might work for the MQ-2 sensor ?

No they don’t. One is using a digital pin and the other is using an analog pin.
I’ve already explained that your board has no ADC1 pins broken-out to physical pins on the board, so you’ll never be able to use an analog sensor (whether that be an MQ2 or a resistor) with your board and use WiFi at the same time.

That’s probably due to the pin you chose for your DHT sensor interfering with the boot process.

Pete.

The MQ-2 contains a digital Pin as well. I tried that and it did not work !

What exactly did you try?

Pete.

I tried using the digital pin of the MQ-2 (Hign and Low) and as usual it always shows zero in Blynk. Do you want to see the code ?

Yes, and any other information that might be relevant.

Pete.

#define BLYNK_TEMPLATE_NAME "cam"
#define BLYNK_AUTH_TOKEN "uhjBMBQNuQg-dvZkG8WLKjzAqavqBLkZ"

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "";
char ssid[] = "Votre_SSID";
char pass[] = "Votre_Mot_de_passe_WiFi";

int pinDO = 2;


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(pinDO, INPUT);
}

void loop()
{
  Blynk.run();

  int smokeDigitalValue = digitalRead(pinDO);

  if (smokeDigitalValue == HIGH)
  {
    Serial.println("Danger : Gaz détecté !");
    Blynk.logEvent("Attention ! Gaz détecté !");
  }

  delay(1000); 
}