Interface an ESP32 with the MQ9 gas sensor

To interface an ESP32 with the MQ9 gas sensor and control a LED based on gas concentration, here’s the step-by-step guide, including the code and wiring details:

Wiring Diagram and Connection

MQ9 Gas Sensor Pinout:

  • VCC (Pin 1): Connect to the 3.3V pin of the ESP32.
  • GND (Pin 2): Connect to the GND pin of the ESP32.
  • AOUT (Pin 3): Analog output pin, which will be connected to an analog input pin on the ESP32 GPIO22.
  • DO (Pin 4): Digital output (optional), used for detecting if the gas concentration exceeds a threshold. You can leave this unconnected if you are only using the analog output.

LED Wiring:

  • LED Anode (+): Connect to GPIO23 (or any other available digital pin).
  • LED Cathode (-): Connect to a 220Ω resistor, then connect the other side of the resistor to GND.

Code for ESP32 with MQ9 Gas Sensor

Here’s a simple code that reads the analog value from the MQ9 gas sensor and turns on the LED if a certain gas threshold is exceeded.

Code:

#define MQ9_PIN 22  // Analog pin connected to AOUT of MQ9 sensor
#define LED_PIN 23  // Pin connected to the LED

// Threshold for gas concentration (you can adjust based on your sensor calibration)
int gasThreshold = 400; 

void setup() {
  // Initialize Serial Monitor for debugging
  Serial.begin(115200);

  // Initialize the LED pin as an output
  pinMode(LED_PIN, OUTPUT);

  // Initialize the MQ9 sensor analog pin
  pinMode(MQ9_PIN, INPUT);
}

void loop() {
  // Read the analog value from MQ9 sensor
  int sensorValue = analogRead(MQ9_PIN);

  // Print the sensor value for debugging
  Serial.print("MQ9 Sensor Value: ");
  Serial.println(sensorValue);

  // Check if the sensor value exceeds the gas threshold
  if (sensorValue > gasThreshold) {
    // Turn on the LED if gas concentration is high
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Gas concentration is high, LED ON!");
  } else {
    // Turn off the LED if gas concentration is low
    digitalWrite(LED_PIN, LOW);
    Serial.println("Gas concentration is normal, LED OFF!");
  }

  // Delay for a while before next reading
  delay(1000);
}

Explanation:

  • MQ9_PIN is the analog pin where the output of the MQ9 gas sensor is connected (in this case, GPIO22).
  • LED_PIN is where the LED is connected (GPIO23).
  • The analogRead(MQ9_PIN) function reads the gas concentration from the MQ9 sensor and outputs a value. This value is checked against a threshold to determine if the LED should turn on or off.
  • The gasThreshold value can be adjusted based on the environment and the specific gas levels you are monitoring. You should calibrate this value based on your tests.

Considerations:

  • The MQ9 sensor may require a preheating time (up to 24 hours) before it starts providing reliable readings.
  • You can adjust the threshold value based on the gas concentration levels you are trying to detect (such as CO, methane, or LPG).
  • The sensor’s output may vary, so it might need calibration to match your environmental conditions.

Power Supply Considerations:

  • The ESP32 works with a 3.3V supply, and the MQ9 sensor works with 5V for proper operation. If you’re using the MQ9’s heating element, ensure it’s powered correctly and that you’re reading the analog output through the ESP32’s ADC pins, which are 3.3V tolerant.

This setup will allow you to monitor gas concentration levels and control an LED indicator based on the detected gas levels.