0

I’m trying to use an MQ2 gas sensor with my ESP32, but I’m having some issues. I've already done the preheating process for the MQ2 sensor, but the analog reading from the sensor always shows 0 in the serial monitor. When I connect the same sensor and circuit to an Arduino Uno (using A0 pin), it works fine and gives good analog readings.

Code I'm using:

#define MQpin 34

float RL = 1.0;               //load resistance (in kilo Ohms)
float analogVal,VRL, Rs, Ro;

void setup(){
  Serial.begin(115200);
  pinMode(MQpin, INPUT);
}
 
void loop(){
  analogVal = analogRead(MQpin);
  VRL = analogVal * (3.3 / 4095.0);
  Rs = RL * ((3.3/VRL) - 1);
  Ro = Rs / 9.531618832347878;  // from datasheet MQ2

  Serial.print("Analaog val = ");
  Serial.println(analogVal);
  Serial.print("Ro in clean air = ");
  Serial.println(Ro);i

I’m not using WiFi or Bluetooth functionalities on the ESP3. I also changed the ADC pin from GPIO 34 to other ADC pins on the ESP32, but the readings are still 0. Previously, I got some readings, but they were highly unstable, fluctuating significantly, and sometimes dropping to 0. But now, after another attempt with preheating, the sensor consistently reads 0.

Is there any specific configuration required on the ESP32 to read analog pins that I might be missing? or any recommendations to ensure proper analog readings from the MQ2 sensor on the ESP32?

4
  • Check that you can read an analogue voltage presented at the input pin (and/or use a DVM to see the voltage present when the sensor is connected). You should check the manual for configuration of IO pins as analogue inputs - I think this might help you (eg config ADC1 for input) Commented Dec 29, 2024 at 9:31
  • If RL is 1k ohm resistor, the value of RL should be 1000.0 instead of 1.0. Commented Dec 29, 2024 at 10:45
  • BTW, Are you connecting the MQ02 to 5V rail or 3.3v? You may want to add analogSetAttenuation(ADC_11db); to setup() to limited the reading not to exceed 3.3v. Commented Dec 29, 2024 at 10:58
  • @hcheung, I defined #define RL 1 because the reference I used specifies RL in kiloOhms, not Ohms and I connected the MQ2 sensor to the 5V rail on the ESP32, but I added a voltage divider circuit to ensure the voltage going to the ESP32’s analog input does not exceed 3.3V. Commented Dec 29, 2024 at 14:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.